将文本设为 link/annotation 以调用 PDF 中的转到其他页面操作

Make text a link/annotation to invoke goTo other page action in PDF

如何将 PDF 中的纯文本 link 转换为 pdf 文档的另一部分? 目前,我正在 post 处理 PDF。如果在页面中找到两个数字(文本对象),我已经确定了应该 link 相互打包的两个页面。

有什么方法可以将该文本转换为可点击的本地文本 link?

我们已经检查了报告的查询“将文本设为 link/annotation 以调用 PDF 中的转到其他页面操作”,并准备了一个测试样本来满足您的要求。在此示例中,我们使用 PdfDocumentLinkAnnotation 通过单击文本来导航内部文档。请检查示例,它在下面 link 中提供,供您参考。

请从 the following link 中查找样本。

另请参阅下方 UG documentation link 以了解有关 PdfDocumentLinkAnnotation 的更多详细信息。

link中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;

namespace ConsoleApplication1
{
    class Program
    {
        public static string DataPathOutput
        {
            get
            {
                if (!Directory.Exists(System.Environment.CurrentDirectory + @"\..\..\Output\"))
                    Directory.CreateDirectory(System.Environment.CurrentDirectory + @"\..\..\Output\");
                return System.Environment.CurrentDirectory + @"\..\..\Output\";
            }
        }

        static void Main(string[] args)
        {
            // Creates a new PDF document
            PdfDocument document = new PdfDocument();

            // Creates a first page 
            PdfPage firstPage = document.Pages.Add();

            PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12f);
            PdfBrush brush = PdfBrushes.Black;
            PdfGraphics graphics1 = firstPage.Graphics;

            string inputText1 = "Sample Text-1";
            graphics1.DrawString(inputText1, font, brush, 10, 40);

            // Measure string size to use same size for annotation
            SizeF size1 = font.MeasureString(inputText1);
            RectangleF rectangle1 = new RectangleF(10, 40, size1.Width, size1.Height);

            // Creates a second page
            PdfPage secondPage = document.Pages.Add();
            PdfGraphics graphics2 = secondPage.Graphics;

            string secondPageInput = "Sample Text-2";
            graphics2.DrawString(secondPageInput, font, brush, 10, 40);

            // Measure string size to use same size for annotation
            SizeF size2 = font.MeasureString(inputText1);
            RectangleF rectangle2 = new RectangleF(10, 40, size2.Width, size2.Height);

            // Add annotation for firstpage to link second page of PdfDocumet
            PdfDocumentLinkAnnotation firstAnnotation = new PdfDocumentLinkAnnotation(rectangle1);
            firstAnnotation.Color = new PdfColor(Color.Transparent);
            firstAnnotation.Destination = new PdfDestination(secondPage);
            // Use below comment for link specific part of page
            //firstAnnotation.Destination.Location = new Point(10, 40);
            firstPage.Annotations.Add(firstAnnotation);

            // Add annotation for second page to link first page of PdfDocumet
            PdfDocumentLinkAnnotation secondAnnotation = new PdfDocumentLinkAnnotation(rectangle2);
            secondAnnotation.Color = new PdfColor(Color.Transparent);
            secondAnnotation.Destination = new PdfDestination(firstPage);
            // Use below comment for link specific part of page
            //secondAnnotation.Destination.Location = new Point(10, 40);
            secondPage.Annotations.Add(secondAnnotation);

            // Save document on mentioned location
            document.Save(System.IO.Path.Combine(DataPathOutput, "Output.pdf"));
            document.Close(true);
        }
    }
}