[]
Working with large documents can be a bit of a hassle because larger files generally require more time to load. However, DsPdf streamlines this workload efficiently, by simplifying certain functionalities. There are two approaches to creating a PDF file when working with DsPdf:
In this scenario, you'd build the document completely first by adding text, graphics and other elements. Then, you would call the Save() method on the document passing the name of the file or the stream to save the document to as the parameter. This approach allows you to modify the already created content - e.g., you can insert pages anywhere in the document, or modify the previously created pages.
However, when it comes to creating large files, this method isn’t the best. It uses a lot of memory and can slow down the document generation process.
Refer to the following code to see the conventional way of creating a PDF document in DsPdf.
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var g = doc.NewPage().Graphics;
g.DrawString("Hello, World!",
new TextFormat() { Font = StandardFonts.Times, FontSize = 12 },
new PointF(72, 72));
// The document is simply saved at the end
doc.Save(stream);
return doc.Pages.Count;
}
With this approach, you can provide the stream to save to at the very beginning of the document generation process, before adding any content to the document, by calling the StartDoc() method on the document. All content is then written directly to that stream, and you cannot go back and update the already created pages.
To complete the document, you call the EndDoc() method. If you try to perform an action that is not allowed, an exception will be thrown. While this approach is somewhat limiting (e.g. GcPdfDocument.Linearized property cannot be set to true in this mode), it uses less memory and may be preferable, especially when generating very large documents.
Refer to the following code sample to see an example of how to use the StartDoc and EndDoc methods:
public int CreatePDF(Stream stream)
{
// Number of pages to generate:
const int N = Common.Util.LargeDocumentIterations;
var doc = new GcPdfDocument();
// Start creating the document by this call:
doc.StartDoc(stream);
// Prep a TextLayout to bold/format the text:
var tl = new TextLayout(72)
{
MaxWidth = doc.PageSize.Width,
MaxHeight = doc.PageSize.Height,
MarginAll = 72,
};
tl.DefaultFormat.Font = StandardFonts.Times;
tl.DefaultFormat.FontSize = 12;
// Start with a title page:
tl.FirstLineIndent = 0;
var fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"));
var tf0 = new TextFormat() { FontSize = 24, FontBold = true, Font = fnt };
tl.Append(string.Format("Large Document\n{0} Pages of Lorem Ipsum\n\n", N), tf0);
var tf1 = new TextFormat(tf0) { FontSize = 14, FontItalic = true };
tl.Append(string.Format("Generated on {0}.", Common.Util.TimeNow().ToString("R")), tf1);
tl.TextAlignment = TextAlignment.Center;
tl.PerformLayout(true);
doc.Pages.Add().Graphics.DrawTextLayout(tl, PointF.Empty);
tl.Clear();
tl.FirstLineIndent = 36;
tl.TextAlignment = TextAlignment.Leading;
// Generate the document:
for (int pageIdx = 0; pageIdx < N; ++pageIdx)
{
tl.Append(Common.Util.LoremIpsum(1));
tl.PerformLayout(true);
doc.NewPage().Graphics.DrawTextLayout(tl, PointF.Empty);
tl.Clear();
}
doc.EndDoc();
return doc.Pages.Count;
}
Limitations
Modifying the already created content (e.g. inserting pages or modifying the already added pages) cannot be done in this mode.
The linearized property cannot be set to true in this mode.