首页 > 解决方案 > 使用 Telerik DocumentProcessing 库将文本项添加到现有 PDF

问题描述

我想打开一个现有的 PDF 文档并向其添加不同的注释。即书签和一些文本我正在使用 Telerik Document Processing Library (dpl) v2019.3.1021.40

我是 dpl 的新手,但我相信 RadFlowDocument 是要走的路。我在创建 RadFlowDocument 时遇到问题

         FlowProvider.PdfFormatProvider provider = new FlowProvider.PdfFormatProvider();
             using (Stream stream = File.OpenRead(sourceFile))
            {
   -->           RadFlowDocument flowDoc  = provider.Import(stream);


            }

带有箭头的行给出错误“不支持导入”

这里有一篇 Telerik 博客文章 https://www.telerik.com/forums/radflowdocument-to-pdf-error 似乎相关,但不是 100% 确定。它警告要确保提供者正确匹配,我相信他们在我的例子中......

同样,最终目标是打开 PDF 并向其中添加一些内容。我认为 RadFlowDocument 是正确的方向。如果有更好的解决方案,我也很高兴听到。

标签: pdftelerik

解决方案


我想到了。DPL 非常好,但文档仍在增长,希望这对某人有所帮助...

这来自无数的文章,我不能开始引用它们。

在 DPL 中使用 PDF 有 2 个概念。FixedDocument 需要页面。我认为这是为了将文档缝合在一起。我相信 FlowDocument 可以像 HTML 渲染器一样进行布局。

我使用的是固定的,主要是 b/c 我可以让它工作。

    using System;
    using System.IO;
    using System.Windows;  //nec for Size struct
    using System.Diagnostics; //nec for launching the pdf at the end 

    using Telerik.Windows.Documents.Fixed.Model;
    //if you have fixed and flow provider, you have to specify, so I make a shortcut
    using FixedProvider = Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
    using Telerik.Windows.Documents.Fixed.Model.Editing;

    using Microsoft.VisualStudio.TestTools.UnitTesting;


    namespace DocAggregator
    {
        [TestClass]
        public class UnitTest2
        {
            [TestMethod]
            public void EditNewFIle_SrcAsFixed_TrgAsFixed()
            {
                String dt = @"C:\USERS\greg\DESKTOP\DPL\";
                String sourceFile = dt + "output.pdf";

                //Open the sourceDoc so you can add stuff to it
                RadFixedDocument sourceDoc;

                //a provider parses the actual file into the model. 

                FixedProvider.PdfFormatProvider fixedProv = new FixedProvider.PdfFormatProvider();
                using (Stream stream = File.OpenRead(sourceFile))
                {
                    //'populate' the doc object from the file

                    //using the  FLOW classes, I get "Import  Not Supported". 
                    sourceDoc = fixedProv.Import(stream);
                }
                int pages = sourceDoc.Pages.Count;
                int pageCounter = 1;
                int xoffset = 150;
                int yoffset = 50;

                //editor is the thing that lets you add elements into the source doc
//Like the provider, the Editor needs to match the document class (Fixed or Flow)
                RadFixedDocumentEditor editor = new RadFixedDocumentEditor(sourceDoc);

                foreach (RadFixedPage page in sourceDoc.Pages)
                {
                    FixedContentEditor pEd = new FixedContentEditor(page);
                    Size ps = page.Size;

                    pEd.Position.Translate(ps.Width - xoffset, ps.Height - yoffset);

                    Block block = new Block();
                    block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
                    block.TextProperties.FontSize = 22;
                    block.InsertText(string.Format("Page {0} of {1} ", pageCounter, pages));
                    pEd.DrawBlock(block);
                    pageCounter++;
                }

                string exportFileName = "addedPageNums.pdf";

                if (File.Exists(exportFileName))
                {
                    File.Delete(exportFileName);
                }

                File.WriteAllBytes(exportFileName, fixedProv.Export(sourceDoc));
                //launch the app
                Process.Start(exportFileName);
            }

        }
    }

推荐阅读