You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by "J.Pietschmann" <j3...@yahoo.de> on 2002/04/28 23:36:49 UTC

PDF encryption

Hi all,
for the archives, I've written a small proof-of-concept
program which couples FOP and iText in order to provide
PDF encryption. Watermarking and everything else, possibly
even Henrik Holle's total page count problem, could be
done in a similar way.
Most of the iText code is directly copied from Lowagie's
enctryption utility.

Home page: http://www.lowagie.com/iText/

   public static void main(String args[]) {
     try {
       ByteArrayOutputStream fopout=new ByteArrayOutputStream();
       FileOutputStream outfile=new FileOutputStream(args[2]);
       Driver driver =new Driver();
       driver.setOutputStream(fopout);
       driver.setRenderer(Driver.RENDER_PDF);
       Transformer transformer=TransformerFactory.newInstance().newTransformer(new StreamSource(new File(args[1])));
       transformer.setParameter("page-count","#");
       transformer.transform(new StreamSource(new File(args[0])), new SAXResult(driver.getContentHandler()));
       PdfReader reader = new PdfReader(fopout.toByteArray());
       int n = reader.getNumberOfPages();
       Document document = new Document(reader.getPageSizeWithRotation(1));
       PdfWriter writer = PdfWriter.getInstance(document, outfile);
       writer.setEncryption(PdfWriter.STRENGTH40BITS, "pdf", null, PdfWriter.AllowCopy);
       document.open();
       PdfContentByte cb = writer.getDirectContent();
       PdfImportedPage page;
       int rotation;
       int i = 0;
       while (i < n) {
         i++;
         document.setPageSize(reader.getPageSizeWithRotation(i));
         document.newPage();
         page = writer.getImportedPage(reader, i);
         rotation = reader.getPageRotation(i);
         if (rotation == 90 || rotation == 270) {
           cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height());
         }
         else {
           cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
         }
         System.out.println("Processed page " + i);
       }
       document.close();
     }
     catch( Exception e) {
       e.printStackTrace();
     }
   }

Have fun
J.Pietschmann


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: PDF encryption

Posted by "David B. Bitton" <da...@codenoevil.com>.
Actually, 128 bit is a ver. 5.0 requirement.  I'm using this exact setup in
my app (iText w/ FOP for encryption), and we are requiring all users to have
AcroReader 5.0 for 128 bit encryption.  Also, if you encrypt at 128 bit, and
then check the encryptions properties of the doc in Reader, you'll see
strength 128 and next to it, 5.0 in parenthesis.
--

David B. Bitton
david@codenoevil.com
www.codenoevil.com

Code Made Fresh DailyT
----- Original Message -----
From: "J.Pietschmann" <j3...@yahoo.de>
To: <fo...@xml.apache.org>
Sent: Sunday, April 28, 2002 5:39 PM
Subject: Re: PDF encryption


> Geez, self-followup:
>  >       writer.setEncryption(PdfWriter.STRENGTH40BITS,
>           "pdf", null, PdfWriter.AllowCopy);
>
> If I set encryption to STRENGTH128BITS, as the original
> had, Acrobat Reader 4.0 complains about "Error while
> decrypting". Probably an export restriction :-(, so be
> careful.
>
> J.Pietschmann
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
> For additional commands, email: fop-dev-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: PDF encryption

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Geez, self-followup:
 >       writer.setEncryption(PdfWriter.STRENGTH40BITS,
          "pdf", null, PdfWriter.AllowCopy);

If I set encryption to STRENGTH128BITS, as the original
had, Acrobat Reader 4.0 complains about "Error while
decrypting". Probably an export restriction :-(, so be
careful.

J.Pietschmann


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org