使用C#使用iTextsharp突出显示现有PDF的文本(颜色)

我想知道我们是否可以使用itextsharp突出显示已创建PDF文本(颜色)?

我看到了创建新PDF等示例,同时我们可以应用颜色。 我正在寻找可以从PDF获取大量文本并应用颜色并保存的地方。

这是我想要完成的事情,阅读PDF文件,解析文本并根据业务规则突出显示文本。

任何第三方dll建议也有效,作为我正在寻找opensource iTextsharp library

是的,你可以突出显示文字,但不幸的是你必须为它工作。 在考虑规范的情况下,看起来像高亮的是PDF文本标记注释。 那部分很简单。 困难的部分是找出应用注释的坐标。

这是使用现有PdfStamper称为stamper创建高亮的简单代码:

 PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad); 

突出显示后,您可以使用以下颜色设置颜色:

 highlight.Color = BaseColor.YELLOW; 

然后使用以下命令将其添加到第1页的stamper

 stamper.AddAnnotation(highlight,1); 

从技术上讲, rect参数实际上并没有被使用(据我所知),而是被quad参数覆盖。 quad参数是x,y坐标的数组,其基本上表示矩形的角(技术上为四边形)。 规格说他们从左下角开始逆时针走,但实际上它们似乎是从左下到右下到左上到右上。 计算四边形是一种痛苦,所以更容易创建一个矩形并从中创建四边形:

 iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f); float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; 

那么如何从一开始就获得现有文本的矩形? 为此,您需要查看TextExtractionStrategyPdfTextExtractor 。 有很多事情要做,所以我将首先指出你的post ,其中有一些链接的post。

下面是一个针对iTextSharp 5.1.1.2的全functionC#2010 WinForms应用程序,该应用程序展示了简单PDF的创建以及使用硬编码坐标突出显示部分文本。 如果您需要帮助计算这些坐标,请从上面的链接开始,然后提出任何问题!

 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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Create a simple test file string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (Document doc = new Document(PageSize.LETTER)) { using (PdfWriter w = PdfWriter.GetInstance(doc, fs)) { doc.Open(); doc.Add(new Paragraph("This is a test")); doc.Close(); } } } //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); } } this.Close(); } } }