Wednesday, December 5, 2007

Getting started with iText - Part 9 (HelloWorldMeta.java)

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


Source: http://itextdocs.lowagie.com/tutorial/general/
Documentation: See Step 3 for more information
Example: HelloWorldMeta.java

Drum roll please ...




<h1>Hello World Metadata</h1>
<cfscript>
    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", "java.io.FileOutputStream").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");
        document.open();
    
        // step 4: we add a paragraph to the document
        paragraph = createObject("java", "com.lowagie.text.Paragraph").init("Hello World");
        document.add(paragraph);
    
        WriteOutput("Finished!");
    }
    catch (com.lowagie.text.DocumentException de) {
        savedErrorMessage = de;
    }
    catch (java.io.IOException ioe) {
        savedErrorMessage = ioe;
    }
    // step 5: we close the document
    document.close();
</cfscript>


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

4 comments:

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?
http://itextpdf.sourceforge.net/howtosign.html

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:

cfscript
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);

document=javaLoader.create("com.lowagie.text.Document");

pdfReader=javaLoader.create("com.lowagie.text.pdf.PdfReader");

pdfStamper=javaLoader.create("com.lowagie.text.pdf.PdfStamper");

PdfSignatureAppearance=javaLoader.create("com.lowagie.text.pdf.PdfSignatureAppearance");

inputStream=createObject("java","java.io.FileInputStream");

outStream=createObject("java","java.io.FileOutputStream");

writer=javaLoader.create("com.lowagie.text.pdf.PdfWriter");

KeyStore=createObject("java","java.security.KeyStore");

key=createObject("java","java.security.PrivateKey");

String=createObject("java","java.lang.String");

ks = KeyStore.getInstance("pkcs12");

ks.load(inputStream.init(pathPFX),"paRRTQPa9");

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

This is the code presented in that tutorial(http://itextpdf.sourceforge.net/howtosign.html):
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");
sap.setLocation("Lisbon");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
stp.close();

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.

http://cfsearching.blogspot.com/2007/12/getting-started-with-itext-how-to-sign.html

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep