'iText CopyAsFormXObject PdfException

As I am processing my main pdf file and when I get to page 2 I start to read this Adobe Illustrator. I get this PdfException at the line origPage.CopyAsFormXObject(aiDocument); about the indirect object. I am not sure why I am getting this error because its a different file.

iText.Kernel.Exceptions.PdfException: 'Cannot copy indirect object from the document that is being written.'

const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;
    
string aifilePath = @"PathToAIFile.ai";    
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
   var aiReader = new iText.Kernel.Pdf.PdfReader(aiStream);
   var aiDocument = new PdfDocument(aiReader, new PdfWriter(aimodifiedaiStream));
   PdfPage origPage = aiDocument.GetFirstPage();
   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(aiDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}

Updated with two PdfDocument

const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;

string aifilePath = @"PathToAIFile.ai";    
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
   var aisourceReader = new iText.Kernel.Pdf.PdfReader(aiStream);
   var pdfsourceDocument = new PdfDocument(aisourceReader, new PdfWriter(aimodifiedaiStream));
   var pdftargetDocument = new PdfDocument(aisourceReader);
   PdfPage origPage = aiDocument.GetFirstPage();
   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(aiDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}

Updated with pdftargetDocument has a writer and pdfsourceDocument with just PdfReader still getting the error Cannot copy to document opened in reading mode.'

using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
   {
   var aisourceReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
   var aitargetReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
                
   var pdfsourceDocument = new PdfDocument(aisourceReader);
   var pdftargetDocument = new PdfDocument(aitargetReader, new PdfWriter(aimodifiedaiStream));
   PdfPage origPage = pdfsourceDocument.GetFirstPage();

   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(pdfsourceDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
   pdfsourceDocument.Close();
   pdftargetDocument.Close();
   }


Solution 1:[1]

Summing up my comments as an answer...

You copy a page from the PdfDocument aiDocument as form XObject to the same aiDocument.

Clearly aiDocument is being written, it has a PdfWriter in its constructor. But the iText API only allows copying pages from a PdfDocument constructed without a PdfWriter.

Furthermore, you can only copy to a PdfDocument that is written to, i.e. that is constructed with a PdfWriter. Thus, you cannot copy from a document into itself.

Thus, you need different PdfDocument instances as source and target of the copy process.

The source PdfDocument instance is generated with only a PdfReader, no PdfWriter. The target PdfDocument instance is generated with a PdfWriter. If the target also is generated with a PdfReader depends on your precise use case: Do you want to add the XObject to an existing document, or do you want to add it to an empty document without any prior content.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkl