When a PDF document is dynamically created from an application, all the pages are kept in memory until the document is saved and destroyed. This can create a performance problem if the document that is created is very large and/or contains a large number of images.
A more efficient way to create large documents is to use the SavePage/Clear combination of calls to keep only one page in memory.
The steps required to achieve this are the following:
Create a blank document.
Call IacDocument::StartSave (FileStream, acFileSaveView) to create the file on the storage device.
Insert objects into the first page, example text, images and graphics.
Once the first page is constructed, call IacDocument::SavePage to save the current page.
Call IacPage::Clear to remove the page contents from memory.
If the document contains bookmarks, call IacDocument::AddPage to add a new page to the document, otherwise construct the next page without adding a new page.
Repeat steps 3 to 6 for all the pages in the document.
Call IacDocument::EndSave to close the file.
Step 6 that consists of adding a new page is optional and is only needed if the bookmarks will be created after the document is finished. This step is needed so that the PDF Creator knows where to point the bookmarks to.
Only the acFileSaveView option of FileSaveOptionConstants in the StartSave method is supported.
Sub Sample()
' Constants for Activation codes
Const strLicenseTo As String = "Amyuni PDF Creator .NET Evaluation"
Const strActivationCode As String = "07EFCDAB0100010025C3B7B351579FF94C49112EAF7368612744C7237C2F6A215A53E83A9ECCFFE54C52063CB05338BDE555773D7B1B"
' Initialize library
' This should be done once
acPDFCreatorLib.Initialize()
' set license key This is needed only with licensed version
acPDFCreatorLib.SetLicenseKey(strLicenseTo, strActivationCode)
' Create a new PDF document
Dim doc As New Amyuni.PDFCreator.IacDocument()
' Create page object
Dim page As Amyuni.PDFCreator.IacPage
' Create new stream object
Dim fileWrite As System.IO.Stream = System.IO.File.OpenWrite("C:\temp\Large_pdf.pdf")
' Start to save the PDF File
doc.StartSave(fileWrite, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView)
' Define variables
Dim objectName As String
Dim picture As Amyuni.PDFCreator.IacObject
Dim textName As String
Dim text As Amyuni.PDFCreator.IacObject
' Save the first 60 pages
For pageIndex = 1 To 60
' Get the page from the document, only if we are adding new pages
page = doc.GetPage(pageIndex)
' Create a picture object on each page
objectName = "page" + pageIndex.ToString
picture = page.CreateObject(Amyuni.PDFCreator.IacObjectType.acObjectTypePicture, objectName)
' Configure the picture
picture.Attribute("Right").Value = page.GetPageFormat().Width
picture.Attribute("Bottom").Value = page.GetPageFormat().Length
picture.Attribute("FileName").Value = ("C:\temp\page" + pageIndex.ToString + ".jpg")
' Create a text annotation below each image
textName = "Label" + pageIndex.ToString
text = page.CreateObject(Amyuni.PDFCreator.IacObjectType.acObjectTypeText, textName)
' Configure the text
text.Attribute("Top").Value = page.GetPageFormat().Length - 300
text.Attribute("Right").Value = page.GetPageFormat().Width
text.Attribute("Bottom").Value = page.GetPageFormat().Length
text.Attribute("Text").Value = textName
text.Attribute("Annotation").Value = True
' insert a bookmark that points to this page
Dim bookmark As Amyuni.PDFCreator.IacBookmark
doc.RootBookmark.InsertChild(pageIndex, "Page " + pageIndex.ToString(), textName)
' Save the current page
doc.SavePageNumber(pageIndex)
' Clear the page contents to save memory
page.Clear()
' Add page, because we are adding BookMarks. Otherwise, it is not necessary
doc.AddPage(pageIndex)
Next
' End Save
doc.EndSave()
' Close the stream
fileWrite.Close()
' terminate library to free resources
acPDFCreatorLib.Terminate()
' destroy objects
doc.Dispose()
page.Dispose()
End Sub
static void Sample()
{
// Constants for Activation codes
const string strLicenseTo = "Amyuni PDF Creator .NET Evaluation";
const string strActivationCode = "07EFCDAB0100010025C3B7B351579FF94C49112EAF7368612744C7237C2F6A215A53E83A9ECCFFE54C52063CB05338BDE555773D7B1B";
// Initialize library
// This should be done once
acPDFCreatorLib.Initialize();
// set license key This is needed only with licensed version
acPDFCreatorLib.SetLicenseKey(strLicenseTo, strActivationCode);
// Create a new PDF document
Amyuni.PDFCreator.IacDocument doc = new Amyuni.PDFCreator.IacDocument();
// Create page object
Amyuni.PDFCreator.IacPage page;
// Create new stream object
System.IO.FileStream fileWrite = new System.IO.FileStream(@"C:\temp\Large_pdf.pdf",
System.IO.FileMode.Create,
System.IO.FileAccess.Write,
System.IO.FileShare.Read);
// Start to save the PDF File
doc.StartSave(fileWrite, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView);
// Define variables
String objectName;
Amyuni.PDFCreator.IacObject picture;
String textName;
Amyuni.PDFCreator.IacObject text;
// Save the first 60 pages
for (int pageIndex = 1; pageIndex < 61; pageIndex++)
{
// Get the page from the document, only if we are adding new pages
page = doc.GetPage(pageIndex);
// Create a picture object on each page
objectName = "page" + pageIndex.ToString();
picture = page.CreateObject(Amyuni.PDFCreator.IacObjectType.acObjectTypePicture, objectName);
// Configure the picture
picture.Attribute("Right").Value = page.GetPageFormat().Width;
picture.Attribute("Bottom").Value = page.GetPageFormat().Length;
picture.Attribute("FileName").Value = (@"C:\temp\page" + pageIndex.ToString() + ".jpg");
// Create a text annotation below each image
textName = "Label" + pageIndex.ToString();
text = page.CreateObject(Amyuni.PDFCreator.IacObjectType.acObjectTypeText, textName);
// Configure the text
text.Attribute("Top").Value = page.GetPageFormat().Length - 300;
text.Attribute("Right").Value = page.GetPageFormat().Width;
text.Attribute("Bottom").Value = page.GetPageFormat().Length;
text.Attribute("Text").Value = textName;
text.Attribute("Annotation").Value = true;
// insert a bookmark that points to this page
Amyuni.PDFCreator.IacBookmark bookmark;
doc.RootBookmark.InsertChild(pageIndex, "Page " + pageIndex.ToString(), textName);
// Save the current page
doc.SavePageNumber(pageIndex);
// Clear the page contents to save memory
page.Clear();
// Add page, because we are adding BookMarks. Otherwise, it is not necessary
doc.AddPage(pageIndex);
}
// End Save
doc.EndSave();
// Close the stream
fileWrite.Close();
// terminate library to free resources
acPDFCreatorLib.Terminate();
// destroy objects
doc.Dispose();
page.Dispose();
}