如何在PDF中将纯文本作为指向pdf文档另一部分的链接? 目前,我正在处理PDF。如果在页面中找到两个数字(文本对象),我已经确定了两个应该将包装链接到彼此的页面。
有没有办法可以将该文本转换为可点击的本地链接?
答案 0 :(得分:1)
我们检查了报告的查询“使文本成为链接/注释以调用goto中的其他页面操作”,并准备了一个测试样本以满足您的要求。在此示例中,我们使用PdfDocumentLinkAnnotation
通过单击文本来导航内部文档。请检查样品,可在以下链接中找到,供您参考。
请从the following link找到样本。
另请参阅以下UG documentation link以了解有关PdfDocumentLinkAnnotation
的更多详情。
链接中的代码:
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);
}
}
}