【问题标题】:Add Document link to existing Pdf file using PdfSharp使用 PdfSharp 将文档链接添加到现有 Pdf 文件
【发布时间】:2021-01-08 09:03:27
【问题描述】:

我正在使用 PdfSharp 修改 Pdf 文件。 我有一个现有的 pdf,其中包含表格格式的数据。

我想知道 PdfSharp 中是否有可以将 DocumentLink 添加到现有元素的内容?

或者类似的东西,我可以从中确定每个文本的 X 和 Y,然后我可以在其上渲染 Rect 并可以在其上添加 DocumentLink?

用外部pdf文件真的可以吗?

【问题讨论】:

    标签: c# pdfsharp


    【解决方案1】:

    是的,这是可能的。我下面的代码只是创建一个 pdf 文档,添加两行文本。第一行是链接文本,第二行是链接操作的目标文本。

        static void Main(string[] args)
        {
            string doc1 = @"C:\doc1.pdf";
    
            CreatePdf(doc1);
    
            CSequence contents;
    
            using (var doc = PdfReader.Open(doc1, PdfDocumentOpenMode.Modify))
            {
    
                AddDocumentLink(doc, page);
    
                doc.Save(doc1);
                doc.Close();
            }
    
            Process.Start(doc1);
    
        }
    
        public static void CreatePdf(string filename)
        {
            // Create a new PDF document
            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";
    
            // Create an empty page
            PdfPage page = document.AddPage();
    
            AddTextToPage(page);
    
            // Save the document...
            document.Save(filename);
            // ...and start a viewer.
        }
    
        public static void AddTextToPage(PdfPage page)
        {
            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);
    
            // Create a font
            XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic, new XPdfFontOptions(PdfFontEncoding.WinAnsi));
    
            // Draw the text
            gfx.DrawString("Click here to go to [Hello, World!]", font, XBrushes.Black,
              new XRect(0, 0, page.Width, page.Height),
              XStringFormats.TopCenter);
    
            gfx.DrawString("Hello, World!", font, XBrushes.Black,
              new XRect(0, 0, page.Width, page.Height),
              XStringFormats.Center);
        }
    
        // Please refer to the pdf tech specs on what all entails in the content stream
        // https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf
        public static void AddDocumentLink(PdfDocument pdf, PdfPage page)
        {
            PdfContents contents = page.Contents;
            if (contents != null && contents.Elements.Count > 0)
            {
    
                CSequence pageContents = ContentReader.ReadContent(page);
                int numOfContentElements = pageContents.Count;
    
                COperator currentElement = null;
                COperator previousElement = null;
                COperator nextElement = null;
    
                double xLink = 0;
                double yLink = 0;
                double xDest = 0;
                double yDest = 0;
    
                for (int j = 0; j < numOfContentElements; j++)
                {
                    currentElement = (COperator)pageContents[j];
    
                    if (j > 0)
                        previousElement = (COperator)pageContents[j - 1];
                    if (j > 0 && j + 1 < numOfContentElements)
                        nextElement = (COperator)pageContents[j + 1];
    
                    // get coordinates for the text to create link for
                    if (currentElement.OpCode.OpCodeName == OpCodeName.Td
                    && nextElement != null
                        && nextElement.OpCode.OpCodeName == OpCodeName.Tj
                        && nextElement.Operands[0] is CString
                        && ((CString)nextElement.Operands[0]).Value.Contains("Click here"))
                    {
                        xLink = ((CReal)currentElement.Operands[0]).Value;
                        yLink = ((CReal)currentElement.Operands[1]).Value;
    
                    }
    
                    // get coordinates of Hello World text
                    if (currentElement.OpCode.OpCodeName == OpCodeName.Td
                        && nextElement != null
                        && nextElement.OpCode.OpCodeName == OpCodeName.Tj
                        && nextElement.Operands[0] is CString
                        && ((CString)nextElement.Operands[0]).Value.StartsWith("Hello World"))
                    {
                        xDest = ((CReal)currentElement.Operands[0]).Value;
                        yDest = ((CReal)currentElement.Operands[1]).Value;
    
                    }
    
                }
            
            
                // Create a link annotation
                pdf.AddNamedDestination("GotoHello", 1, PdfNamedDestinationParameters.CreatePosition(xDest, yDest, 1));
                page.AddDocumentLink(new PdfRectangle(new XRect(xLink, yLink, 300, 25)), "GotoHello");
            }
    
    
        }
    

    最好, 原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多