Copy PDF page without fields

Is there a way to copy PDF page without form fields, i.e. canvas only?

Also, after page with fields is copied we can’t get fields on the new page via pdf.getForm().getFields(), but fields still presented in output PDF file
Hi John,

Thanks for using our API's.

We are working on testing the scenario based on your requirements and will keep you updated with our findings.

Hi John,


OneVizion_VENDOR:
Is there a way to copy PDF page without form fields, i.e. canvas only?

As per my understanding, you need to copy only text of the page to new page, if it is the requirement then you can achieve this by copying the TextFragment of the page and insert into new page. But I am afraid it is not working as expected, so I have logged an investigation ticket PDFJAVA-36066 in our issue tracking system for further investigation and rectification.

However if there is some difference in your requirement and my understanding then please share some more details, so we will provide you information accordingly.

OneVizion_VENDOR:
Also, after page with fields is copied we can’t get fields on the new page via pdf.getForm().getFields(), but fields still presented in output PDF file
As the new fields are being copied as duplicate copy of original fields so you are getting only 3 form fields.

We are sorry for the inconvenience caused.

Best Regards,

We need to copy not only text of the page, but also all images and other graphics if any.


pdf.getPages().insert should work for us, because in theory we may delete all fields from the new page after it is inserted, but we have no access to those new fields, and field.size() return same size as before page insert, so we don’t have access to the new annotations either

Hi John,


Thanks for providing additional information. Yes, if you want to copy all page resources instead only text then you can use insert() method and delete form field later.

Please note when we insert existing page then form fields are being added as Form field annotation and currently Aspose.Pdf is not deleting specific Form field Annotation. However we are already investigating the issue as per your other related thread.

We are sorry for the inconvenience.

Best Regards,

Hi John,

Thanks for your patience.

We have further investigated earlier reported issue PDFJAVA-36066 and in order to resolve this problem, please try using following code snippet.

[Java]

public static void main(String[] args) {

    initLicense();

    String myDir = "F:/LocalTesting/";

    Document pdf = new Document();
    com.aspose.pdf.Page page1 = pdf.getPages().insert(1);

    System.out.println("Page 1 created.........");
    System.out.println("Fields count=" + pdf.getForm().getFields().length);

    for (Field field : pdf.getForm().getFields()) {
        System.out.println("Field name=" + field.getFullName() + " size=" + field.size());
    }

    TextFragment text = new TextFragment("Pdf form");
    page1.getParagraphs().add(text);

    TextBoxField field1 = new TextBoxField(pdf, new Rectangle(0, 0, 100, 100));
    field1.setPartialName("FIELD1");
    pdf.getForm().add(field1, pdf.getPages().get_Item(1).getNumber());

    TextBoxField field2 = new TextBoxField(pdf, new Rectangle(0, 150, 100, 250));
    field2.setPartialName("FIELD2");
    pdf.getForm().add(field2, pdf.getPages().get_Item(1).getNumber());

    TextBoxField field3 = new TextBoxField(pdf, new Rectangle(0, 300, 100, 400));
    field3.setPartialName("FIELD3");
    pdf.getForm().add(field3, pdf.getPages().get_Item(1).getNumber());

    System.out.println("Fields inserted into page 1.........");
    System.out.println("Fields count=" + pdf.getForm().getFields().length);

    for (Field field : pdf.getForm().getFields()) {
        System.out.println("Field name=" + field.getFullName() + " size=" + field.size());
    }

    pdf.processParagraphs();

    TextFragmentAbsorber absorber = new TextFragmentAbsorber();
    absorber.visit(pdf.getPages().get_Item(1));

    // Get the extracted text fragments into the collection
    TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

    TextFragment[] fragments = new TextFragment[absorber.getTextFragments().size()];

    System.out.println("TextFragments " + absorber.getTextFragments().size());

    for (int i = 0; i < textFragmentCollection.size(); i++) {
        fragments[i] = copyTextWithSegments(textFragmentCollection.get_Item(i + 1));
    }

    com.aspose.pdf.Page page = pdf.getPages().insert(2);
    TextBuilder b = new TextBuilder(page);

    for (int i = 0; i < fragments.length; i++) {
        b.appendText(fragments[i]);
    }

    pdf.save(myDir + "testform_1180_codeSnippet2.pdf");
    System.out.println("Done");
}

private static TextFragment copyTextWithSegments(TextFragment tf) {
    TextFragment newTF = new TextFragment();

    for (TextSegment ts : tf.getSegments()) {
        TextSegment newTS = new TextSegment();
        newTS.setText(ts.getText());
        newTS.setPosition(ts.getPosition());
        newTS.setTextState(ts.getTextState());
        newTF.getSegments().add(newTS);
    }

    newTF.getTextState().applyChangesFrom(tf.getTextState());
    return newTF;
}