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

时间:2011-12-20 22:18:38

标签: c# asp.net .net itextsharp

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

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

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

任何第三方dll建议也有效,这是我期待opensource iTextsharp library的第一步。

1 个答案:

答案 0 :(得分:6)

是的,您 可以 突出显示文字,但不幸的是,您必须为此工作。在考虑规范的情况下,看起来像高亮的是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。我需要做很多事情,所以我将从pointing you at this post开始,其中还有其他一些帖子。

以下是针对iTextSharp 5.1.1.2的全功能C#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();
        }
    }
}
相关问题