我正在使用 PdfSharp 修改 Pdf 文件。 我有一个现有的 pdf,其中包含表格格式的数据。
我想知道 PdfSharp 中是否有任何内容可以将 DocumentLink 添加到这些现有元素中?
或者类似的东西,我可以从中确定每个文本的 X 和 Y,然后我可以在其上渲染 Rect 并可以在其上添加 DocumentLink?
外部pdf文件真的可以做到这一点吗?
答案 0 :(得分:0)
是的,这是可能的。我下面的代码只是创建一个 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");
}
}
最好的, 理由