How do you clone a PDF Page

Can anyone tell me how to clone a page and add it to the end of the document?

Background: We are evaluating varisou PDF products. We need to populate existing PDF Forms with data from our database. Sometimes there is more data than will fit on a single page, so we clone the page, change the field names and continue populating the form. We can do this with iTextSharp and PDFNet, but cannot figure out how to do it with Aspose.

Hi There,

Thanks for contacting support.

You can clone a particular page by adding it in the Pages Collection of the Document. Please check the following code snippet to add existing PDF page at the end of the document.

Document doc = new Document(dataDir + "inuput.pdf");

// Add existing page into Pages Collection
doc.Pages.Add(doc.Pages[1]);

// Save the document
doc.Save(dataDir + "PdfWithClonedPage.pdf");

In case if you have any other query please feel free to ask.

Best Regards,

Thank you for your response. Unfortunately the code sample you posted works as far the final printed product, but it does not update the document’s form. Thus, the fields on the page are not cloned and are not appended to the Document.Form object. We need the Form Fields cloned so we can update their value programmatically. We also had problems with the Annotations not being updated, so in order to clone a page we had to do the following. If you know a more efficient way, that would be great.
// m_forDataStream is a memory stream of an existing PDF document

AP.Facades.PdfFileEditor pdfEditor = new AP.Facades.PdfFileEditor();

using (MemoryStream pageToCloneStream = new MemoryStream())
using (MemoryStream renamedPageToCloneStream = new MemoryStream())
{
    pdfEditor.Extract(m_formDataStream, 1, 1, pageToCloneStream);

    using (AP.Document tempDocument = new AP.Document(pageToCloneStream))
    {
        (tempDocument.Form[1] as AP.Forms.Field).PartialName = $"Page{newPageNumber}";
        tempDocument.Save(renamedPageToCloneStream);
    }

    MemoryStream output = new MemoryStream();
    pdfEditor.Append(m_formDataStream, renamedPageToCloneStream, 1, 1, output);

    Document pdfDoc = new AP.Document(output);

    m_formDataStream.Dispose();
    m_formDataStream = null;
    m_formDataStream = output;
}

Hi There,


Thanks for contacting support and sharing details about updating Annotations Collection after cloning the page. I have tried to clone a page containing forms and noticed that the Fields Collection was not updated after adding a duplicate page. Therefore I have generated a ticket with my sample document(s) as PDFNET-42359 in our issue tracking system against the issue. We will further investigate the issue and keep you updated on the status.

We would really appreciate if you please share your input/output document(s). It will really help us to investigate the issue in details and get back to you with some definite information. We are sorry for the inconvenience.


Best Regards,