I have code that modifies a pdf by adding a TextFragment to TextBuilder. It works for most PDFs but for this specific PDF it throws an IndexOutOfRangeException on the line:
textBuilder.AppendText(textFragment);
Any ideas what could be the reason?
I’m using Aspose PDF version 22.7 but also tried and verified on 23.2
File “PDF_1.pdf” is attached, testmethod below:
PDF_1.pdf (4.1 MB)
Thanks,
Karl
public void VerifyBug()
{
var fs = File.OpenRead(@"C:\tmp\PDF_1.pdf");
var contentEditor = new Aspose.Pdf.Facades.PdfContentEditor();
contentEditor.BindPdf(fs);
for (int i = 0; i < contentEditor.Document.Pages.Count; i++)
{
var currentPageNumber = i + 1;
Aspose.Pdf.Page pdfPage = contentEditor.Document.Pages[currentPageNumber];
double maxX = pdfPage.Rect.URX;
maxX -= 50;
//Create text fragment
Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment();
Aspose.Pdf.Text.TextSegment segment1 = new Aspose.Pdf.Text.TextSegment();
segment1.Text = "Indicates R in µm";
segment1.Position = new Aspose.Pdf.Text.Position(maxX - 470, 178);
segment1.TextState.FontSize = 8F;
segment1.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Helvetica");
textFragment.Segments.Add(segment1);
Aspose.Pdf.Text.TextBuilder textBuilder = new Aspose.Pdf.Text.TextBuilder(pdfPage);
textBuilder.AppendText(textFragment);
}
contentEditor.Document.OptimizeResources();
contentEditor.Save(@"C:\tmp\output.pdf");
contentEditor.Dispose();
fs.Dispose();
}