Wednesday, December 5, 2007

Getting started with iText - Part 9 (

The next installation in Getting started with iText translates the final example in the Part I: It demonstrates how to add metadata to a PDF document.

Documentation: See Step 3 for more information

Drum roll please ...

<h1>Hello World Metadata</h1>
    savedErrorMessage = "";
    // by default outputs to current directory. change as needed
    fullPathToOutputFile = ExpandPath("./HelloWorldMeta.pdf");
    // step 1: creation of a document-object
    document = createObject("java", "com.lowagie.text.Document").init();

    try {
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
        outStream = createObject("java", "").init(fullPathToOutputFile);
        writer = createObject("java", "com.lowagie.text.pdf.PdfWriter").getInstance(document, outStream);

        // step 3: we add some metadata open the document
        document.addTitle("Hello World example");
        document.addSubject("This example explains how to add metadata.");
        document.addKeywords("iText, Hello World, step 3, metadata");
        document.addCreator("My program using iText");
        document.addAuthor("Bruno Lowagie");;
        // step 4: we add a paragraph to the document
        paragraph = createObject("java", "com.lowagie.text.Paragraph").init("Hello World");
    catch (com.lowagie.text.DocumentException de) {
        savedErrorMessage = de;
    catch ( ioe) {
        savedErrorMessage = ioe;
    // step 5: we close the document

<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
    Error - unable to create document
    <cfdump var="#savedErrorMessage#">


Web Specialist December 6, 2007 at 12:42 AM  

Hey, cool this iText/CF series. Do you think about an example to certificate a PDF file with iText?

cfSearching December 6, 2007 at 8:27 PM  

Thanks :) I have not really done much with certificates yet. Is this what you had in mind?

Let me know. I will be happy to give it a shot. I always enjoy learning new tricks.

Web Specialist December 7, 2007 at 3:17 AM  

Yes. Exactly. I'm using this tutorial and port to CF world. But I don't know which java package I need to instantiate. Please look my code:

pathPFX = ExpandPath('assinatura_Marco.pfx');

paths = arrayNew(1);
paths[1] = application.dir_jar & 'iText-2.0.7.jar';
javaLoader = createObject('component', '#application.dir_com#.javaloader.JavaLoader').init(paths);











ks = KeyStore.getInstance("pkcs12");


alias = ks.aliases().nextElement();
// ks.getKey(alias, "paRRTQPa9"); // BOTTLENECK

This is the code presented in that tutorial(
OK KeyStore ks = KeyStore.getInstance("pkcs12");
OK ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray());
OK String alias = (String)ks.aliases().nextElement();
BOTTLENECK PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader("original.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.setReason("I'm the author");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);

cfSearching December 8, 2007 at 4:13 AM  

I still have not figured out how to make the signature visible, but give this a try.

  © Blogger templates The Professional Template by 2008

Header image adapted from atomicjeep