檔案名稱 | sample.pdf |
描述 | 樣本檔 |
檔案大小 | 23 Kbytes |
下載次數 | 11 次 |
下載 |
/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package com.andowson.pdf;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.lang.StringEscapeUtils;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class HelloWorld {
/** Path to the resulting PDF file. */
public static final String RESULT
= "D:/hello.pdf";
/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
new HelloWorld().createPdf(RESULT);
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// Assumes that source string comes from database
String source = "測試物品-珉彣峯喆献";
// Unescapes the Unicode characters from HTML entities (decimal format) to string
String output = StringEscapeUtils.unescapeHtml(source);
// Specifies the file path to the Chinese font file
BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/kaiu.ttf",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese, 12); // The font size is 12.
// step 4
document.add(new Paragraph(output, font));
// step 5
document.close();
}
}
檔案名稱 | HelloWorld.java |
描述 | 產生PDF的範例檔 |
檔案大小 | 2 Kbytes |
下載次數 | 5 次 |
下載 |
檔案名稱 | hello.pdf |
描述 | 產生的範例pdf檔 |
檔案大小 | 23 Kbytes |
下載次數 | 7 次 |
下載 |