Java Operation PDF document

Source: Internet
Author: User

1. Basic formatting of document content

Example code:


public class Pdf05c {

public static void Main (string[] args) {

Document document = new document ();

try {

Pdfwriter.getinstance (document, New FileOutputStream ("C:\\005.pdf"));

Document.open ();

Basefont font = Basefont.createfont (basefont.times_roman,basefont.cp1252,basefont.not_embedded);

Basefont Font2 = Basefont.createfont (Basefont.helvetica, basefont.cp1252,basefont.not_embedded);

Basefont Font3 = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded);

Basefont.createfont (name, encoding, embedded)

Font chinese = new font (FONT3,15,FONT.NORMAL);

Font content1 = new Font (FONT2, 12,font.bold);

Font content = new Font (font, 16);

Document.add (New Paragraph ("This document was created by itext!", content));

Document.add (New Paragraph ("www.samsung.com", Content1));

Document.add (New Paragraph ("www.samsung.com"));

Document.add (New Paragraph ("There is some words", Fontfactory.getfont (Fontfactory.helvetica,15,font.underline)));

Document.add (New Paragraph ("These content would be deperated!",

Fontfactory.getfont (fontfactory.courier,15,font.normal| (Font.strikethru)));

Document.add (New Paragraph ("Chinese content can also be displayed normally", Chinese));

Chunk uk1 = new Chunk ("Text Chunk1", Fontfactory.getfont (Fontfactory.courier_bold,12,font.italic));

Chunk UK2 = new Chunk ("Text Chunk2", Fontfactory.getfont (Fontfactory.courier_bold,15,font.bold));

Uk2.setbackground (New Color (30, 50, 120));

Document.add (UK1);

Document.add (UK2);

Joptionpane.showmessagedialog (NULL, "document created!");

Document.close ();

} catch (FileNotFoundException e) {

E.printstacktrace ();

} catch (Documentexception e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

}

}

}


Points:

1) Set the font, use Basefont to create a base font, and format the font for the specific display with the font class. Common formats include: bold, italic, underline, strikethrough, and so on.

2) document block chunk, use the chunk class to create a document block for display in a PDF document that can be displayed side-by-side in the same row.

3) When displaying non-English characters in a PDF document, you need to use the static method of the Basefont class CreateFont (String name,string encoding,boolean Embedded) to re-encode the text. When working with Chinese, name is the specific Chinese font name, and the encoding value is unigb-ucs2-h,embedded with the font constant not_embedded.

2. Add page numbers to the document

Example code:

public class Pdf06c {


/**

* @date July 7, 2015 3:19:34

*/

public static void Main (string[] args) {

Document document = new document ();

try {

Pdfwriter.getinstance (document, New FileOutputStream ("C:\\006.pdf"));

Document.open ();

for (int i = 1; i < 5; i++) {

Document.add (New Paragraph ("No.") +i+ "page"));

Document.newpage ();

}

Document.close ();

Pdfreader reader = new Pdfreader ("C:\\006.pdf");

int pages = Reader.getnumberofpages ();

Pdfstamper stamp = new Pdfstamper (reader, New FileOutputStream ("C:\\006_pre.pdf"));

Basefont ch = basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded);

Pdfcontentbyte pager = null;

for (int i = 1; I <= pages; i++) {

Pager = Stamp.getundercontent (i);

Pager.begintext ();

Pager.setfontandsize (CH, 10);

Pager.settextmatrix (280, 15);

Pager.showtext ("+i+" page, Total "+pages+" page ");

Pager.endtext ();

}

Stamp.close ();

Joptionpane.showmessagedialog (NULL, "Operation completed!");

} catch (FileNotFoundException e) {

E.printstacktrace ();

} catch (Documentexception e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

}

}

}


The above program first created a 4-page PDF document 006.pdf, located under the C packing path. When the page number is added, the document is read through the Pdfreader class, and the page number is added through the Pdfstamper class, with the output processed as 006_pre.pdf.

Key API:

1) public int com.lowagie.text.pdf.PdfReader.getNumberOfPages (), which returns the total number of pages that reader reads from a document.

2) Use pdfstamper to get the content at the bottom of the current page, add the page number, and set the display position of the page number using the Settextmatrix (float x,float y) method.

This article from "Notdebug" blog, reproduced please contact the author!

Java Operation PDF document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.