使用Java操作PDF文档 博客分类: 编程语言 JavaPdfItext
程序员文章站
2024-03-25 15:45:16
...
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(); } } }
推荐阅读
-
Java操作pdf 博客分类: 行业应用 javapdfitext
-
java 使用itext分割pdf 博客分类: java javapdfitext
-
使用Java操作LDAP案例 博客分类: J2EE案例文档 javaldapUnboundid SDK
-
java 注解的几大作用及使用方法详解(完) 博客分类: 编程语言 注解 java 注解
-
java 注解的几大作用及使用方法详解(完) 博客分类: 编程语言 注解 java 注解
-
JAVA: 为什么要使用"抽象类"? 使用"抽象类"有什么好处? 博客分类: JAVAC#抽象类,语言 javac#编程基础类
-
JAVA: 为什么要使用"抽象类"? 使用"抽象类"有什么好处? 博客分类: JAVAC#抽象类,语言 javac#编程基础类
-
java使用POI操作Word文档,写入文字与图片 博客分类: JAVA java操作WordApache POIjava使用POI操作Word文档java操作Word文档写入文字与图片