【发布时间】:2019-10-19 03:24:30
【问题描述】:
我有 5 页 pdf 需要根据坐标突出显示特定元素
有X top left,Y top left,X top right ,Y top right , X bottom right , Y bottom right ,X bottom left, Y bottom left 。
我使用 iTextsharp 尝试了下面的代码,请建议我们如何做到这一点,包括页面编号
using System;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
//Create a simple test file
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//Create a new file from our test file with highlighting
string highLightFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Highlighted.pdf");
//Bind a reader and stamper to our test PDF
PdfReader reader = new PdfReader(outputFile);
using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
//Create a rectangle for the highlight. NOTE: Technically this isn't used but it helps with the quadpoint calculation
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f);
//Create an array of quad points based on that rectangle. NOTE: The order below doesn't appear to match the actual spec but is what Acrobat produces
float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };
//Create our hightlight
PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);
//Set the color
highlight.Color = BaseColor.YELLOW;
//Add the annotation
stamper.AddAnnotation(highlight,1);
}
}
输出 以矩形突出显示元素。 需要突出显示 PDF 的第 3 页。
"boundingBox": [3.2924,7.7146,5.7564,7.7038,5.7671,7.9836,3.3032,7.9943]
这个 "text": "66 66 6666 6666" 应该被高亮显示
【问题讨论】:
-
“需要突出显示 PDF 的第 3 页。”- 那你为什么要在第 1 页添加注释呢?
stamper.AddAnnotation(highlight,1) -
谢谢 mkl,但是矩形框没有出现。有没有任何公式可以计算
rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top当我使用我的坐标时,它出现在底部。 -
@mkl 你能建议找到输入和输出文件吗
-
您的 output.pdf 看起来不像 iText 5.x 是操纵它的最终 PDF 处理器。之后是否可能有一个注释展平器来处理 PDF?
-
@mkl
the final PDF processor manipulating?是的,例如创建的示例文件需要使用指定的边界框指向该段
标签: c# asp.net pdf itext hiqpdf