Document Content Modification
- 5 minutes to read
A PDF document contains content customization API that allows you to do the following:
- Add, delete, and rearrange Pages.
- Append a separate document and thus merge two PDF files into one.
- Manage file FileAttachments.
Every document change raises the OnChanged event. Enclose multiple structure changes between BeginUpdate and EndUpdate calls to avoid excessive change notifications.
Append Documents
You can merge multiple documents into one. Call a PDF document container’s Append procedure to append all pages, attachments, and bookmarks from a specified document to the current document.
The following code example invokes the Open dialog to load a PDF document and append it to the “Demo.pdf” document file:
var
ADocument: TdxPDFDocument;
ASource: TdxPDFDocument;
begin
if OpenDialog.Execute then
begin
ADocument := TdxPDFDocument.Create;
ASource := TdxPDFDocument.Create;
try
ADocument.LoadFromFile('Data\Demo.pdf');
ASource.LoadFromFile(OpenDialog.FileName);
ADocument.Append(ASource);
ADocument.SaveToFile('Data\Result.pdf');
finally
ADocument.Free;
ASource.Free;
end;
end;
end;
Manage Pages
Add Pages
Call the Pages.Add or Pages.Insert procedure to append or insert pages from a source document into the current document. These operations ignore attachments and bookmarks in the source document. If you need to merge multiple documents including attachments and bookmarks, call the Append procedure instead.
The following code example inserts the first two pages from a source document after the first page in the “Demo.pdf” document file:
var
ADocument: TdxPDFDocument;
ASource: TdxPDFDocument;
begin
ADocument := TdxPDFDocument.Create;
ASource := TdxPDFDocument.Create;
try
ADocument.LoadFromFile('Data\Demo.pdf');
ASource.LoadFromFile('Data\Source.pdf');
ADocument.BeginUpdate;
try
// Inserts the first page (0) from the source document as the second page (1) in the current document
ADocument.Pages.Insert(1, ASource, 0);
// Inserts the second page (1) from the source document as the third page (2) in the current document
ADocument.Pages.Insert(2, ASource, 1);
finally
ADocument.EndUpdate;
end;
ADocument.SaveToFile('Data\Result.pdf');
finally
ADocument.Free;
ASource.Free;
end;
Rearrange Pages
Call the Pages.Move procedure to move pages within the same document.
The following code example swaps the first and second pages in the Demo.pdf document:
var
ADocument: TdxPDFDocument;
begin
ADocument := TdxPDFDocument.Create;
try
ADocument.LoadFromFile('Data\Demo.pdf');
ADocument.Pages.Move(0, 1);
ADocument.SaveToFile('Data\Result.pdf');
finally
ADocument.Free;
end;
Delete Pages
Call the Pages.Delete procedure to delete a page.
The following code example loads a PDF document from the Demo.pdf file, deletes the first two pages of the loaded document, and saves the resulting document to a different file (Result.pdf):
uses
dxPDFDocument; // This unit declares the TdxPDFDocument class
// ...
var
ADocument: TdxPDFDocument;
begin
ADocument := TdxPDFDocument.Create;
try
ADocument.LoadFromFile('Data\Demo.pdf');
ADocument.BeginUpdate; // Initiates the following batch change
try
ADocument.Pages.Delete(0);
ADocument.Pages.Delete(0);
finally
ADocument.EndUpdate; // Calls EndUpdate regardless of the batch operation's success
end;
ADocument.SaveToFile('Data\Result.pdf');
finally
ADocument.Free; // Releases the created document container to avoid memory leaks
end;
Batch Operations
Call the following overloaded procedures available in a PDF document container and pass an array of page indexes as a parameter to perform batch page management operations:
Manage File Attachments
Add Attachments
Call the FileAttachments.Add function to create a new empty attachment. Then, call the attachment’s LoadFromFile or LoadFromStream procedure to load content.
The following code example invokes the Open dialog to load a file and attach it to the “Demo.pdf” document:
var
ADocument: TdxPDFDocument;
AAttachment: TdxPDFFileAttachment;
begin
if OpenDialog.Execute then
begin
ADocument := TdxPDFDocument.Create;
try
ADocument.LoadFromFile('Data\Demo.pdf');
ADocument.BeginUpdate;
try
AAttachment := ADocument.FileAttachments.Add;
AAttachment.LoadFromFile(OpenDialog.FileName);
finally
ADocument.EndUpdate;
end;
ADocument.SaveToFile('Data\Result.pdf');
finally
ADocument.Free;
end;
end;
end;
Remove Attachments
Call the FileAttachments.Remove function to remove a file attachment.
The following procedure loads the “Demo.pdf” file and removes file attachments whose names match the parameter value.
procedure TfrmPDFFileAttachment.Remove(const AFileName: string);
var
ADocument: TdxPDFDocument;
AAttachment: TdxPDFFileAttachment;
begin
ADocument := TdxPDFDocument.Create;
try
ADocument.LoadFromFile('Data\Demo.pdf');
for AAttachment in ADocument.FileAttachments do
if SameText(AAttachment.FileName, AFileName) then
ADocument.FileAttachments.Remove(AAttachment)
ADocument.SaveToFile('Data\Result.pdf');
finally
ADocument.Free;
end;
end;