Add String to pdf document - Java PDF

Java examples for PDF:Document

Description

Add String to pdf document

Demo Code


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.PDFont;

public class PDFWithText {
    public static void main(String[] args) {
        PDDocument doc = null;/*from   w w w  . j av  a2  s. c o m*/
        PDPage page = null;

        try {
            doc = new PDDocument();
            page = new PDPage();

            doc.addPage(page);
            PDFont font = PDType1Font.HELVETICA_BOLD;

            PDPageContentStream content = new PDPageContentStream(doc, page);

            content.beginText();
            content.setFont(font, 12);
            content.moveTextPositionByAmount(100, 700);
            content.drawString("tomcat from phoenixjcam.no-ip.biz:8081");
            content.endText();

            content.close();
            doc.save("pdfbox/pdf/create/Empty PDF.pdf");
            doc.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Related Tutorials