package com.yan.exc;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
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 is created by iText!",content));
document.add(new Paragraph("www.samsung.com", content1));
document.add(new Paragraph("www.samsung.com"));
document.add(new Paragraph("there are some words",FontFactory.getFont(FontFactory.HELVETICA,15,Font.UNDERLINE)));
document.add(new Paragraph("These content will be deperated!",
FontFactory.getFont(FontFactory.COURIER,15,Font.NORMAL|Font.STRIKETHRU)));
document.add(new Paragraph("中文内容也可以正常显示",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.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.yan.exc; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JOptionPane; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; public class Pdf06C { public static void main(String[] args) { try { PdfReader reader = new PdfReader("C:\\EJB3.0实例教程.pdf"); int pages = reader.getNumberOfPages(); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("C:\\test_pre.pdf")); BaseFont ch = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); PdfContentByte pager = null; for (int i = 1; i <= pages; i++) { //stamp.get pager = stamp.getUnderContent(i); pager.beginText(); pager.setFontAndSize(ch, 10); pager.setTextMatrix(280, 15); pager.showText("第"+i+"页,共"+pages+"页"); pager.endText(); } stamp.close(); JOptionPane.showMessageDialog(null, "操作已完成!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
扫描二维码关注公众号,回复:
791484 查看本文章