Merge Options
When merging PDFs, often information such as JavaScript, a PDF's properties, metadata, form fields, and other content should be excluded from an existing PDF to be merged. Use the MergeOptions class to customize what is merged from an existing PDF.
MergeOptions
The MergeOptions class customizes how documents are merged. The MergeOptions class has numerous properties that customize which content from a PDF is merged.
Properties
Property | Description |
---|---|
All | Gets a MergeOptions object with all options set to true. |
AllOtherData | Gets or sets a value indicating if all other document data should be imported. |
Append | Gets a MergeOptions object that you would typically use when appending documents. |
DocumentInfo | Gets or sets a value indicating if document information should be imported. |
DocumentJavaScript | Gets or sets a value indicating if document level JavaScript should be imported. |
DocumentProperties | Gets or sets a value indicating if document properties should be imported. |
EmbeddedFiles | Gets or sets a value indicating if the Embedded files should be imported. |
FormFields | Gets or sets a value indicating if form fields should be imported. |
FormsXfaData | Gets or sets a value indicating if form XFA data should be imported. |
LogicalStructure | Gets or Sets the value indicating if logical structure should be imported. |
None | Gets a MergeOptions object with all options set to false. |
OpenAction | Gets or sets a value indicating if the documents opening action (initial page and zoom settings) should be imported. |
OptionalContentInfo | Gets or sets the Output Content should be imported. |
Outlines | Gets or sets a value indicating if outlines and bookmarks should be imported. |
OutputIntent | Gets or Sets a value indication if OutputIntent should be imported. |
PageAnnotations | Gets or sets a value indicating if annotations should be imported. |
PageLabelsAndSections | Gets or sets a value indicating if page labels and sections should be imported. |
RootFormField | Gets or sets the root form field for imported form fields. |
RootOutline | Gets or sets the root outline for imported outlines. |
XmpMetadata | Gets or sets a value indicating if Xmp Metadata should be imported. |
Using MergeOptions Statically
To simplify merging, there are three defined static MergeOptions properties that do not require a user to specify numerous properties needlessly when merging.
MergeOptions.All
- Used when propagating all merge options from the PDF being merged or appended.MergeOptions.Append
- Used when appending PDF documents.MergeOptions.None
- Used when excluding all merge options when merging or appending.
By default, if a MergeDocument instance's constructor omits specifying MergeOptions, then it uses the MergeOptions.All
as the merge options. When using a MergeDocument instance's Append method, if the method omits specifying merge options, then it uses MergeOptions.Append
as the merge options.
MergeOptions do not apply to the PDF to be produced by the MergeDocument instance, only to the PDF documents being loaded/appended into the MergeDocument.
The following example illustrates using MergeOptions statically two different ways. First, it merges two documents together and specifies MergeOptions.None
for both. Second, it creates a new MergeDocument instance and loads a PdfDocument and MergeOptions for that document. Then, it appends two PDF documents and specifies MergeOptions for those documents.
MergeDocument document = MergeDocument.Merge("DocumentB.pdf", MergeOptions.None, "outline-example.pdf", MergeOptions.None);
document.Draw(outputPath);
MergeDocument document1 = new MergeDocument("DocumentA.pdf", MergeOptions.None);
document1.Append("DocumentB.pdf", MergeOptions.None);
document1.Append("outline-example.pdf", MergeOptions.None);
document.Draw(outputPath);
Dim document As MergeDocument = MergeDocument.Merge("DocumentB.pdf", MergeOptions.None, "outline-example.pdf", MergeOptions.None)
document.Draw(outputPath)
Dim document As New MergeDocument("DocumentA.pdf", MergeOptions.None)
document.Append("DocumentB.pdf", MergeOptions.None)
document.Append("outline-example.pdf", MergeOptions.All)
document.Draw(outputPath)
Figure 1. Removing bookmarks, outline, tags, and attachment from merged PDFs.
Using MergeOptions
You are not limited to using MergeOptions statically, you can also specify exactly which options to exclude. Instantiate a MergeOptions instance - rather than use one of its static methods - for complete control over the document parts that are merged into the final PDF.
The following example illustrates creating a MergeOptions instance and setting several of its properties to false so they exclude the specified elements.
MergeDocument document = new MergeDocument("DocumentA.pdf", MergeOptions.All);
MergeOptions options = new();
options.Outlines = false;
options.EmbeddedFiles = false;
options.LogicalStructure = false;
document.Append("DocumentB.pdf", options);
document.Append("outline-example.pdf");
document.Draw(outputPath);
Dim document As New MergeDocument("DocumentA.pdf", MergeOptions.All)
Dim options As New MergeOptions() With {
.Outlines = False,
.LogicalStructure = False,
.EmbeddedFiles = False
} document.Append("DocumentB.pdf", options)
document.Append("outline-example.pdf")
document.Draw(outputPath)