How can I help you?
PdfDocument
21 Apr 202624 minutes to read
Represents a PDF document and can be used to parse an existing PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Access first page
let page: PdfPage = document.getPage(0);
// Flatten annotations and form fields
document.flatten = true;
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();Properties
Get bookmarks PdfBookmarkBase
Gets the bookmarks (Read only).
Get fileStructure PdfFileStructure
Gets the internal structure of the PDF document.
Get flatten boolean
Gets the boolean flag to flatten the annotations and form fields.
Set flatten void
Sets the boolean flag to flatten the annotations and form fields.
| Parameter | Type | Description |
|---|---|---|
| value | boolean |
To flatten |
Get form PdfForm
Gets the PDF form fields included in the document (Read only).
Get isEncrypted boolean
Gets a value indicating whether the document is encrypted. (Read Only).
Get isUserPassword boolean
Gets a value indicating whether the document is decrypted using the user password. (Read only).
Get layers PdfLayerCollection
Gets the collection of PdfLayer from the document.
Get pageCount number
Gets the page count (Read only).
Get permissions PdfPermissionFlag
Gets the permission flag of the PDF document (Read only).
Methods
addPage
Creates a new page with default page settings and adds it to the collection.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Add a new PDF page
let page: PdfPage = document.addPage();
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();Returns PdfPage
addPage
Creates a new page with default settings and inserts it into the collection at the specified page index.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Create and insert a new PDF page at 5th index
let page: PdfPage = document.addPage(5);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| index | number |
Page index. |
Returns PdfPage
addPage
Creates a new page with specified page settings and adds it to the collection.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Create a new PDF page settings instance
let pageSettings: PdfPageSettings = new PdfPageSettings();
// Sets the margins
pageSettings.margins = new PdfMargins(40);
// Sets the page size
pageSettings.size = {width: 595, height: 842};
// Sets the page orientation
pageSettings.orientation = PdfPageOrientation.landscape;
// Add a new PDF page with page settings
page = document.addPage(pageSettings);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| pageSettings | PdfPageSettings |
Page settings. |
Returns PdfPage
addPage
Creates a new page with specified page settings and inserts it into the collection at the specified page index.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Create a new PDF page settings instance
let pageSettings: PdfPageSettings = new PdfPageSettings();
// Sets the margins
pageSettings.margins = new PdfMargins(40);
// Sets the page size
pageSettings.size = {width: 595, height: 842};
// Sets the page orientation
pageSettings.orientation = PdfPageOrientation.landscape;
// Create and insert a new PDF page at 5th index with specified page settings
page = document.addPage(5, pageSettings);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| index | number |
Page index. |
| pageSettings | PdfPageSettings |
Page settings. |
Returns PdfPage
addSection
Creates a new section with default page settings.
// Create a new PDF document
let document: PdfDocument = new PdfDocument();
// Add a new section to the document
let section: PdfSection = document.addSection();
// Add a new page to the section
let page: PdfPage = section.addPage();
// Gets the graphics of the PDF page
let graphics: PdfGraphics = page.graphics;
// Create a new pen.
let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1);
// Draw line on the page graphics.
graphics.drawLine(pen, {x: 10, y: 10}, {x: 100, y: 100});
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();Returns PdfSection
addSection
Creates a new section with custom page settings.
// Create an new PDF document
let document: PdfDocument = new PdfDocument();
// Create a new PDF page settings instance
let pageSettings: PdfPageSettings = new PdfPageSettings();
// Sets the margins
pageSettings.margins = new PdfMargins(40);
// Sets the page size
pageSettings.size = {width: 595, height: 842};
// Sets the page orientation
pageSettings.orientation = PdfPageOrientation.landscape;
// Add a new section to the document with page settings
let section: PdfSection = document.addSection(pageSettings);
// Add a new page to the section
let page: PdfPage = section.addPage();
// Gets the graphics of the PDF page
let graphics: PdfGraphics = page.graphics;
// Create a new pen.
let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1);
// Draw line on the page graphics.
graphics.drawLine(pen, {x: 10, y: 10}, {x: 100, y: 100});
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| settings | PdfPageSettings |
Settings of the section. |
Returns PdfSection
destroy
Disposes the current instance of PdfDocument class.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Destroy the document
document.destroy();Returns void
embedFont
Embed a standard font into the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Embed a font into the PDF document.
const embedFont = document.embedFont(PdfFontFamily.helvetica, 12, PdfFontStyle.regular);
// Draw string using embed font.
page.graphics.drawString('value', embedFont, {x: 10, y: 10, width: 100, height: 100}, new PdfBrush({r: 255, g: 0, b: 0}));
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| fontFamily | PdfFontFamily |
The font family. |
| size | number |
The font size. |
| style | PdfFontStyle |
The font style. |
Returns PdfStandardFont
embedFont
Embed a Cjk font into the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Embed a font into the PDF document with CJK embedding enabled.
const embedFont = document.embedFont(PdfCjkFontFamily.hanyangSystemsGothicMedium, 14, PdfFontStyle.bold, true);
// Draw string using embed font.
page.graphics.drawString('value', embedFont, {x: 10, y: 10, width: 100, height: 100});
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| fontFamily | PdfCjkFontFamily |
The Cjk font family. |
| size | number |
The font size. |
| style | PdfFontStyle |
The font style. |
| isCjk | true |
Set to true by default to embed the font as a CJK font. |
Returns PdfCjkStandardFont
embedFont
Embed a true type font into the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Embed a font into the PDF document.
let embedFont = document.embedFont(fontData, 14, { shouldUnderline: true });
// Draw string using embed font.
page.graphics.drawString('value', embedFont, {x: 10, y: 10, width: 100, height: 100});
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| fontData | Uint8Array |
The font data as byte array. |
| size | number |
The font size. |
| options (optional) | Object |
Optional object containing font style options. |
Returns PdfTrueTypeFont
embedFont
Embed a true type font into the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Embed a font into the PDF document.
let embedFont = document.embedFont(fontData, 14, { shouldStrikeout: true });
// Draw string using embed font.
page.graphics.drawString('value', embedFont, {x: 10, y: 10, width: 100, height: 100});
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| fontData | string |
The font data as base64 string. |
| size | number |
The font size. |
| options (optional) | Object |
Optional object containing font style options. |
Returns PdfTrueTypeFont
exportAnnotations
Exports the annotations from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Exports the annotations from the PDF document.
let data: Uint8Array = document.exportAnnotations();
// Destroy the document
document.destroy();Returns Uint8Array
exportAnnotations
Exports the annotations from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Sets export data format as JSON type to annotation export settings
let settings: PdfAnnotationExportSettings = new PdfAnnotationExportSettings();
settings.dataFormat = DataFormat.json;
// Export annotations to JSON format
let json: Uint8Array = document.exportAnnotations(settings);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| settings | PdfAnnotationExportSettings |
Annotation export settings. |
Returns Uint8Array
exportAnnotations
Exports the annotations from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Exports the annotations from the PDF document.
document.exportAnnotations('annotations.xfdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| filename | string |
Output file name. |
Returns void
exportAnnotations
Exports the annotations from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Sets export data format as JSON type to annotation export settings
let settings: PdfAnnotationExportSettings = new PdfAnnotationExportSettings();
settings.dataFormat = DataFormat.json;
// Export annotations to JSON format
let json: Uint8Array = document.exportAnnotations('annotations.json', settings);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| filename | string |
Output file name. |
| settings | PdfAnnotationExportSettings |
Annotation export settings. |
Returns void
exportFormData
Exports the form data from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Exports the form data from the PDF document.
let data: Uint8Array = document.exportFormData();
// Destroy the document
document.destroy();Returns Uint8Array
exportFormData
Exports the form data from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Sets the form field data export settings with output data format.
let settings: PdfFormFieldExportSettings = new PdfFormFieldExportSettings();
settings.dataFormat = DataFormat.json;
// Export form field to JSON format
let json: Uint8Array = document.exportFormData(settings);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| settings | PdfFormFieldExportSettings |
Form field export settings. |
Returns Uint8Array
exportFormData
Exports the form data from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Export form field to XFDF format
let xfdf: Uint8Array = document.exportFormData('formData.xfdf');
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| filename | string |
Output file name. |
Returns void
exportFormData
Exports the form data from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Sets the form field data export settings with output data format.
let settings: PdfFormFieldExportSettings = new PdfFormFieldExportSettings();
settings.dataFormat = DataFormat.json;
// Export form field to JSON format
let json: Uint8Array = document.exportFormData('formData.json', settings);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| filename | string |
Output file name. |
| settings | PdfFormFieldExportSettings |
Form field export settings. |
Returns void
getDocumentInformation
Gets the document information of the PDF.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Gets the document information of the PDF
let documentProperties: PdfDocumentInformation = document.getDocumentInformation();
// Gets the title of the PDF document
let title = documentProperties.title;
// Gets the author of the PDF document
let author = documentProperties.author;
// Gets the subject of the PDF document
let subject = documentProperties.subject;
// Gets the keywords of the PDF document
let keywords = documentProperties.keywords;
// Gets the creator of the PDF document
let creator = documentProperties.creator;
// Gets the producer of the PDF document
let producer = documentProperties.producer;
// Gets the language of the PDF document
let language = documentProperties.language;
// Gets the creation date of the PDF document
let creationDate = documentProperties.creationDate;
// Gets the modification date of the PDF document
let modificationDate = documentProperties.modificationDate;
document.destroy();Returns PdfDocumentInformation
getPage
Gets the PdfPage at the specified index.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access first page
let page: PdfPage = document.getPage(0);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| pageIndex | number |
Page index. |
Returns PdfPage
getRevisions
Gets an array of revision numbers for the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Retrieve all revision indexes of the PDF document
let revisions: number[] = document.getRevisions();
// Destroy the document
document.destroy();Returns number[]
importAnnotations
Imports the annotations from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Imports annotations from to the PDF document.
document.importAnnotations('annotations.json', DataFormat.json);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| data | string |
annotations data as base64 string. |
| dataFormat | DataFormat |
Data format of the input data. |
Returns void
importAnnotations
Imports the annotations from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Imports annotations from to the PDF document.
document.importAnnotations(annotations, DataFormat.json);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| data | Uint8Array |
annotations data as byte array. |
| dataFormat | DataFormat |
Data format of the input data. |
Returns void
importFormData
Imports the form data from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Imports form data from to the PDF document.
document.importFormData('formData.json', DataFormat.json);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| data | string |
Form data as base64 string. |
| dataFormat | DataFormat |
Data format of the input data. |
Returns void
importFormData
Imports the form data from the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Imports form data from to the PDF document.
document.importFormData(data, DataFormat.json);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| data | Uint8Array |
Form data as byte array. |
| dataFormat | DataFormat |
Data format of the input data. |
Returns void
importPage
Copy the specific page and insert it as the next page
// Load an existing PDF document
let sourceDocument: PdfDocument = new PdfDocument(data1);
// Copy the second page and add it as third page
sourceDocument.importPage(1);
// Save the output PDF
sourceDocument.save('Output.pdf');
// Destroy the documents
sourceDocument.destroy();Returns void
importPage
Copy the specific page and insert it at the specified target page index and page rotation.
// Load an existing PDF document
let sourceDocument: PdfDocument = new PdfDocument(readFromResources('PDF_Succinctly.pdf'));
// Options to customize the support of import PDF pages.
let options: PdfPageImportOptions = new PdfPageImportOptions();
// Sets the target page index to import
options.targetIndex = 1;
// Sets the rotation angle of the page to import
options.rotation = PdfRotationAngle.angle180;
// Copy the first page and add it as second page with page rotation
sourceDocument.importPage(0, options);
// Save the output PDF
let output = sourceDocument.save();
write('863764-86.pdf', output);
// Destroy the documents
sourceDocument.destroy();Returns void
importPage
Import the specified page into the current document pages collection as the last page
// Load an existing PDF document
let destination: PdfDocument = new PdfDocument(data1);
// Load another existing PDF document
let sourceDocument: PdfDocument = new PdfDocument(data2);
// Access first page of the source document
let pageToImport: PdfPage = sourceDocument.getPage(0);
// Import the page into the destination document as the last page.
destination.importPage(pageToImport, sourceDocument);
// Save the output PDF
destination.save('Output.pdf');
// Destroy the documents
destination.destroy();
sourceDocument.destroy();| Parameter | Type | Description |
|---|---|---|
| page | PdfPage |
Page to import. |
| sourceDocument | PdfDocument |
PDF document to get pages to import. |
Returns void
importPage
Create a new page with default settings and insert it into the collection at the specified page index.
// Load an existing PDF document
let destination: PdfDocument = new PdfDocument(data1);
// Load another existing PDF document
let sourceDocument: PdfDocument = new PdfDocument(data2);
// Access first page of the source document
let pageToImport: PdfPage = sourceDocument.getPage(0);
// Options to customize the support of import PDF pages.
let options: PdfPageImportOptions = new PdfPageImportOptions();
// Sets the target page index to import
options.targetIndex = 5;
// Imports the page into destination document as 5th page
destination.importPage(pageToImport, sourceDocument, options);
// Save the output PDF
destination.save('Output.pdf');
// Destroy the documents
destination.destroy();
sourceDocument.destroy();Returns void
importPageRange
Import the pages specified by the start and end index into the current document’s pages collection.
// Load an existing PDF document
let destination: PdfDocument = new PdfDocument(data1);
// Load another existing PDF document
let sourceDocument: PdfDocument = new PdfDocument(data2);
// Import 5 pages from page index 2 to 6 into the destination document.
destination.importPageRange(sourceDocument, 2, 6);
// Save the output PDF
destination.save('Output.pdf');
// Destroy the documents
destination.destroy();
sourceDocument.destroy();| Parameter | Type | Description |
|---|---|---|
| sourceDocument | PdfDocument |
PDF document to get pages to import. |
| startIndex | number |
Start page index. The default value is 0. |
| endIndex | number |
End page index. The default value is the index of the last page in the source document. |
Returns void
importPageRange
Import the pages specified by start and end index into the current document’s pages collection.
// Load an existing PDF document
let destination: PdfDocument = new PdfDocument(data1);
// Load another existing PDF document
let sourceDocument: PdfDocument = new PdfDocument(data2);
// Options to customize the support of import PDF pages.
let options: PdfPageImportOptions = new PdfPageImportOptions();
// Sets the target page index to import
options.targetIndex = 3;
// Import 5 pages from page index 2 to 6 into the destination document and insert them at index 3.
destination.importPageRange(sourceDocument, 2, 6, options);
// Save the output PDF
destination.save('Output.pdf');
// Destroy the documents
destination.destroy();
sourceDocument.destroy();| Parameter | Type | Description |
|---|---|---|
| sourceDocument | PdfDocument |
PDF document to get pages to import. |
| startIndex | number |
Start page index. The default value is 0. |
| endIndex | number |
End page index. The default value is the index of the last page in the source document. |
| options (optional) | PdfPageImportOptions |
Options to customize the support of import PDF pages. |
Returns void
removePage
Removes the specified page.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Removes the specified page
document.removePage(page);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| page | PdfPage |
The page to remove. |
Returns void
removePage
Removes the page from the specified index.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Removes the first page
document.removePage(0);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| index | number |
The page index to remove. |
Returns void
reorderPages
Reorders the pages in the PDF document.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Reorders the pages in the PDF document
document.reorderPages([3, 2, 1]);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| orderArray | number[] |
The page sequence to arrange the pages. |
Returns void
save
Saves the modified document synchronously.
Returns Uint8Array
save
Saves the modified document to the specified filename.
| Parameter | Type | Description |
|---|---|---|
| filename | string |
Specifies the filename to save the output pdf document. |
Returns void
saveAsBlob
Saves the document to the specified output stream and return the stream as Blob.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
// Save the document
let data: Promise<{ blobData: Blob }> = document.saveAsBlob();
// Destroy the document
document.destroy();Returns Promise
saveAsync
Saves the PDF document asynchronously and returns the resulting bytes.
Returns Promise
saveAsync
Saves the PDF document asynchronously to the specified filename.
| Parameter | Type | Description |
|---|---|---|
| filename | string |
The target filename to write the output PDF. |
Returns Promise
setDocumentInformation
Sets the document information of the PDF.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Sets the document information of the PDF
document.setDocumentInformation({
author: 'Syncfusion',
modificationDate: Date.now(),
creator: 'Essential PDF',
keywords: 'PDF',
subject: 'Document information DEMO',
title: 'Essential PDF Sample',
producer: 'Syncfusion PDF'
});
// Save the document
document.save('output.pdf);
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| information | PdfDocumentInformation |
Fields to set. |
Returns void
split
Splitting a PDF file into individual pages.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
document.splitEvent = documentSplitEvent;
// Split PDF document into individual pages
document.split();
// Event to invoke while splitting PDF document data
function documentSplitEvent(sender: PdfDocument, args: PdfDocumentSplitEventArgs): void {
Save.save('output_' + args.splitIndex + '.pdf', new Blob([args.pdfData], { type: 'application/pdf' }));
}
// Destroy the document
document.destroy();Returns void
splitByFixedNumber
Splits the PDF document into parts, each containing a maximum number of pages specified.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
document.splitEvent = documentSplitEvent;
// Split PDF document by fixed number of pages
document.splitByFixedNumber(1);
// Event to invoke while splitting PDF document data
function documentSplitEvent(sender: PdfDocument, args: PdfDocumentSplitEventArgs): void {
Save.save('output_' + args.splitIndex + '.pdf', new Blob([args.pdfData], { type: 'application/pdf' }));
}
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| fixedNumber | number |
specifies the maximum number of pages in each split PDF. The default value is 1. |
Returns void
splitByPageRanges
Splits the PDF document into multiple parts based on the specified page ranges.
// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data);
document.splitEvent = documentSplitEvent;
// Split PDF document by page ranges specified
document.splitByPageRanges([[0, 4], [5, 9]]);
// Event to invoke while splitting PDF document data
function documentSplitEvent(sender: PdfDocument, args: PdfDocumentSplitEventArgs): void {
Save.save('output_' + args.splitIndex + '.pdf', new Blob([args.pdfData], { type: 'application/pdf' }));
}
// Destroy the document
document.destroy();| Parameter | Type | Description |
|---|---|---|
| ranges | number[][] |
The two dimensional number array specified for start and end page indexes to split PDF documents. |
Returns void