Eine Zahl liegt außerhalb des zulässigen Bereichs, während PDF-Datei mit itextsharp markiert wird
Ich habe das Wort im PDF-Format mithilfe des Codes in der Antwort auf die folgende Frage hervorgehoben: [Hervorheben von Wörtern in einem PDF mit itextsharp, wobei das hervorgehobene Wort im Browser nicht angezeigt wird]2
Aber ich habe "Eine Zahl ist außerhalb des Bereichs Fehler" beim Öffnen dieses pdf.
private void hightlightPDFText()
{
try
{
string[] highlightText = new string[] { "EXTENSIONAL", "geology" };
string inputFile = Server.MapPath("~/pdf/") + "101111j174754572003tb00036x.pdf";
string outputFile = Server.MapPath("~/pdf/") + "101111j174754572003tb00036x_Highlighted.pdf";
// get input document
highlightPDFAnnotation(inputFile, outputFile, highlightText);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
private void highlightPDFAnnotation(string inputFile, string outputFile, string[] highlightText)
{
try
{
PdfReader reader = new PdfReader(inputFile);
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
int pageCount = reader.NumberOfPages;
for (int pageno = 1; pageno <= pageCount; pageno++)
{
string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy);
for (int i = 0; i < highlightText.Length; i++)
{
List<iTextSharp.text.Rectangle> MatchesFound = strategy.GetTextLocations(highlightText[i].Trim(), StringComparison.CurrentCultureIgnoreCase);
foreach (Rectangle rect in MatchesFound)
{
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;
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);
stamper.AddAnnotation(highlight, pageno);
}
}
}
}
}
reader.Close();
File.Copy(outputFile, inputFile, true);
File.Delete(outputFile);
}
catch (Exception ex)
{
throw;
}
}