Edit content of PDF page in WinForms / WPF viewer

Blog category: PDF.NET

January 20, 2022

VintaSoft Imaging .NET SDK has extensive features for editing of PDF document in WinForms/WPF viewer:


The abilities of visual editing of PDF document are demonstrated in demo applications VintaSoft PDF Editor Demo (WinForms) and VintaSoft WPF PDF Editor Demo (WPF).

This article provides an overview of functionality for visual editing of PDF document content in WinForms/WPF viewer.



Edit content of PDF page in WinForms/WPF viewer


WinForms control ImageViewer allows to display pages of PDF document in WinForms application. ImageViewer control can use visual tool PdfContentEditorTool for editing of PDF page content in viewer.
WPF control WpfImageViewer allows to display pages of PDF document in WPF application. WpfImageViewer control can use visual tool WpfPdfContentEditorTool for editing of PDF page content in viewer.

Here is the code that creates ImageViewer with PdfContentEditorTool for displaying and editing of PDF page content in WinForms application:
// The project, which uses this code, must have references to the following assemblies:
// - Vintasoft.Imaging
// - Vintasoft.Imaging.UI
// - Vintasoft.Imaging.Office.OpenXml
// - Vintasoft.Imaging.Office.OpenXml.UI
// - Vintasoft.Imaging.Pdf
// - Vintasoft.Imaging.Pdf.UI
// - Vintasoft.Imaging.Pdf.Office.UI

public partial class FormWithPdfEditorTool : System.Windows.Forms.Form
{
    //...
    Vintasoft.Imaging.UI.ImageViewer _imageViewer;
    //...

    /// <summary>
    /// The PDF content editor tool.
    /// </summary>
    Vintasoft.Imaging.Pdf.UI.PdfContentEditorTool _contentEditorTool;

    /// <summary>
    /// The undo manager.
    /// </summary>
    Vintasoft.Imaging.Undo.UndoManager _undoManager = new Vintasoft.Imaging.Undo.UndoManager(20);

    public FormWithPdfEditorTool()
    {
        //...
        // initialize Vintasoft.Imaging.Pdf.Office.UI assembly
        Vintasoft.Imaging.PdfOfficeUIAssembly.Init();

        // create visual tool that allows to edit content on PDF page
        _contentEditorTool = new Vintasoft.Imaging.Pdf.UI.PdfContentEditorTool();

        // specify that PDF content editor tool should add and edit content on PDF page
        _contentEditorTool.AppendMode = false;

        // specify that PDF content editor tool should highlight content figures
        _contentEditorTool.FiguresHighlight = true;

        // specify that PDF content editor tool should not render figures on PDF page when current page is changed in image viewer
        _contentEditorTool.RenderFiguresWhenImageIndexChanging = false;
        
        // specify that PDF content editor tool should render figures on PDF page when PDF content editor tool is deactivating
        _contentEditorTool.RenderFiguresWhenDeactivating = true;

        // specify undo manager for PDF content editor tool
        _contentEditorTool.UndoManager = _undoManager;

        // specify that PDF content editor tool should work only with content of specified types
        _contentEditorTool.InteractiveContentType =
            Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.GraphicsFigureContentType.Text |
            Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.GraphicsFigureContentType.Image |
            Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.GraphicsFigureContentType.Form |
            Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.GraphicsFigureContentType.StrokePath |
            Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.GraphicsFigureContentType.FillPath;

        // create visual tool that allows to edit text in text content on PDF page
        Vintasoft.Imaging.Office.OpenXml.UI.VisualTools.UserInteraction.OfficeDocumentVisualEditorTextTool contentEditorTextTool =
            new Vintasoft.Imaging.Office.OpenXml.UI.VisualTools.UserInteraction.OfficeDocumentVisualEditorTextTool();

        // create the composite visual tool and set it as a current tool of image viewer
        _imageViewer.VisualTool = new Vintasoft.Imaging.UI.VisualTools.CompositeVisualTool(contentEditorTextTool, _contentEditorTool);
    }

    //...

    private void drawEllipseToolStripButton_Click(object sender, System.EventArgs e)
    {
        // start building an ellipse
        _contentEditorTool.StartBuildEllipse(
            new Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Red, 5),
            new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Green));
    }
}



General features for editing of PDF page content in WinForms/WPF viewer

The SDK can divide PDF page content to the following graphic figures:
The PdfContentEditorTool/WpfPdfContentEditorTool allows to display and edit graphic figures on PDF page in viewer.

The PdfContentEditorTool/WpfPdfContentEditorTool allows to:
Here is a screenshot of PdfEditorDemo application, which represents the main view of visual PDF content editor in PdfEditorDemo (each digit corresponds to the list above):




Edit rich text on PDF page in WinForms/WPF viewer

The SDK provides OfficeDocumentVisualEditor class that represents the rich text editor in WinForms/WPF viewer.
PdfContentEditorTool/WpfPdfContentEditorTool uses the rich text editor (OfficeDocumentVisualEditor) for editing of object of type "Text content group" (ContentStreamGraphicsFigureTextGroup) on PDF page in viewer.

The editing process of selected text object can be started by a few ways:
The rich text editor (OfficeDocumentVisualEditor) allows to:
Here is a screenshot of PdfEditorDemo application, which demonstrates the text and paragraph properties while editing the rich text:



Here is a screenshot of PdfEditorDemo application, which demonstrates the edited text block:




Edit vector graphics on PDF page in WinForms/WPF viewer

PdfContentEditorTool/WpfPdfContentEditorTool allows to use mouse for transforming, copying, changing of vector graphics on PDF page in WinForms/WPF viewer.

Here is a screenshot of PdfEditorDemo application, which demonstrates how to select and transform vector graphics figure on PDF page:



The PdfContentEditorTool.SetContentGraphicsProperties method allows to set the graphics state of graphic figure.

Here is a screenshot of PdfEditorDemo application, which demonstrates how to ungroup figures and change graphic state properties for vector graphics figure on PDF page:




Replace image or form on PDF page in WinForms/WPF viewer

An existing image or form on PDF page in WinForms/WPF viewer can be replaced using PdfContentEditorTool.ReplaceResource method.

Here is a screenshot of PdfEditorDemo application, which demonstrates dialog with information about image-resource of selected image on PDF page:


Here is a screenshot of PdfEditorDemo application, which demonstrates how to create new image-resource from image file and use it as image-resource for selected image on PDF page:


Here is a screenshot of PdfEditorDemo application, which displays the changed image on PDF page:




Create, change and delete the clipping area for figure on PDF page in WinForms/WPF viewer

New clipping area can be added to any figure on PDF page using the PdfContentEditorTool.AddFigureClip method.

Here is a screenshot of PdfEditorDemo application, which demonstrates how to set the elliptical clipping area for image on PDF page:



An existing clipping area for any figure on PDF page can be selected, transformed or deleted using mouse.

Here is a screenshot of PdfEditorDemo application, which demonstrates the transformation of image clipping area on PDF page:




Add graphics onto PDF page in WinForms/WPF viewer

The PdfContentEditorTool allows to add the following graphical figures onto PDF page:
The PdfContentEditorTool.AddFigure method allows to add graphical figure onto PDF page.

The PdfContentEditorTool.StartBuildFigure method allows to start building of graphical figure onto PDF page using mouse.

Here is a screenshot of PdfEditorDemo application, which demonstrates how to add graphics onto PDF page:




Add charts onto PDF page in WinForms/WPF viewer

SDK provides OfficeDocumentFigure class that allows to draw DOCX page on PDF page.
PDF Editor Demo demonstrates how to add DOCX page with chart onto PDF page (add/build OfficeDocumentFigure onto PDF page using PdfContentEditorTool.AddFigure/ PdfContentEditorTool.StartBuildFigure method).

Here is a screenshot of PdfEditorDemo application, which demonstrates how to select chart for adding onto PDF page:



Also PDF Editor Demo demonstrates how to change the chart data and view the chart changes on PDF page in viewer.

Here is a screenshot of PdfEditorDemo application, which demonstrate how to select the chart on PDF page and edit the chart properties:




Requirements for editing of PDF page content in WinForms/WPF viewer

If you want to edit PDF page content in WinForms/WPF viewer, the following VintaSoft products are necessary:

Share this article