Learning The "J"

Home » JAVA Learning » Working with PDF

Category Archives: Working with PDF

About iText

iText ® is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.

Developers can use iText to:

Serve PDF to a browser
Generate dynamic documents from XML files or databases
Use PDF’s many interactive features
Add bookmarks, page numbers, watermarks, etc.
Split, concatenate, and manipulate PDF pages
Automate filling out of PDF forms
Add digital signatures to a PDF file
iText is available in Java as well as in C#.

Click Here to read more about iText

Advertisements

Writing PDF files


import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;

public class WritingPDF extends PdfPageEventHelper {

    int i = 0;

    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\Bookmarks.pdf"));

            writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
            document.open();

            document.add(new Paragraph("This is Page 1", new Font(Font.HELVETICA, 12)));
            document.newPage();
            document.add(new Paragraph("This is page 2", new Font(Font.HELVETICA, 12)));
            document.newPage();
            document.add(new Paragraph("This is page 3", new Font(Font.HELVETICA, 12)));
        } catch (Exception de) {
            de.printStackTrace();
        }

        document.close();
    }
    
}

Reading PDF files

Reading PDF Files

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.io.FileWriter;
import java.io.IOException;

public class ReadingPDF {

    public static void main(String ar[]) {
        try {
            PdfReader readerN = new PdfReader("c:\\anchor.pdf");
            for (int i = 1; i <= 1; i++) {
                String myLine = PdfTextExtractor.getTextFromPage(readerN, i);
                System.out.println(myLine);

                try {
                    FileWriter fw = new FileWriter("c://reading.txt", true);
                    fw.write(myLine);
                    fw.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}