NamedActionsPDF.java Source code

Java tutorial

Introduction

Here is the source code for NamedActionsPDF.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfWriter;

public class NamedActionsPDF {
    public static void main(String[] args) {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NamedActionsPDF.pdf"));
            document.open();
            Paragraph p = new Paragraph(
                    new Chunk("Click to print").setAction(new PdfAction(PdfAction.PRINTDIALOG)));

            p.add(new Phrase(new Chunk("First Page").setAction(new PdfAction(PdfAction.FIRSTPAGE))));
            p.add(new Phrase(new Chunk("Prev Page").setAction(new PdfAction(PdfAction.PREVPAGE))));
            p.add(new Phrase(new Chunk("Next Page").setAction(new PdfAction(PdfAction.NEXTPAGE))));
            p.add(new Phrase(new Chunk("Last Page").setAction(new PdfAction(PdfAction.LASTPAGE))));

            for (int i = 1; i <= 10; ++i) {
                document.add(new Paragraph("This is page " + i));
                document.add(new Paragraph(Chunk.NEWPAGE));
                document.add(p);
                document.newPage();
            }
        } catch (Exception de) {
            de.printStackTrace();
        }
        document.close();
    }
}