0

I'm using pdfbox for flatenning pdf but after calling acroForm.flatten() images are not visible in the resulting PDF.

I have tried all the suggestions available on the internet removed the acrofields, removed widget no of them work

List<InputStream> docs = new ArrayList<>();
for (String id : imageIdentifiers) {
    Optional<Map> o = images.stream()
        .filter(i ->id.equalsIgnoreCase((String) i.get("IMAGE_TYPE")))
        .findFirst();
    if (o.isPresent()) {
        try {
            PDDocument document = PDDocument.load(new File(template));
            PDAcroForm form = document.getDocumentCatalog().getAcroForm();                       
            List<PDField> fields = form.getFields();                         
            List<PDField> txtFields = fields.stream()
                .filter(textField)
                .collect(Collectors.toList());                           
            for (PDField field : txtFields) {                                
                String value = getValue(values, field.getPartialName(), secondPassFields);                               
                field.setValue(value);                               
                field.setReadOnly(true);
            }
            PDField imageField = fields.stream()
                .filter(imgField)
                .findFirst()
                .get();
            fillImage(document, imageField, (String) o.get().get("IMAGE_URL"));                          


            //document.getDocumentCatalog().getAcroForm().
            setNeedAppearances(false);
            //document.getDocumentCatalog().getAcroForm().flatten();

            document.getDocumentCatalog().getAcroForm().setFields(Collections.<PDField>emptyList());
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();                
            document.save(outputStream);                         
            document.close();
            InputStream in = new ByteArrayInputStream(outputStream.toByteArray());
            docs.add(in);
        } catch (Exception e) {
            throw e;
        }
    }
}

I want the pdf to be uneditable anyhow whether flatten or not

Jakub Ch.
  • 3,577
  • 3
  • 24
  • 43
Nishant
  • 1
  • 3
  • 1
    Please share your sample PDF. Your issue might be related to the one described at the end of [this answer](https://stackoverflow.com/a/54091766/1729265) for which meanwhile the PDFBox issue [PDFBOX-4430](https://issues.apache.org/jira/browse/PDFBOX-4430) has been generated. – mkl Jan 11 '19 at 13:00
  • Re "I want the pdf to be uneditable anyhow whether flatten or not" - How about `PDField.setReadOnly()`? – Tilman Hausherr Jan 13 '19 at 08:41
  • @Tilman Hausherr : I did try that but after that the fields are clickable and they move a little when clicked – Nishant Jan 14 '19 at 09:23

0 Answers0