Getting Started with PDFEdit


Requirements: Secure PDF

Introduction

The PDFEdit component in Secure PDF provides high-level PDF text and document editing functionality that, alongside the PDFForm component, can be used to prepare the content of your documents before signing and/or encrypting them.

This guide will focus on how to use this component to edit PDFs at both the page content level and document level. Before continuing, it is recommended to download the latest version of Secure PDF to follow along with this guide.

Overview

The PDFEdit component can be used to automate search and replace operations, text extraction, content composition, and document merging and splitting.

To begin, provide the input document as a file (InputFile), byte array (InputData), or stream (SetInputStream) and call the Open method. When finished editing the document, call the Close method to close it and save the changes to either OutputFile, OutputData, or the stream set in SetOutputStream.

Search, Replace, and Extract Text

Use the ReplacePageText and ReplaceDocumentText methods to iterate through the page or document content and replace all the text elements that match the specified pattern with the provided text. The component will fire the ReplaceText event during this operation, providing the flexibility of skipping certain text blocks or updating the replacement string on the fly. For example:

pdfedit.InputFile = "invoice.pdf"; pdfedit.OutputFile = "invoice_filled.pdf";
pdfedit.OnReplaceText += (s, e) => { if (e.Substitute == "/n software") { e.Substitute = "my company"; } };
pdfedit.Open(); int occurrences = pdfedit.ReplacePageText(0, "SELLER_NAME", "/n software"); pdfedit.Close();

Additionally, the GetPageText and GetDocumentText methods let you extract all the text residing on a specific page or in the entire document.

Content Composition

With PDFEdit, you can also create documents from scratch or add text or images to existing documents using the editing API. This consists of the following four methods:

  • CreateNew creates a blank PDF document.
  • AddTextBlock adds a block of text.
  • AddBitmap adds an image (JPEG, BMP, or PNG).
  • AddDrawing adds a vector drawing.

The above methods are complemented by a set of formatting methods that let you choose the parameters of the added text or image (where, what color, how big, or how skewed):

  • SetPosition sets the page to work with and places the cursor at a specific position on the page.
  • SetFont sets the font to use, including its size, style, and color.
  • SetAlignment sets the block alignment (left/center/right, top/center/bottom).
  • SetTransform sets the transformation matrix, which alters positioning, scaling, rotation angle, and skew.

Document Merging and Splitting

Document-level operations can be performed with the AppendPage, InsertPage, RemovePage, and RemovePages methods.

To copy pages from one document to another, open each document in an individual PDFEdit object. Then, using the SelectPage method, select the page of document 1 that you want to add to document 2, and assign the PDFPage object populated in its SelectedPage property to the NewPage property of the recipient document. Finally, commit the page to the recipient document using the AppendPage or InsertPage method.

dest.InputFile = "recipient.pdf"; dest.Open();
src.InputFile = "donor.pdf"; src.Open();
src.SelectPage(0); dest.NewPage = src.SelectedPage; dest.AppendPage();
dest.Close(); src.Close();

To split the document in two, use the RemovePages method to remove the respective subsets of pages from the document, then save each remaining part. For example:

pdfedit.InputFile = "input_4_pages.pdf"; pdfedit.Open();
// Remove the pages after page 1, and save the first part (pages 0-1) pdfedit.OutputFile = "input_first.pdf"; pdfedit.RemovePages(2, 2); pdfedit.Close();
// Remove the pages up to and including page 1, and save the second part (pages 2-3) pdfedit.OutputFile = "input_second.pdf"; pdfedit.Open(); pdfedit.RemovePages(0, 2); pdfedit.Close();

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.