gpt4 book ai didi

c# - 使用itextsharp突出显示pdf中的单词,而不在浏览器中显示突出显示的单词

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:00 25 4
gpt4 key购买 nike

使用itextsharp在浏览器中不显示突出显示的单词。

土坯

enter image description here

浏览器

enter image description here





 List<iTextSharp.text.Rectangle> MatchesFound = strategy.GetTextLocations(splitText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
foreach (Rectangle rect in MatchesFound)
{
float[] quad = { rect.Left - 3.0f, rect.Bottom, rect.Right, rect.Bottom, rect.Left - 3.0f, rect.Top + 1.0f, rect.Right, rect.Top + 1.0f };
//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, pageno);

}





请帮助我解决这个问题。

更新代码

  private void highlightPDF()
{
//Create a simple test file
string outputFile = Server.MapPath("~/pdf/16193037V_Dhana-FI_NK-QA_Completed.pdf");
string filename = "HL" + Convert.ToString(Session["Filename"]) + ".pdf";
Session["Filename"] = "HL" + Convert.ToString(Session["Filename"]);
//Create a new file from our test file with highlighting
string highLightFile = Server.MapPath("~/pdf/" + filename);

//Bind a reader and stamper to our test PDF

PdfReader reader = new PdfReader(outputFile);
iTextSharp.text.pdf.PdfContentByte canvas;
int pageno = Convert.ToInt16(txtPageno.Text);
using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
canvas = stamper.GetUnderContent(pageno);
myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
strategy.UndercontentCharacterSpacing = canvas.CharacterSpacing;
strategy.UndercontentHorizontalScaling = canvas.HorizontalScaling;

string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy);
string text = txtHighlight.Text.Replace("\r\n", "").Replace("\\n", "\n").Replace(" ", " ");
string[] splitText = text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < splitText.Length; i++)
{
List<iTextSharp.text.Rectangle> MatchesFound = strategy.GetTextLocations(splitText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
foreach (Rectangle rect in MatchesFound)
{
canvas.SaveState();
canvas.SetColorFill(BaseColor.YELLOW);
canvas.Rectangle(rect);
canvas.Fill();
canvas.RestoreState();
}
}

}
}
reader.Close();


}


它没有突出显示文本。我通过了文字和第no页以突出显示文字。

最佳答案

首先...

为什么OP的(更新的)代码不起作用

实际上有两个因素。

首先,OP的代码中存在一个问题,就是要在他使用的路径中添加一个矩形

canvas.Rectangle(rect);


不幸的是,这不是他所期望的: Rectangle类具有多个属性,而不仅仅是矩形的坐标,有关所选边框,边框颜色和内部颜色的最重要信息,而 PdfContentByte.Rectangle(Rectangle)根据这些属性绘制矩形。

但是,在当前情况下, rect仅用于传输矩形的坐标,因此那些附加属性都是 falsenull。因此, canvas.Rectangle(rect)什么都不做!

相反,OP应该使用

canvas.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);


这里。

此外,@ Bruno在回答中提到


  请注意,如果将黄色矩形添加为不透明形状(例如,在图像下方),则不会看到该矩形。


不幸的是,这里确实是这样:该文档实际上是一个扫描文档,每页都是一个页面填充图像,在该图像下绘制了等效的文本(可能在OCR之后),以允许进行文本复制和粘贴。

因此,无论OP的代码可以在 UnderContent上绘制什么,它都会被该图像隐藏。

因此,让我们尝试一些不同的东西...

如何运作

@Bruno在他的回答中也指出了这种情况的解决方案:


  在这种情况下,您可以在现有内容之上添加一个透明矩形。


遵循此建议,我们将替换

canvas = stamper.GetUnderContent(pageno);


通过

canvas = stamper.GetOverContent(pageno);

PdfGState state = new PdfGState();
state.FillOpacity = .3f;
canvas.SetGState(state);


在第三个文档页面上选择“支持”一词,我们得到:

using an opacity of .3

黄色在这里很苍白。

使用 Opacity.6值代替

using an opacity of .6

现在,黄色更加强烈,但文字开始变淡。

对于这样的任务,我实际上更喜欢使用混合模式“变暗”。这可以通过使用

state.BlendMode = new PdfName("Darken");


而不是 state.FillOpacity = .3f。这导致

using the blend mode Darken

这海事组织看起来更好。

客户是如何做到的

OP评论


  客户提供了pdf文件。这样,他们突出显示了文本,突出显示的文本显示在浏览器中


客户的PDF实际上使用了注释,就像他原始代码中的OP一样,但是相反,客户的每个注释都包含一个外观流,而iText生成的高亮注释则没有。

提供外观是可选的,并且如果没有给出,则PDF查看器确实应该生成外观。但是,显然,有许多PDF查看器依赖于PDF带来的外观。

顺便说一句,客户端PDF中的外观实际上使用混合模式“乘”。对于基础白色和黑色,“变暗”和“相乘”具有相同的结果。

使它与注释一起使用

OP在评论中想知道


  请再怀疑一次,如果用户错误地加亮了颜色,那么如何去除黄色(或将黄色变为白色)?我将黄色更改为白色,但无法正常工作。 canvas.SetColorFill(BaseColor.WHITE);


通常,撤消页面内容的更改比撤消添加注释更困难。因此,让我们也可以使用OP的原始代码,即在高亮注释中添加外观流。

正如OP在另一条评论中所述,他首次尝试添加外观流失败:


PdfAppearance appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect.Width, rect.Height);
appearance.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);
appearance.SetColorFill(BaseColor.WHITE);
appearance.Fill();
highlight.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, appearance );
stamper.AddAnnotation(highlight, pageno);

  
  但它不起作用。


他尝试中遇到的问题是:


外观模板的原点位于注释区域而不是页面的左下角。因此,要给所讨论的区域着色,矩形的左下角必须为(0,0)。
严格来说,必须在开始构建路径之前设置颜色。
应该使用不同于白色的颜色突出显示。
应使用透明胶片或适当的呈现模式,以使原始的标记文本发光。


因此,以下代码显示了如何执行此操作。

private void highlightPDFAnnotation(string outputFile, string highLightFile, int pageno, string[] splitText)
{
PdfReader reader = new PdfReader(outputFile);
iTextSharp.text.pdf.PdfContentByte canvas;
using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
strategy.UndercontentHorizontalScaling = 100;

string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy);
for (int i = 0; i < splitText.Length; i++)
{
List<iTextSharp.text.Rectangle> MatchesFound = strategy.GetTextLocations(splitText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
foreach (Rectangle rect in MatchesFound)
{
float[] quad = { rect.Left - 3.0f, rect.Bottom, rect.Right, rect.Bottom, rect.Left - 3.0f, rect.Top + 1.0f, rect.Right, rect.Top + 1.0f };
//Create our hightlight
PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);
//Set the color
highlight.Color = BaseColor.YELLOW;

PdfAppearance appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect.Width, rect.Height);
PdfGState state = new PdfGState();
state.BlendMode = new PdfName("Multiply");
appearance.SetGState(state);
appearance.Rectangle(0, 0, rect.Width, rect.Height);
appearance.SetColorFill(BaseColor.YELLOW);
appearance.Fill();

highlight.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance);

//Add the annotation
stamper.AddAnnotation(highlight, pageno);
}
}
}
}
reader.Close();
}


这些注释也可以由Chrome显示,并且可以轻松删除它们作为注释。

关于c# - 使用itextsharp突出显示pdf中的单词,而不在浏览器中显示突出显示的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com