C# pdfReader引发System.NullReferenceException错误 private void highlightpdfantation(字符串输出文件,int pageno,字符串[]splitText) { 尝试 { PdfReader reader=newpdfreader(System.IO.File.ReadAllBytes(outputFile)); 使用(FileStream fs=newfilestream(outputFile,FileMode.Open,FileAccess.Write,FileShare.None)) { 使用(PdfStamper压模=新PdfStamper(读卡器,fs)) { myLocationTextExtractionStrategy策略=新建myLocationTextExtractionStrategy(); 字符串currentText=PdfTextExtractor.GetTextFromPage(读取器,页码,策略); for(int i=0;i
第四次尝试后,它抛出“对象引用未设置为对象实例。”错误 Stacktrace: 在iTextSharp.text.pdf.PdfReader.PageRefs.ReadPages()上 在iTextSharp.text.pdf.PdfReader.PageRefs..ctor(PdfReader阅读器)C# pdfReader引发System.NullReferenceException错误 private void highlightpdfantation(字符串输出文件,int pageno,字符串[]splitText) { 尝试 { PdfReader reader=newpdfreader(System.IO.File.ReadAllBytes(outputFile)); 使用(FileStream fs=newfilestream(outputFile,FileMode.Open,FileAccess.Write,FileShare.None)) { 使用(PdfStamper压模=新PdfStamper(读卡器,fs)) { myLocationTextExtractionStrategy策略=新建myLocationTextExtractionStrategy(); 字符串currentText=PdfTextExtractor.GetTextFromPage(读取器,页码,策略); for(int i=0;i,c#,pdf,itextsharp,C#,Pdf,Itextsharp,第四次尝试后,它抛出“对象引用未设置为对象实例。”错误 Stacktrace: 在iTextSharp.text.pdf.PdfReader.PageRefs.ReadPages()上 在iTextSharp.text.pdf.PdfReader.PageRefs..ctor(PdfReader阅读器) 在iTextSharp.text.pdf.PdfReader.ReadPages()上 在iTextSharp.text.pdf.PdfReader.ReadPdf()上 在iTextSharp
在iTextSharp.text.pdf.PdfReader.ReadPages()上 在iTextSharp.text.pdf.PdfReader.ReadPdf()上 在iTextSharp.text.pdf.PdfReader..ctor(字节[]pdfIn,字节[] 所有者密码)位于iTextSharp.text.pdf.PdfReader..ctor(字节[]pdfIn) 在底部.highlightpdfantation(字符串输出文件,Int32页码, 字符串[]拆分文本)
它在哪一行代码中抛出此命令?PdfReader reader=newpdfreader(System.IO.File.ReadAllBytes(outputFile));检查
outputFile
变量的值,确保在指定位置可以访问该变量。此行不会引发任何错误。“System.IO.File.ReadAllBytes(outputFile)”。新的PdfReader(仅抛出错误三次我可以突出显示pdf。它在第四次尝试后抛出错误。
private void highlightPDFAnnotation(string outputFile, int pageno, string[] splitText)
{
try
{
PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(outputFile));
using (FileStream fs = new FileStream(outputFile, FileMode.Open, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
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);
highlight.Clear();
}
}
stamper.Close();
stamper.Dispose();
}
fs.Close();
fs.Dispose();
}
reader.Close();
reader = null;
}
catch (Exception ex)
{
throw;
}
}