You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Jean Couteau <co...@codelutin.com> on 2011/09/27 09:26:28 UTC

Trying to fill a PDF form and then make it readonly

Hi all,

I am editing prefectly well a PDF form created using LibreOffice. But I
am trying to improve that unsuccessfully.

What I am trying to do is, when I finished editing my PDF, I want to
make the fields readonly so that my users can get the PDF, print it
without modifications, sign it and then send it back to me.

I am filling my fields like that :

InputStream emptyForm006 = null;
        PDDocument pdDocument;
        try {
            emptyForm006 = getClass().getResourceAsStream("/form006.pdf");
            pdDocument = PDDocument.load(emptyForm006);
            SetField fields = new SetField();
            if (log.isTraceEnabled()) {
                log.trace("fields in empty form 006 are:");
                PrintFields printFields = new PrintFields();
                printFields.printFields(pdDocument);
            }

            for (Map.Entry<String, String> field :
getPdfFieldValues().entrySet()) {
                String fieldName = field.getKey();
                String fieldValue = field.getValue();
                fields.setField(pdDocument, fieldName, fieldValue);
            }

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            pdDocument.save(out);
            pdDocument.close();

            return new ByteArrayInputStream(out.toByteArray());
        } catch (IOException e) {
            throw new NFLogementTechnicalException(e);
        } catch (COSVisitorException e) {
            throw new NFLogementTechnicalException(e);
        } finally {
            Closeables.closeQuietly(emptyForm006);
        }

I was trying to create a new AccessPermission to put the form as
readonly before saving it, without success.

I was trying to do that :

AccessPermission ap = pdDocument.getCurrentAccessPermission();
            ap.setCanModify(false);
            ap.setCanAssembleDocument(false);
            ap.setCanExtractContent(false);
            ap.setCanFillInForm(false);
            ap.setCanModifyAnnotations(false);
            ap.setReadOnly();
            StandardProtectionPolicy policy = new
StandardProtectionPolicy("cerqual", null, ap);
            try {
                pdDocument.protect(policy);
            } catch (BadSecurityHandlerException eee) {
                log.error("Could not protect PDF document");
            }

But I got no error log and I can still modify my pdf file.

I have search this mailing list archive, found a couple of threads
talking about that but with no success for my case.

Is there a way to easily make the file or the fields readonly ?

Best regards,
Jean


-- 
Jean Couteau
--
Code Lutin
44 Boulevard des Pas Enchantés
44230 Saint Sébastien sur Loire
--
Tel : 02 40 50 29 28
Port : 06 68 07 29 29
Fax : 09 59 92 29 28