Concatenating new PDFs throws ArgumentOutOfRangeException

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()?

@grant.bowering

We have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as PDFNET-51491. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

1 Like

Addendum to this:

(my actual code only experiences this if the first one of the documents being concatenated is missing, otherwise it seems to be fine)

Actually that’s not correct; if they’re being concatenated to an existing document, nothing crashes and the pages insert fine, but there’s no content on them, it’s just a blank page. Since that’s not the case if these generated pages are saved instead of concatenated, I suspect this might be related.

@grant.bowering

We have logged this detail in our issue tracking system. We will be sure to inform you via this forum thread once there is an update available on it.