Aspose.Pdf

Writing PDF directly

Problem

 

In the normal working mode of Aspose.Pdf , developers need to create an object tree through API or XML according to the Document Object Model of Aspose.Pdf and then save the object tree as PDF document. This working mode provides more flexibility for developers in integrating API & XML and editing the document object tree before saving it to PDF document. But, when the document is very large, the object tree takes too much memory which makes the server slow down.

 

Solution by Aspose.Pdf

 

Since version 1.7.0, Aspose.Pdf provides a new working mode: Direct-To-File Mode. In this working mode, when a new Paragraph is added to a PDF documnet, Aspose.Pdf writes it directly to the Pdf file and frees the memory used by the Paragraph . This working mode can be of great help for developers to save their system's memory usage when the document is large.

 

Please remember the steps below when working with Direct-To-File Mode:

 

 

Note: Close method of Pdf class is only used for Direct-To-File Mode.

 

Limitations

 

Every good thing comes at a cost. That's why, some of the features of Aspose.Pdf are lost in Direct-To-File Mode including:

 

 

Developers can select any working mode according to their business requirements to produce PDF documents effectively.

 

Direct-To-File Mode can be applied using API or XML. So, we have listed the example below using both approaches for your better understanding.

 

Using API

 

Code Snippet

 

[C#]

 

 

//Create a file stream to create the PDF document

FileStream fs = new FileStream("e:/temp/SingleSeg-d.pdf",FileMode.Create);

 

 

//Instantiate the Pdf instance and pass the file stream object to its constructor

Pdf pdf = new Pdf(fs);

 

 

//Add a section to the PDF document

Section sec1 = pdf.Sections.Add();

 

 

//Add 1000 text paragraphs to the section

for(int i = 0; i < 1000; i++)

{

     Text t = new Text("hello world hello world hello " + i.ToString());

     sec1.AddParagraph(t);

}

 

 

//Close the Pdf. This method is used only for direct file mode

pdf.Close();

 

 

[VB.NET]

 

 

'Create a file stream to create the PDF document

Dim fs As FileStream = New FileStream("e:/temp/SingleSeg-d.pdf",FileMode.Create)

 

 

'Instantiate the Pdf instance and pass the file stream object to its constructor

Dim pdf As Pdf = New Pdf(fs)

 

 

'Add a section to the PDF document

Dim sec1 As Section = pdf.Sections.Add()

 

 

'Add 1000 text paragraphs to the section

Dim i As Integer

For i = 0 To 1000- 1 Step 1

     Dim t As Text = New Text("hello world hello world hello " + i.ToString())

     sec1.AddParagraph(t)

Next

 

 

'Close the Pdf. This method is used only for direct file mode

pdf.Close()

 

Using XML

 

Code Snippet

 

[C#]

 

 

//Create a file stream to create the PDF document

FileStream fs = new FileStream("e:/temp/SingleSeg-d.pdf",FileMode.Create);

 

 

//Instantiate the Pdf instance and pass the file stream object to its constructor

Pdf pdf = new Pdf(fs);

 

 

//Bind the XML file to the Pdf and leave the XSL file parameter as Nothing

pdf.BindXML("Test.xml",null);

 

 

//Close the Pdf. This method is used only for direct file mode

pdf.Close();

 

 

[VB.NET]

 

 

'Create a file stream to create the PDF document

Dim fs As FileStream = New FileStream("e:/temp/SingleSeg-d.pdf",FileMode.Create)

 

 

'Instantiate the Pdf instance and pass the file stream object to its constructor

Dim pdf As Pdf = New Pdf(fs)

 

 

'Bind the XML file to the Pdf and leave the XSL file parameter as Nothing

pdf.BindXML("Test.xml",Nothing)

 

 

'Close the Pdf. This method is used only for direct file mode

pdf.Close()