AspPDF.NET is capable of joining together two
or more PDFs to form a new document. This process
is often referred to as document stitching.
14.1.1 AppendDocument Method
Document stitching is performed via the AppendDocument method
provided by the PdfDocument object.
This method expects a single argument: an instance
of the PdfDocument object representing another document
to be appended to the current document. The AppendDocument method
can be called more than once to append multiple documents
to the current one.
The PdfDocument object to which other documents are appended
(the master document) can either be
a new or existing document. The PdfDocument objects that get appended
must be all existing documents.
A document cannot be appended to
itself.
The master document determines the general and security properties
of the resultant document.
The following code sample appends the file doc2.pdf
to the end of the document doc1.pdf:
C# |
PdfManager objPDF = new PdfManager();
// Open Document 1
PdfDocument objDoc1 = objPDF.OpenDocument( Server.MapPath("doc1.pdf") );
// Open Document 2
PdfDocument objDoc2 = objPDF.OpenDocument( Server.MapPath("doc2.pdf") );
// Append doc2 to doc1
objDoc1.AppendDocument( objDoc2 );
|
VB.NET |
Dim objPDF As PdfManager = New PdfManager()
' Open Document 1
Dim objDoc1 As PdfDocument = objPDF.OpenDocument(Server.MapPath("doc1.pdf"))
' Open Document 2
Dim objDoc2 As PdfDocument = objPDF.OpenDocument(Server.MapPath("doc2.pdf"))
' Append doc2 to doc1
objDoc1.AppendDocument(objDoc2)
|
Click the links below to run this code sample:
http://localhost/asppdf.net/manual_14/14_stitch.cs.aspx
http://localhost/asppdf.net/manual_14/14_stitch.vb.aspx
14.1.2 Applying and Removing Security
A cumulative document produced by appending one or more PDFs to
a master document inherits the master document's security properties.
For example, if a master document is encrypted and the documents
appended to it are not, the resultant PDF
will be encrypted with the same passwords and permission flags
as the master document. Conversely, if the master document is unencrypted
and encrypted documents are appended to it, the result document
will be unencrypted.
This feature can be used to apply security to unsecure documents,
as well as modify or remove security from encrypted documents.
The idea is to create an empty document, call
the Encrypt method on it if necessary,
then append the PDF that needs security added or removed.
To be in compliance with Adobe PDF licensing requirements,
AspPDF.NET performs security removal only if the document
being appended is opened using the owner
password. Otherwise, an error exception is thrown.
The following code sample applies security
to the file doc1.pdf. Note that various document properties
are being copied from the original document (doc1.pdf) to the new one, because
by default the resultant PDF would inherit document properties
of the master PDF (in our case, an empty document) and the original
document's properties would be lost.
C# |
...
// Create empty document
PdfDocument objDoc = objPdf.CreateDocument();
// Open Document 1
PdfDocument objDoc1 = objPdf.OpenDocument( Server.MapPath("doc1.pdf") );
// Copy properties
objDoc.Title = objDoc1.Title;
objDoc.Creator = objDoc1.Creator;
objDoc.Producer = objDoc1.Producer;
objDoc.CreationDate = objDoc1.CreationDate;
objDoc.ModDate = objDoc1.ModDate;
// Apply security to objDoc, use pdfFull permission by default
objDoc.Encrypt( "abc", "", 128 );
// Append doc1 to doc
objDoc.AppendDocument( objDoc1 );
...
|
VB.NET |
...
' Create empty document
Dim objDoc As PdfDocument = objPdf.CreateDocument()
' Open Document 1
Dim objDoc1 As PdfDocument = objPdf.OpenDocument(Server.MapPath("doc1.pdf"))
' Copy properties
objDoc.Title = objDoc1.Title
objDoc.Creator = objDoc1.Creator
objDoc.Producer = objDoc1.Producer
objDoc.CreationDate = objDoc1.CreationDate
objDoc.ModDate = objDoc1.ModDate
' Apply security to objDoc, use pdfFull permission by default
objDoc.Encrypt("abc", "", 128)
' Append doc1 to doc
objDoc.AppendDocument(objDoc1)
...
|
Click the links below to run this code sample:
http://localhost/asppdf.net/manual_14/14_applysecurity.cs.aspx
http://localhost/asppdf.net/manual_14/14_applysecurity.vb.aspx
14.1.3 Making Changes to Documents Being Appended
As mentioned earlier, a document being appended must be an existing
document opened via OpenDocument. Changes made to a document
being appended will not propagate to the resultant compound document.
If you need to make changes to a document being appended, the following
workaround is recommended:
PdfDocument objDoc1 = objPDF.OpenDocument(...);
PdfDocument objDoc2 = objPDF.OpenDocument(...);
// Make changes to objDoc2
PdfDocument objDoc3 = objPDF.OpenDocument( objDoc2.SaveToMemory );
objDoc1.AppendDocument( objDoc3 );
This code fragment uses an intermediary memory-based document objDoc3 to hold the modified version of objDoc2.
14.1.4 Creating Multi-Page Documents Based on a Template
AppendDocument is not a very efficent way to create multi-page documents
based on a single-page PDF template. We recommend that the method CreateGraphicsFromPage
described in Section 9.6 be used for this task instead.
For a code sample, see our KB Article PS130905190.