AspPDF is capable of joining together two
or more PDFs to form a new document. This process
is often referred to as document stitching.
12.2.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) 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:
| VBScript |
|
Set Pdf = Server.CreateObject("Persits.Pdf")
' Open document 1
Set Doc1 = Pdf.OpenDocument( Server.MapPath("doc1.pdf") )
' Open document 2
Set Doc2 = Pdf.OpenDocument( Server.MapPath("doc2.pdf") )
' Append doc2 to doc1
Doc1.AppendDocument Doc2
' Save document, the Save method returns generated file name
Filename = Doc1.Save( Server.MapPath("stitch.pdf"), False )
|
| C# |
|
IPdfManager objPdf = new PdfManager();
// Open Document 1
IPdfDocument objDoc1 = objPdf.OpenDocument(Server.MapPath("doc1.pdf"), Missing.Value);
// Open Document 2
IPdfDocument objDoc2 = objPdf.OpenDocument(Server.MapPath("doc2.pdf"), Missing.Value);
// Append doc2 to doc1
objDoc1.AppendDocument(objDoc2);
// Save document, the Save method returns generated file name
String strFilename = objDoc1.Save( Server.MapPath("stitch.pdf"), false );
|
Click the links below to run this code sample:
http://localhost/asppdf/manual_12/12_stitch.asp
http://localhost/asppdf/manual_12/12_stitch.aspx
12.2.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 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.
| VBScript |
|
Set Pdf = Server.CreateObject("Persits.Pdf")
' Create empty document
Set Doc = Pdf.CreateDocument
' Open document to apply security to
Set Doc1 = Pdf.OpenDocument( Server.MapPath("doc1.pdf") )
' Copy properties
Doc.Title = Doc1.Title
Doc.Creator = Doc1.Creator
Doc.Producer = Doc1.Producer
Doc.CreationDate = Doc1.CreationDate
Doc.ModDate = Doc1.ModDate
' Apply security to Doc
Doc.Encrypt "abc", "", 128
' Append doc1 to doc
Doc.AppendDocument Doc1
' Save document, the Save method returns generated file name
Filename = Doc.Save( Server.MapPath("apply.pdf"), False )
|
| C# |
|
IPdfManager objPdf = new PdfManager();
// Create empty document
IPdfDocument objDoc = objPdf.CreateDocument( Missing.Value );
// Open Document 1
IPdfDocument objDoc1 = objPdf.OpenDocument( Server.MapPath("doc1.pdf"), Missing.Value );
// 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 Doc
objDoc.Encrypt( "abc", "", 128, Missing.Value );
// Append doc1 to doc
objDoc.AppendDocument( objDoc1 );
// Save document, the Save method returns generated file name
String strFilename = objDoc.Save( Server.MapPath("apply.pdf"), false );
|
Click the links below to run this code sample:
http://localhost/asppdf/manual_12/12_applysecurity.asp
http://localhost/asppdf/manual_12/12_applysecurity.aspx
12.2.3 Making Changes to Documents Being Appended
As mentioned earlier, a document being appended must be an existing
document opened via OpenDocument or OpenDocumentBinary. 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:
Set Doc1 = Pdf.OpenDocument(...)
Set Doc2 = Pdf.OpenDocument(...)
' Make changes to Doc2
Set Doc3 = Pdf.OpenDocumentBinary( Doc2.SaveToMemory )
Doc1.AppendDocument Doc3
This code fragment uses an intermediary memory-based document Doc3 to hold the modified version of Doc2.