在 WinForms/WPF 查看器中编辑 PDF 页面内容。

博客分类:PDF.NET

2022/01/20

VintaSoft Imaging .NET SDK 具有丰富的功能,可用于在 WinForms/WPF 查看器中编辑 PDF 文档:


在演示应用程序 VintaSoft PDF Editor Demo (WinForms) 和 VintaSoft WPF PDF Editor Demo (WPF) 中演示了 PDF 文档的可视化编辑功能。

本文概述了在 WinForms/WPF 查看器中可视化编辑 PDF 文档内容的功能。



在 WinForms/WPF 查看器中编辑 PDF 页面内容


WinForms控件ImageViewer允许在WinForms应用程序中显示PDF文档的页面。ImageViewer控件可以使用可视化工具PdfContentEditorTool在查看器中编辑PDF页面内容。
WPF控件WpfImageViewer允许在WPF应用程序中显示PDF文档的页面。WpfImageViewer控件可以使用可视化工具WpfPdfContentEditorTool在查看器中编辑PDF页面内容。

以下代码创建了一个ImageViewer,其中包含一个PdfContentEditorTool,用于在WinForms应用程序中显示和编辑PDF页面内容:
// 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));
    }
}



在 WinForms/WPF 查看器中编辑 PDF 页面内容的常规功能

SDK 可以将 PDF 页面内容划分为以下图形:
PdfContentEditorTool / WpfPdfContentEditorTool 允许在查看器中显示和编辑 PDF 页面上的图形。

PdfContentEditorTool / WpfPdfContentEditorTool 允许:
以下是 PdfEditorDemo 应用程序的屏幕截图,展示了 PdfEditorDemo 中可视化 PDF 内容编辑器的主视图(每个数字对应上面的列表):




在 WinForms/WPF 查看器中编辑 PDF 页面上的富文本

SDK 提供 OfficeDocumentVisualEditor 类,该类表示WinForms/WPF 查看器中的富文本编辑器。
PdfContentEditorTool / WpfPdfContentEditorTool 使用富文本编辑器 (OfficeDocumentVisualEditor) 用于在查看器中编辑 PDF 页面上的"文本内容组"对象(ContentStreamGraphicsFigureTextGroup)。

可以通过以下几种方式开始编辑选定的文本对象:
富文本编辑器(OfficeDocumentVisualEditor)允许:
以下是 PdfEditorDemo 应用程序的屏幕截图,演示了编辑富文本时的文本和段落属性:



以下是 PdfEditorDemo 应用程序的屏幕截图,演示了编辑后的文本块:




在 WinForms/WPF 查看器中编辑 PDF 页面上的矢量图形

PdfContentEditorTool / WpfPdfContentEditorTool 允许在 WinForms/WPF 查看器中使用鼠标对 PDF 页面上的矢量图形进行变换、复制和更改。

以下是以下是 PdfEditorDemo 应用程序的屏幕截图,演示了如何在 PDF 页面上选择和变换矢量图形:



PdfContentEditorTool.SetContentGraphicsProperties 方法允许设置图形的图形状态。

以下是 PdfEditorDemo 应用程序的屏幕截图,演示了如何在 PDF 页面上取消图形分组并更改矢量图形的图形状态属性:




在 WinForms/WPF 查看器中替换 PDF 页面上的图像或窗体

可以使用PdfContentEditorTool.ReplaceResource方法替换WinForms/WPF查看器中PDF页面上的现有图像或表单。

以下是 PdfEditorDemo 应用程序的屏幕截图,演示了包含有关 PDF 页面上所选图像的图像资源信息的对话框:


以下是 PdfEditorDemo 应用程序的屏幕截图,演示了如何从图像文件创建新的图像资源并将其用作 PDF 页面上所选图像的图像资源:


以下是 PdfEditorDemo 应用程序的屏幕截图,显示了 PDF 页面上已更改的图像:




在WinForms/WPF查看器中创建、更改和删除PDF页面上图形的剪切区域

可以使用PdfContentEditorTool.AddFigureClip方法向PDF页面上的任何图形添加新的裁剪区域。

以下是PdfEditorDemo应用程序的屏幕截图,演示如何设置 PDF 页面上图像的椭圆形裁剪区域:



可以使用鼠标选择、变换或删除 PDF 页面上任何图形的现有裁剪区域。

以下是 PdfEditorDemo 应用程序的屏幕截图,演示了 PDF 页面上图像剪切区域的转换:




在 WinForms/WPF 查看器中向 PDF 页面添加图形

PdfContentEditorTool 允许在 PDF 页面上添加以下图形:
PdfContentEditorTool.AddFigure 方法允许在 PDF 页面上添加图形。

The PdfContentEditorTool.StartBuildFigure 方法允许使用鼠标在 PDF 页面上开始构建图形。

以下是 PdfEditorDemo 应用程序的屏幕截图,演示了如何在 PDF 页面上添加图形:




在 WinForms/WPF 查看器中将图表添加到 PDF 页面

SDK 提供 OfficeDocumentFigure 类,允许在 PDF 页面上绘制 DOCX 页面。
PDF 编辑器演示如何将包含图表的 DOCX 页面添加到 PDF 页面(使用 OfficeDocumentFigure 添加到 PDF 页面 / PdfContentEditorTool.AddFigure / PdfContentEditorTool.StartBuildFigure 方法)。

以下是 PdfEditorDemo 应用程序的屏幕截图,演示了如何选择要添加到 PDF 页面的图表:



PDF 编辑器演示还演示了如何更改图表数据并在查看器中查看 PDF 页面上的图表更改。

以下是 PdfEditorDemo 应用程序的屏幕截图,演示了如何在 PDF 页面上选择图表并编辑图表属性:




在 WinForms/WPF 查看器中编辑 PDF 页面内容的要求

如果您想在 WinForms/WPF 查看器中编辑 PDF 页面内容,则需要以下 VintaSoft 产品: