【发布时间】:2015-11-27 07:22:40
【问题描述】:
使用 itextsharp 在浏览器中未显示突出显示的字词。
Adobe
浏览器
代码
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();
}
它没有突出显示文本。我传递了文本和页码以突出显示文本。
【问题讨论】:
-
这不是 iText 的问题。这是您在浏览器中使用的 PDF 查看器的问题,而您不知道是哪个 PDF 查看器。它可能是 Chrome 的 PDF 查看器;在这种情况下,请将其设为 Chrome PDF 查看器问题。它可以是 Firefox 中的 pdf.js;在这种情况下,将其设为 pdf.js 问题。不要将 PDF 查看器的缺陷归咎于 iTextSharp。
-
我也在 pdf.js 和 chrome 浏览器中测试过
-
所以您已经确定 Chrome PDF 查看器和 pdf.js 都完全忽略了标记注释。您是否询问过 pdf.js 和 Chrome 的开发人员该诊断是否正确?您是否询问过他们计划何时解决该问题?
-
我参考了这篇文章stackoverflow.com/questions/29032422/… 他们说你已经部分得到了答案,只是那些 PDF 渲染器不完全支持整个 PDF 语法。具体来说,(这只是一个有根据的猜测)似乎这些渲染需要为这些注释存在一个外观条目。
-
好的,所以你有你的答案。现在只需等待 Chrome 和 pdf.js 开发人员通过正确实施 ISO-32000-1 来满足您的要求。
标签: c# itextsharp syntax-highlighting