`
bjtale
  • 浏览: 28720 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

使用Java操作PDF文档

阅读更多

1.文档内容的基本格式设置

    示例代码:

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();
		}
	}
}

 关键要点:

    1)设置字体,使用BaseFont创建基准字体,通过Font类来格式化具体显示内容的字体。常见格式包括:加粗、斜体、下划线、删除线等。

    2)文档块Chunk,使用Chunk类创建一个文档块用于显示在PDF文档中,这些文档块是可以在同一行内并列显示的。

 

    3)当PDF文档中显示非英文字符时,需要使用BaseFont类的静态方法createFont(String name,String encoding,boolean embedded)来对文字进行重新编码指定。处理中文时name为具体的中文字体名称,encoding值为UniGB-UCS2-H,embedded值使用字体常量NOT_EMBEDDED。

2.对文档添加页码

    示例代码:

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();
		}
	}
}

     以上程序先读取了一个pdf文件(也可以通过程序创建),位于C盘根路径下。添加页码时,通过PdfReader类来读取这个文档,并通过PdfStamper类来完成页码添加,输出处理后的文档为test_pre.pdf。

    关键API:

    1)public int com.lowagie.text.pdf.PdfReader.getNumberOfPages(),该方法返回reader读取文档的总页数。

    2)使用PdfStamper获取当前页面底部内容,进行页码添加,并使用setTextMatrix(float x,float y)方法设置页码的显示位置。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics