Working with the PdfContentEditor class

Implementation details

PdfContentEditor class allows you to manipulate variety of contents. It helps you create different annotations; twenty four annotation types are supported in Aspose.Pdf.Facades namespace. We’ll show you a simple example to help you understand how the variety of annotations can be added in a PDF file.

 // Create PdfContentEditor class

PdfContentEditor editor = new PdfContentEditor();

// Create rectangle object

System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 20, 20);

// Bind input PDF file

editor.BindPdf("input.pdf");

// Create any annotation - in this case Text Annotation

editor.CreateText(rectangle, "Welcome to Aspose", "You are welcome to Aspose!", true, "Key", 2);

// Save output

editor.Save("output.pdf");

Working with Viewer preferences

It also allows you to change and get viewer preferences as shown in the code snippet given below:

 // Create PdfContentEditor object

PdfContentEditor editor = new PdfContentEditor();

// Bind input PDF file

editor.BindPdf("input.pdf");

// Change Viewer Preferences

editor.ChangeViewerPreference(ViewerPreference.HideMenubar);

// Get viewer preference

int preferenceValue = editor.GetViewerPreference();

// Save output PDF file

editor.Save("output.pdf");

Working with JavaScript

You can also set the document level JavaScript as shown below.

 // Create PdfContentEditor object to add additional document actions

PdfContentEditor editor = new PdfContentEditor();

// Open input PDF file

editor.BindPdf("input.pdf");

// Set script for additional action

editor.AddDocumentAdditionalAction(PdfContentEditor.DocumentOpen, "app.alert('Welcome to Aspose.');");

// Save output PDF file

editor.Save("output.pdf");

Replace text and Images inside PDF

This class also helps you replace text and images.

 // Create PdfContentEditor object

PdfContentEditor editor = new PdfContentEditor();

// Bind input PDF file

editor.BindPdf("input.pdf");

// Replace text

editor.ReplaceText("source string", "destination string");

// Replace image

editor.ReplaceImage(1, 1, "test.jpg");

// Save output PDF file

editor.Save("output.pdf");

Deleting images

You can also delete either all the images or the images on particular index at a particular page.

 // Create PdfContentEditor object

PdfContentEditor editor = new PdfContentEditor();

// Bind input PDF file

editor.BindPdf("input.pdf");

// Delete particular images

editor.DeleteImage(1, new int[] { 1, 4 });

// or - delete all images

editor.DeleteImage();

// Save output PDF file

editor.Save("output.pdf");

Adding attachments

Document attachments can also be added in a PDF file as shown in the following code snippet.

 // Create PdfContentEditor object

PdfContentEditor editor = new PdfContentEditor();

// Bind input PDF file

editor.BindPdf("input.pdf");

// Add document attachment

editor.AddDocumentAttachment("sample.ppt", "Adding Powerpoint Presentation as attachment");

// Save output PDF file

editor.Save("output.pdf");