IText实现url转pdf
入门文章:http://www.micmiu.com/opensource/expdoc/itext-pdf-demo/
进阶文章:http://www.micmiu.com/opensource/expdoc/itext-url-pdf/
实现中文的两种方法:
1、
BaseFont base = BaseFont.createFont("fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontCN = new Font(base, 12, Font.NORMAL, BaseColor.BLUE);
document.add(new Paragraph("这是中文:欢迎来到iText世界。", fontCN));
或者:
BaseFont base = BaseFont.createFont("fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontCN = new Font(base, 12, Font.NORMAL, BaseColor.BLUE);
Paragraph p = new Paragraph();
p.setFont(font);
p.add("这是一个段落 hello world");
document.add(p);
2、
Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
document.add(new Paragraph("hello world, 你好a世界", f1));
==================================
写文章记录一下
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
public class IText2PDF {
public static void main(String[] args) throws Exception {
String pdfPath = "E:\\Desktop/iText.pdf";
createFirstPDF(pdfPath);
}
public static void createFirstPDF(String pdfPath) throws Exception {
// 第一步: Create a Document
Document document = new Document(PageSize.A4);
// 第二 步: Get a PdfWriter instance.
PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
// 第三步:Open the Document.
document.open();
// 添加Meta信息
document.addAuthor("Dong Hao");
document.addCreator("Dong Hao");
document.addTitle("URL转换PDF");
document.addSubject("COSMOPlat");
document.addCreationDate();
document.addKeywords("Java,iText");
// 添加Header信息
document.addHeader("blog", "https://blog.csdn.net/weixin_39715012");
document.addHeader("mail", "donghao0205@163.coom");
// 第四步:添加内容
// 添加 Paragraph
document.add(new Paragraph("Hello iText."));
document.add(Chunk.NEWLINE);
// 添加 中文信息
BaseFont base = BaseFont.createFont("fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontCN = new Font(base, 12, Font.NORMAL, BaseColor.BLUE);
document.add(new Paragraph("这是中文:欢迎来到iText世界。", fontCN));
// 第五步:Close the Document.
document.close();
}
}