danfickle/openhtmltopdf

Add PDF properties like owner, security...

juju35 opened this issue · 4 comments

Is it possible to add PDF properties ?
I mean properties like owner, title, subject, security...

OK, I've just added a method to get the PDDocument so you can protect it. The code will look something like:

             PdfRendererBuilder builder = new PdfRendererBuilder();

             // set options on builder...

             PdfBoxRenderer r = builder.buildPdfRenderer();
             AccessPermission p = new AccessPermission();
             p.setCanAssembleDocument(false);
             p.setCanExtractContent(true);
             p.setCanExtractForAccessibility(true);
             p.setCanFillInForm(true);
             p.setCanModify(false);
             p.setCanModifyAnnotations(true);
             p.setCanPrint(false);
             p.setCanPrintDegraded(false);
             p.setReadOnly();

             PDDocument doc = r.getPdfDocument();
             doc.protect(new StandardProtectionPolicy("ownerPassword", "userPassword", p));

             r.layout();
             r.createPDF();
             r.cleanup();

You'll probably have to add the Bouncy Castle dependencies. The protection happens when the document is saved.

You can set the title, author, subject and keywords in the html:

<html>
<head>
<title>The Great American Novel</title>
<meta name="author" content="Herbit"/>
<meta name="subject" content="A tale of mice and men"/>
<meta name="keywords" content="pet, mice, novel"/>
</head>
<body>
....
</body>
</html>

Great thanks !

Thanks Dan. I know there is an issue on this already, but more examples would be very helpful -- I had no idea how to dot this prior to your post.

I tried to protect my PDF document using the AccessPermission / ProtectionPolicy approach @danfickle suggested.

Unfortunately, the document is not protected unless i set everything to false

This does work:
AccessPermission accessPermission = new AccessPermission(); accessPermission.setCanExtractContent(false); accessPermission.setCanModify(false); accessPermission.setCanModifyAnnotations(false); accessPermission.setCanPrint(false); accessPermission.setCanPrintDegraded(false); accessPermission.setCanAssembleDocument(false); accessPermission.setCanExtractForAccessibility(false); accessPermission.setCanFillInForm(false); accessPermission.setReadOnly();

--> The document prevents me from copying selected text and printing etc.

This does not work:
AccessPermission accessPermission = new AccessPermission(); accessPermission.setCanExtractContent(false); accessPermission.setCanModify(false); accessPermission.setCanPrint(true);

--> I am able to do anything with the document. Copying selected text is not being prevented.

Does anybody know what i am missing out?