pdf中基于坐标c#的高亮显示元素

时间:2019-06-03 14:39:17

标签: c# asp.net pdf itext hiqpdf

我有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的第三页。

"boundingBox": [3.2924,7.7146,5.7564,7.7038,5.7671,7.9836,3.3032,7.9943]

“文本”:“ 66 66 6666 6666” 应该得到

Input File Output File

1 个答案:

答案 0 :(得分:1)

页面错误

首先,将注释添加到错误的页面。

你说

  

需要突出显示PDF的第三页。

但您将其放在第1页上

stamper.AddAnnotation(highlight,1);

要解决此问题,请在此处更改页码:

stamper.AddAnnotation(highlight,3);

坐标错误

代码中的坐标都没有

iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f);

也不是您以JSON方式提供的内容

"boundingBox": [3.2924,7.7146,5.7564,7.7038,5.7671,7.9836,3.3032,7.9943]

位于您要突出显示的位置附近的任何地方,至少不在页面媒体框提供的常规PDF坐标系中。通过在Adobe Acrobat中进行测量,我得到了以下近似坐标:

iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(240f, 264f, 413f, 289f);

如果您显示的任何坐标都是要突出显示的图像部分的实际坐标,请向这些坐标的提供者询问所使用的坐标系,并相应地转换为给定页面媒体框中的坐标。

QuadPoints中的可疑顺序

您使用以下顺序创建quad

float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top };

这将导致凹盖。您可能要使用

float[] quad = { rect.Left, rect.Top, rect.Right, rect.Top, rect.Left, rect.Bottom, rect.Right, rect.Bottom };

而不是Adobe Reader显示为凸盖。要了解背景,请阅读this answer

示例输出

您说:

  

“ 66 66 6666 6666”应该被高亮显示

将以上三个更改应用于您的代码后,我得到了:

screenshot

相关问题