使用iTextSharp的使用C#现有的PDF中突出显示文本(颜色) [英] Highlighting text ( colors ) of existing PDF using iTextsharp using C#

查看:1382
本文介绍了使用iTextSharp的使用C#现有的PDF中突出显示文本(颜色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like know whether we can highlight text (colors) of already created PDF using itextsharp?

我看到这样创建一个新的PDF,而这样做,我们可以应用颜色的例子。我在寻找在那里我能得到的文本从PDF块,应用颜色并保存。

I see examples like creating a new PDF, while doing so we can apply colors. I am looking for where I can get chunks of text from PDF and apply colors and save it.

下面是物我试图完成,根据业务规则,阅读PDF文件,分析文本和突出显示文本。

Here is the thing I am trying to accomplish, read a PDF file, parse text and highlight text based on business rules.

任何第三方DLL的建议也适用,因为我期待到开源iTextSharp的库第一步

Any third party dll suggestion also works, as a first step I am looking in to opensource iTextsharp library.

推荐答案

是您可以高亮文本,但你必须为它不幸的工作。什么样子的一大亮点是PDF文本标记注释就认为spec。这部分是pretty的方便。困难的部分是搞清楚坐标标注适用。

Yes you can highlight text but you will have to work for it unfortunately. What looks like a highlight is a PDF Text Markup Annotation as far as the spec is considered. That part is pretty easy. The hard part is figuring out the coordinates to apply the annotation to.

下面是简单的code使用创建的一大亮点现有 PdfStamper 名为压模

Here's the simple code for creating a highlight using an existing PdfStamper called stamper:

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

一旦你有,你可以使用设置颜色的亮点:

Once you have the highlight you can set the color using:

highlight.Color = BaseColor.YELLOW;

然后将其添加到第1页使用压模的:

stamper.AddAnnotation(highlight,1);

技术上的矩形参数实际上并不习惯(只要我可以告诉),而是得到了四重写参数。该参数是x的数组,Y COORDS,基本上重新矩形(技术上四边形)的present角落。该规范说,他们开始在左下角和逆时针走,但在现实中,他们似乎去左下右下到左上到右上。计算四是痛苦所以不是它只是更容易地创建一个矩形,并从它创建四:

Technically the rect parameter doesn't actually get used (as far as I can tell) and instead gets overridden by the quad parameter. The quad parameter is an array of x,y coords that essentially represent the corners of a rectangle (technically quadrilateral). The spec says they start in the bottom left and go counter-clockwise but in reality they appear to go bottom left to bottom right to top left to top right. Calculating the quad is a pain so instead its just easier to create a rectangle and create the quad from it:

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 };

那么,如何让现有文本的矩形摆在首位?对于您需要看 TextExtractionStrategy PdfTextExtractor 。有很多进入,所以我要开始由<一个href="http://stackoverflow.com/questions/7513209/using-locationtextextractionstrategy-in-itextsharp-for-text-coordinate">pointing你在这个职位其中有一些进一步的职位联系起来。

So how do you get the rectangle of existing text in the first place? For that you need to look at TextExtractionStrategy and PdfTextExtractor. There's a lot to go into so I'm going to start by pointing you at this post which has some further posts linked.

下面是一个完整的工作C#2010 WinForms应用程序针对iTextSharp的5.1.1.2,展示创建一个简单的PDF和使用硬codeD协调文本的一部分高亮显示。如果您需要帮助计算这些坐标先从上面的链接,然后问​​任何问题!

Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.2 that shows off the creation of a simple PDF and the highlighting of part of the text using hard-coded coordinates. If you need help calculating these coordinates start with the link above and then ask any questions!

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();
        }
    }
}

这篇关于使用iTextSharp的使用C#现有的PDF中突出显示文本(颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆