Create or process a PDF fileTag(s): IO Open Source


A nice OpenSource is http://www.lowagie.com/iText/ [ITEXT] . iText can deal with RTF and HTML file too.

Nicely documented with many examples.

  • Create a PDF
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import com.lowagie.text.*;
    import com.lowagie.text.pdf.PdfWriter;
    
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello World");
            Document document = new Document();
            try {
                PdfWriter.getInstance(document,
                        new FileOutputStream("HelloWorld.pdf"));
                document.open();
                document.add(new Paragraph("Hello World"));
            } 
            catch (DocumentException de) {
                System.err.println(de.getMessage());
            } 
            catch (IOException ioe) {
                System.err.println(ioe.getMessage());
            }
            document.close();
        }
    }
    
  • Add a watermark to an existing document.
    import java.io.FileOutputStream;
    import java.util.HashMap;
    
    import com.lowagie.text.Element;
    import com.lowagie.text.Image;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.pdf.PdfContentByte;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    
    public class AddWatermarkPageNumbers {
        public static void main(String[] args) {
            System.out.println("Add watermarks and pagenumbers");
            try {
                PdfReader reader = new PdfReader("ChapterSection.pdf");
                int n = reader.getNumberOfPages();
                // create a stamper that will copy the document to a new file
                PdfStamper stamp = new PdfStamper(reader, 
                   new FileOutputStream("watermark_pagenumbers.pdf"));
                // adding some metadata
                HashMap moreInfo = new HashMap();
                moreInfo.put("Author", "Bruno Lowagie");
                stamp.setMoreInfo(moreInfo);
                // adding content to each page
                int i = 0;
                PdfContentByte under;
                PdfContentByte over;
                Image img = Image.getInstance("watermark.jpg");
                BaseFont bf = 
                   BaseFont.createFont
                     (BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
                img.setAbsolutePosition(200, 400);
                while (i < n) {
                    i++;
                    // watermark under the existing page
                    under = stamp.getUnderContent(i);
                    under.addImage(img);
                    // text over the existing page
                    over = stamp.getOverContent(i);
                    over.beginText();
                    over.setFontAndSize(bf, 18);
                    over.setTextMatrix(30, 30);
                    over.showText("page " + i);
                    over.setFontAndSize(bf, 32);
                    over.showTextAligned
                       (Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
                    over.endText();
                }
                // adding an extra page
                stamp.insertPage(1, PageSize.A4);
                over = stamp.getOverContent(1);
                over.beginText();
                over.setFontAndSize(bf, 18);
                over.showTextAligned(Element.ALIGN_LEFT, 
                        "DUPLICATE OF AN EXISTING PDF DOCUMENT", 30, 600, 0);
                over.endText();
                // adding a page from another document
                PdfReader reader2 = new PdfReader("SimpleAnnotations1.pdf");
                under = stamp.getUnderContent(1);
                under.addTemplate
                    (stamp.getImportedPage(reader2, 3), 1, 0, 0, 1, 0, 0);
                // closing PdfStamper will generate the new PDF file
                stamp.close();
            }
            catch (Exception de) {
                de.printStackTrace();
            }
        }
    }
    See also this HowTo
    blog comments powered by Disqus