I get an exception thrown up from inside obfuscated Aspose code if I try to just create a couple new PDFs and smush them together.
Here’s a 30-line reproduction I ran in LINQPad referencing Aspose.PDF 22.2:
void Main()
{
var doc1 = GetErrorPage("testing");
var doc2 = GetErrorPage("also testing");
AppendPdf(ref doc1, doc2);
doc1.Save("example.pdf");
}
public Document GetErrorPage(string errorText)
{
var doc = new Document();
var page = doc.Pages.Add();
var text = new TextFragment($"[Error: {errorText}]");
page.Paragraphs.Add(text);
return doc;
}
public void AppendPdf(ref Document ongoingPdfBuildup, Document documentToAppend)
{
if (ongoingPdfBuildup == null)
{
// this is the first document in this PDF, so just set it directly
ongoingPdfBuildup = documentToAppend;
}
else
{
// append the document
var editor = new PdfFileEditor();
editor.Concatenate(new[] { documentToAppend }, ongoingPdfBuildup);
}
}
Resulting in:
image.png (25.8 KB)
ArgumentOutOfRangeException
Index was out of range. Must be non-negative and less than the size of the
collection. (Parameter 'index')
at Aspose.Pdf.PageCollection.#=zSFF5AGMwLxTiLE$pRSQCiZs=(Int32 #=zvLH6zsk=)
at Aspose.Pdf.PageCollection.#=zfIDpZtA=(Int32 #=zvLH6zsk=)
at Aspose.Pdf.Document.ProcessParagraphs()
at Aspose.Pdf.Document.#=zG7VhpvNMzChd(Stream #=z4_mitY8=, SaveOptions
#=zDrH$9qVYCr8I)
at Aspose.Pdf.Document.#=zQg2rMssyUtgB(String #=zYiXckzBWQh06)
at Aspose.Pdf.Document.Save(String outputFileName)
There’s nothing wrong with the output of GetErrorPage
; if I just call Save, the resulting file works and looks like I’d expect.
It also works if I append one of these to a PDF created in some other way (my actual code only experiences this if the first one of the documents being concatenated is missing, otherwise it seems to be fine). So I think there’s something in PageCollection that isn’t getting properly set by Pages.Add()?