How to Edit Existing PDFs in .NET with Xceed PDF Library

Learn to edit existing PDFs with Xceed PDF Library for .NET 3.0. Discover how to modify content efficiently without rebuilding.

Editing an existing PDF programmatically does not necessarily require rebuilding the entire document. Xceed PDF Library for .NET lets .NET developers load existing PDF files, find and replace text, modify supported document elements, add images and form fields, and save the resulting document.

Developers frequently need to update PDFs that already exist, whether they are modifying generated contracts, reports, invoices, templates, or other application documents.

Xceed PDF Library for .NET provides APIs for creating, loading, modifying, extracting, signing, securing, and saving PDF documents directly from .NET applications without requiring an external PDF application.

Why Xceed PDF Library for .NET?

Xceed PDF Library for .NET provides APIs for working with both new and existing PDF documents. Developers can create PDFs from scratch, add content such as text, images, shapes, form fields, hyperlinks, attachments, and watermarks, and work with supported elements in existing documents.

For existing PDFs, the library supports operations including text replacement, text removal, modification of images, page-level changes, and extraction of document elements.

The right PDF library ultimately depends on your application's technical requirements, deployment environment, licensing requirements, and the specific PDF operations you need to perform.

Installing Xceed PDF Library for .NET

Install the library from NuGet using the correct package ID:

Package Manager

Install-Package Xceed.PdfLibrary.NET

Or with the .NET CLI:

.NET CLI

dotnet add package Xceed.PdfLibrary.NET

The official NuGet package is Xceed.PdfLibrary.NET. Then import the library namespace:

Import the Namespace

using Xceed.PdfLibrary.NET;

Because DocumentPdf belongs to the Xceed.PdfLibrary.NET namespace, this namespace must either be imported or the class must be fully qualified.

If you install the library through NuGet, configure your Xceed license key before creating or loading PDF documents:

Configure the License Key

using Xceed.PdfLibrary.NET;

Licenser.LicenseKey = "YOUR-LICENSE-KEY";

Xceed currently provides trial and purchased license keys for the library.

Loading and Modifying an Existing PDF

An existing PDF can be opened with PdfDocument.Charger. For example:

Load, Modify, and Save a PDF

using Xceed.PdfLibrary.NET;

using (var pdf = PdfDocument.Load("example.pdf"))
{
    // Modify the document here.

    pdf.SaveAs("example-updated.pdf");
}

DocumentPdf exposes documented Chargement, Économisezet Sauvegarde methods for opening and persisting PDF documents.

Finding and Replacing Text

For text replacement, do not attempt to modify individual objects through a pattern such as:

Avoid This Pattern

foreach (var word in page.Words)
{
    word.Text = "replacement";
}

The documented approach is to locate editable text using FindText and then pass the resulting matches to ReplaceText.

Find and Replace Text

using Xceed.PdfLibrary.NET;
using System.Linq;

using (var pdf = PdfDocument.Load("example.pdf"))
{
    var page = pdf.Pages.First();

    var searchOptions = new TextSearchOptions("placeholder");
    var matches = page.FindText(searchOptions);

    page.ReplaceText(matches, "replaced text");

    pdf.SaveAs("example-updated.pdf");
}

Xceed's current API also allows you to provide a replacement StyleDeTexte et TextEditOptions when more control over formatting and layout behavior is required.

Embedded font consideration: When replacement text relies on an embedded font from the original PDF, the characters available in that font can affect whether the replacement succeeds. Xceed documents additional options, including a fallback font, through TextEditOptions.

Adding Images

Images can be added to a PDF using ImageSource. The documented API uses ImageSource.FromPath, not Image.FromPath.

Add an Image to a PDF

using Xceed.PdfLibrary.NET;
using System.Linq;

using (var pdf = PdfDocument.Load("example.pdf"))
{
    var page = pdf.Pages.First();

    var imageSource = ImageSource.FromPath("image.png");

    page.Graphics.DrawImage(
        imageSource,
        new Rectangle(50, 50, 200, 150)
    );

    pdf.SaveAs("example-with-image.pdf");
}

ImageSource.FromPath creates an image source from a file path, and Graphics.DrawImage can position and size the image on the PDF page.

Images can also be created from streams using ImageSource.FromStream, which can be useful when working with uploaded files, database content, web applications, or dynamically generated image data.

Existing images can also be retrieved from a page and modified. For example, Xceed documents changing an image's Bounds et RotationAngle before saving the updated PDF.

Adding Form Fields

Xceed PDF Library for .NET supports several PDF form-field types, including text boxes, number fields, date/time fields, checkboxes, combo boxes, list boxes, radio buttons, and signature fields.

Important: The correct text field class is Champ de saisie de texte, rather than TextBox.

Add PDF Form Fields

using Xceed.PdfLibrary.NET;
using System.Linq;

using (var pdf = PdfDocument.Load("example.pdf"))
{
    var page = pdf.Pages.First();

    var textField = new TextBoxFormField(
        "Default",
        new Rectangle(125, 92, 120, 22)
    );

    page.FormFields.Add(textField);

    var checkBox = new CheckBoxFormField(
        true,
        new Rectangle(125, 130, 20, 20)
    );

    page.FormFields.Add(checkBox);

    pdf.SaveAs("example-with-form-fields.pdf");
}

The official Xceed examples add form fields through the page's ChampsDeFormulaire collection and use classes such as Champ de saisie de texte et ChampFormulaireCaseÀCocher.

Form fields can also be styled with borders, brushes, fonts, fill colors, and other field-specific properties where supported.

Modifying Existing Images

When working with a PDF that already contains images, you can retrieve those images from the page and modify supported properties.

Resize and Rotate an Existing Image

using Xceed.PdfLibrary.NET;
using System.Linq;

using (var pdf = PdfDocument.Load("documentWithImage.pdf"))
{
    var page = pdf.Pages.First();

    var image = page.Images[0];

    image.Bounds = new Rectangle(100, 150, 400, 400);
    image.RotationAngle = 45d;

    pdf.SaveAs("modified-image.pdf");
}

This pattern is documented by Xceed for changing the position, size, and rotation of an existing image.

Best Practices and Performance Considerations

When editing PDFs programmatically, keep the document lifecycle explicit. Load or create the document, perform the required operations, save the result, and dispose of the document when finished.

Using a en utilisant statement is a convenient pattern:

Recommended Document Lifecycle

using (var pdf = PdfDocument.Load("input.pdf"))
{
    // Perform modifications.

    pdf.SaveAs("output.pdf");
}

When working with important production documents, saving modifications to a separate output file can also make testing and rollback easier.

For larger or more complex documents, avoid performing unnecessary searches or transformations across pages you do not need to modify. Scope operations to the relevant pages or document elements whenever possible.

  • Keep the lifecycle explicit: Load, modify, save, and dispose of each PDF document.
  • Protect source files: Save important production changes to a separate output file when appropriate.
  • Limit searches: Search only the pages that actually need text replacement or extraction.
  • Use documented editing APIs: Prefer operations such as FindText et ReplaceText rather than assuming extracted objects are arbitrarily mutable.
  • Consider fonts: Account for embedded-font character availability when replacing text.

Licensing and Trial Availability

Xceed PDF Library for .NET is a commercial library that uses license keys.

Xceed currently advertises a free 45-day trial for PDF Library for .NET. A trial license key must be configured in the application before using the library's document APIs. Trial duration, licensing options, pricing, and terms may change, so developers should verify the current product and license information before deployment or purchase.

Conclusion

Xceed PDF Library for .NET provides .NET developers with APIs for creating and working with PDF documents programmatically.

For existing PDFs, the important distinction is to use the library's documented editing APIs rather than treating extracted PDF elements as arbitrary mutable objects.

  • Utiliser PdfDocument.Charger to open an existing PDF.
  • Utiliser FindText et ReplaceText for supported text replacement.
  • Utiliser ImageSource.FromPath et Graphics.DrawImage to add images.
  • Use classes such as Champ de saisie de texte et ChampFormulaireCaseÀCocher for form fields.
  • Utiliser Économisez ou Sauvegarde to persist the modified document.

These APIs are part of the current Xceed.PdfLibrary.NET namespace and package.

Get Started with Xceed PDF Library

Developers can evaluate Xceed PDF Library for .NET using Xceed's currently advertised 45-day trial or install the Xceed.PdfLibrary.NET package directly from NuGet.

Start evaluating Xceed PDF Library for .NET with a 45-day trial, or visit Xceed's NuGet profile to install the package and explore available .NET libraries.

Foire aux questions

How do I install Xceed PDF Library for .NET?

Install the official NuGet package with the .NET CLI:

dotnet add package Xceed.PdfLibrary.NET

Or with Package Manager:

Install-Package Xceed.PdfLibrary.NET

Then import:

using Xceed.PdfLibrary.NET;

The package and namespace are both currently documented by Xceed.

Can Xceed PDF Library replace text in an existing PDF?

Yes. The current API provides FindText for locating editable text and ReplaceText for replacing the resulting matches.

Can I customize added elements?

Yes. The available customization depends on the element. The library exposes APIs for text styles, brushes, fonts, image bounds and rotation, form-field borders and fill colors, and other element-specific settings.

Does Xceed PDF Library support PDF security features?

Yes. Xceed's current documentation includes PDF signing, certification, and encryption APIs. Encryption options include user and owner passwords as well as configurable permissions.

Can Xceed PDF Library add form fields?

Yes. Supported form-field types documented by Xceed include text boxes, number fields, date/time fields, checkboxes, combo boxes, list boxes, radio buttons, and signature fields.

Does the library require Adobe Acrobat?

No external PDF editing application is required for Xceed PDF Library for .NET to create and manipulate PDF documents programmatically.

Découvrez la bibliothèque de mots et PDF de Xceed paquet