Edit PDFs with
Powerful Precision
Create, modify, and manipulate PDF documents programmatically. Add text, images, watermarks, merge documents, and fill forms with ease.
Code Examples
Learn how to manipulate PDF documents with simple, clean code
Create New PDF Document
Create a new PDF document from scratch and add text, images, and graphics with full control.
PdfDocument pdfDocument = new PdfDocument();
var page = pdfDocument.AddPage();
page.Size = PdfEdit.PageSize.A4;
XGraphics gfx = XGraphics.FromPdfPage(page);
// Draw text
gfx.DrawString("Hello, PDF World!",
new XFont("Arial", 10),
XBrushes.Blue,
new PointF(100, 100));
// Draw image
XImage image = XImage.FromFile(@"c:\images\logo.jpg");
gfx.DrawImage(image, new Point(300, 300));
gfx.Dispose();
pdfDocument.SetLicenseInfo("companyName", "licenseKey");
pdfDocument.Save(@"c:\test\output.pdf");
Add Pages to Existing PDF
Open an existing PDF document and append additional blank pages for further content.
PdfDocument pdfDocument = PdfReader.Open(
@"c:\test\test.pdf",
PdfDocumentOpenMode.Modify);
// Add new blank pages
var newPage = pdfDocument.AddPage();
var newPage2 = pdfDocument.AddPage();
// Configure page size if needed
newPage.Size = PdfEdit.PageSize.Letter;
pdfDocument.SetLicenseInfo("companyName", "licenseKey");
pdfDocument.Save(@"c:\test\test.pdf");
Edit Existing PDF
Open an existing document and add watermarks, text, or images to every page.
PdfDocument pdfDocument = PdfReader.Open(
@"c:\test\test.pdf",
PdfDocumentOpenMode.Modify);
// Add watermark to every page
foreach(var page in pdfDocument.Pages)
{
XGraphics gfx = XGraphics.FromPdfPage(page);
// Draw text watermark
gfx.DrawString("CONFIDENTIAL",
new XFont("Arial", 10),
XBrushes.Red,
new PointF(50, 50));
// Add logo image
XImage image = XImage.FromFile(@"c:\images\logo.bmp");
gfx.DrawImage(image, new Point(150, 150));
gfx.Dispose();
}
pdfDocument.SetLicenseInfo("companyName", "licenseKey");
pdfDocument.Save(@"c:\test\test.pdf");
Remove Pages from PDF
Open a PDF document and remove specific pages by their index.
PdfDocument pdfDocument = PdfReader.Open(
@"c:\test\test.pdf",
PdfDocumentOpenMode.Modify);
// Check if document has enough pages
if (pdfDocument.Pages.Count > 1)
{
// Remove second page (0-indexed)
pdfDocument.Pages.RemoveAt(1);
}
// Remove multiple pages (from end to avoid index shift)
for (int i = pdfDocument.Pages.Count - 1; i >= 5; i--)
{
pdfDocument.Pages.RemoveAt(i);
}
pdfDocument.SetLicenseInfo("companyName", "licenseKey");
pdfDocument.Save(@"c:\test\test.pdf");
Extract Pages from PDF
Create a new document from selected pages of an existing PDF without modifying the original.
// Open in Import mode (read-only)
PdfDocument pdfDocument = PdfReader.Open(
@"c:\test\test.pdf",
PdfDocumentOpenMode.Import);
PdfDocument newPdfDocument = new PdfDocument();
// Extract every second page
for (int i = 0; i < pdfDocument.Pages.Count; i++)
{
if ((i + 1) % 2 == 0)
newPdfDocument.AddPage(pdfDocument.Pages[i]);
}
newPdfDocument.SetLicenseInfo("companyName", "licenseKey");
newPdfDocument.Save(@"c:\test\extracted.pdf");
Protect PDF & Set Permissions
Encrypt your PDF with AES-256 and configure granular user access permissions.
PdfDocument pdfDocument = PdfReader.Open(
@"c:\test\test.pdf",
PdfDocumentOpenMode.Modify);
// Set encryption algorithm
pdfDocument.SecuritySettings.PdfDocumentEncryptionAlgorithm =
new EncryptionAlgorithmInfo();
pdfDocument.SecuritySettings.PdfDocumentEncryptionAlgorithm.Type =
PdfDocumentSecurityLevel.AES_256;
// Set passwords
pdfDocument.SecuritySettings.OwnerPassword = "ownerPass";
pdfDocument.SecuritySettings.UserPassword = "userPass";
// Configure permissions
pdfDocument.SecuritySettings.PermitPrint = true;
pdfDocument.SecuritySettings.PermitFullQualityPrint = false;
pdfDocument.SecuritySettings.PermitModifyDocument = true;
pdfDocument.SecuritySettings.PermitFormsFill = false;
pdfDocument.SecuritySettings.PermitExtractContent = true;
pdfDocument.SetLicenseInfo("companyName", "licenseKey");
pdfDocument.Save(@"c:\test\protected.pdf");
Fill in a PDF Form
Programmatically fill text fields in PDF forms and save to a new document.
PdfDocument pdfDocument = PdfReader.Open(
@"c:\test\form.pdf",
PdfDocumentOpenMode.Modify);
// Get field by name
var textField = pdfDocument.AcroForm.Fields["FieldSubjectType"]
as PdfTextField;
// Check if field is editable
if (!textField.ReadOnly)
{
textField.Text = "Top Secret";
}
// Fill multiple fields
var nameField = pdfDocument.AcroForm.Fields["Name"] as PdfTextField;
nameField.Text = "John Doe";
var dateField = pdfDocument.AcroForm.Fields["Date"] as PdfTextField;
dateField.Text = DateTime.Now.ToShortDateString();
pdfDocument.SetLicenseInfo("companyName", "licenseKey");
pdfDocument.Save(@"c:\test\filled-form.pdf");
Create a new PDF document from scratch.
PdfDocument pdf = new PdfDocument();
var page = pdf.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString("Hello!", new XFont("Arial", 10),
XBrushes.Blue, new PointF(100, 100));
pdf.Save(@"c:\output.pdf");
Append new pages to an existing document.
PdfDocument pdf = PdfReader.Open("test.pdf",
PdfDocumentOpenMode.Modify);
pdf.AddPage();
pdf.Save("test.pdf");
Add watermarks or content to existing pages.
foreach(var page in pdfDocument.Pages)
{
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString("DRAFT", font, XBrushes.Red, pos);
}
Delete specific pages from a document.
if (pdfDocument.Pages.Count > 1)
pdfDocument.Pages.RemoveAt(1);
Create a new document from selected pages.
PdfDocument newPdf = new PdfDocument();
newPdf.AddPage(sourcePdf.Pages[0]);
newPdf.Save("extracted.pdf");
Encrypt and set access permissions.
pdf.SecuritySettings.OwnerPassword = "owner";
pdf.SecuritySettings.UserPassword = "user";
pdf.SecuritySettings.PermitPrint = true;
Fill form fields programmatically.
var field = pdf.AcroForm.Fields["Name"] as PdfTextField;
field.Text = "John Doe";
Ready to Edit PDFs?
Download the free demo and start creating and editing PDF documents in your .NET applications today.
Download Free Demo