JAva出力PDFドキュメント

13316 ワード

PDF出力はiTextパッケージに使用されます.PDFドキュメントの特殊性のため、ローカルのフォントサポートが必要になる場合があります.Webアプリケーションでは、サーバ側にフォントパッケージを配置する必要がある場合があります.
一般的な出力PDFには2つの方法があります
1つ目は、adobe lifecycle処理を経て、固定されたテンプレートを構築する必要があります.テキストを入力する場所にテキストボックスを追加します.(固定サイズのみ)、テキストボックスの枠線を外し、属性をreadonlyに変更します.pageの名前とテキストボックスのnameを設定することに注意してください.文字があまり多くないことを保証しなければなりません.そうしないと、テキストボックスが完全に表示されません.中国語が表示されない場合は、lifecycleでテキストボックスフォントを中国語フォントに設定してみてください.例コードは以下の通りです.
File file = new File("d:\\aaa.pdf");//      
		OutputStream os = new FileOutputStream(file);//       ,   jsp  ,     response      
		PdfReader reader = new PdfReader("d:/Untitled3.pdf", null); //  pdf  
		PdfStamper ps = new PdfStamper(reader, os); //    PdfStamper  
		AcroFields af = ps.getAcroFields();//   pdf     
		//    
		BaseFont bfchinese = BaseFont.createFont("SIMFANG.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//      
		af.setFieldProperty("Page1.t1", "textfont",bfchinese, null);//    
		af.setFieldProperty("Page1.t1","textsize",11,null); //      
		af.setField("Page1.t1","   "); //       
		
		af.setFieldProperty("Page1.t2", "textfont",bfchinese, null);//    
		af.setFieldProperty("Page1.t2","textsize",11,null); //      
		af.setField("Page1.t2","   "); //       
		
		af.setFieldProperty("Page1.t3", "textfont",bfchinese, null);//    
		af.setFieldProperty("Page1.t3","textsize",11,null); //      
		af.setField("Page1.t3","24"); //       
		//    
		ps.close();
		os.flush();
		os.close();
		//done
		System.out.println("done..");

 
2つ目の方法は、コードから直接PDFファイルを生成することです.操作は少し煩雑で、テンプレートのサポートは必要ありません.サンプルコードは以下の通りです.
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class MyPdfEncoder {

	public static void main(String[] args) throws Exception{
		new MyPdfEncoder().create();
		System.out.println("done...");
	}
	private Document pdfDocument;
	private PdfWriter pdfWriter;
	private OutputStream os;
	private String out = "2011.6     
2011.6 "; /** * */ public void create() throws Exception { pdfDocument = new Document (PageSize.A4);// A4 PDF os = new FileOutputStream(new File("d:\\"+new Date().getTime()+".pdf")); pdfWriter = PdfWriter.getInstance (pdfDocument, os); pdfDocument.setMargins(20,20,100,30);// : pdfDocument.open(); loadTitle(); close(); } /** * PDF * @throws Exception */ public void loadTitle() throws Exception{ // Paragraph titleParagraph = new Paragraph(" ", this.getFont2(16)); titleParagraph.setAlignment(Paragraph.ALIGN_CENTER); titleParagraph.setSpacingAfter(15f);// pdfDocument.add(titleParagraph); PdfPTable table = new PdfPTable(new float[]{90,90,90,90,90,90,90,90}); // table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol("23",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol("001",22,12," ")); // table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellColColSpan("xxx",22,12," ",3)); table.addCell(createCellColColSpan(" ",22,12," ",2)); table.addCell(createCellColColSpan("131111111",22,12," ",2)); // table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol("2011-1-1",22,12," ")); table.addCell(createCellColColSpan(" ",22,12," ",2)); table.addCell(createCellColColSpan("111111",22,12," ",2)); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); // table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellColColSpan(" ",22,12," ",2)); table.addCell(createCellColColSpan("100000",22,12," ",2)); table.addCell(createCellCol(" ",22,12," ")); table.addCell(createCellCol(" ",22,12," ")); // table.addCell(createCellColColSpan(" ",22,12," ",8)); // table.setSplitLate(false);// table.setSplitRows(true);// PdfPCell cellx = createCellContent(out,8,90,10); table.addCell(cellx); // table.addCell(createCellColColSpan("",22,12," ",8)); // table.addCell(createCellColColSpan(" ",22,12," ",3)); table.addCell(createCellColColSpan(" ",22,12," ",3)); table.addCell(createCellColColSpan(" ( )",22,12," ",2)); // table.addCell(createCellColColSpan("1",44,12," ",3)); table.addCell(createCellColColSpan("Perfect!",44,12," ",3)); //9 PdfPTable c9 = new PdfPTable(new float[]{20,70}); c9.addCell(createCellColColSpan("5 4 3 2 1 ",22,8," ",2)); c9.addCell(createCellCol(" ",22,8," ")); c9.addCell(createCellCol("Perfect!",22,8," ")); PdfPCell cell = new PdfPCell(c9); cell.setFixedHeight(44); cell.setLeading(1, 1.2f); cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cell.setVerticalAlignment(PdfPCell.TOP); cell.setColspan(2); table.addCell(cell); // 10 table.addCell(createCellColColSpan("2",44,12," ",3)); table.addCell(createCellColColSpan("Perfect!",44,12," ",3)); //10 PdfPTable c10 = new PdfPTable(new float[]{20,70}); c10.addCell(createCellColColSpan("5 4 3 2 1 ",22,8," ",2)); c10.addCell(createCellCol(" ",22,8," ")); c10.addCell(createCellCol("Perfect!",22,8," ")); PdfPCell cell2 = new PdfPCell(c10); cell2.setFixedHeight(44); cell2.setLeading(1, 1.2f); cell2.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cell2.setVerticalAlignment(PdfPCell.TOP); cell2.setColspan(2); table.addCell(cell2); // 11 table.addCell(createCellColColSpan("3",44,12," ",3)); table.addCell(createCellColColSpan("Perfect!",44,12," ",3)); //11 PdfPTable c11 = new PdfPTable(new float[]{20,70}); c11.addCell(createCellColColSpan("5 4 3 2 1 ",22,8," ",2)); c11.addCell(createCellCol(" ",22,8," ")); c11.addCell(createCellCol("Perfect!",22,8," ")); PdfPCell cell3 = new PdfPCell(c11); cell3.setFixedHeight(44); cell3.setLeading(1, 1.2f); cell3.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cell3.setVerticalAlignment(PdfPCell.TOP); cell3.setColspan(2); table.addCell(cell3); // table.addCell(createCellColColSpan(" ( )
A B C D E",44,12," ",3)); table.addCell(createCellColColSpan(" ",44,12," ",3)); //12 PdfPTable c12 = new PdfPTable(new float[]{20,70}); c12.addCell(createCellCol("
",44,8," ")); c12.addCell(createCellCol("100",44,8," ")); PdfPCell cell4 = new PdfPCell(c12); cell4.setFixedHeight(44); cell4.setLeading(1, 1.2f); cell4.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cell4.setVerticalAlignment(PdfPCell.TOP); cell4.setColspan(2); table.addCell(cell4); pdfDocument.add(table); } /** * * @param text * @param colspan * @param height * @param fontSize * @return */ public static PdfPCell createCellContent(String text,int colspan,int height,int fontSize) { PdfPCell cell = new PdfPCell(new Paragraph(text,getFont2(fontSize))); cell.setMinimumHeight(height);// , cell.setIndent(20f); cell.setLeading(1, 1.2f); cell.setHorizontalAlignment(PdfPCell.LEFT); cell.setVerticalAlignment(PdfPCell.TOP); cell.setColspan(colspan); return cell; } /** * * @param text * @param height * @param fontSize * @param fontType * @param colspan * @return */ public static PdfPCell createCellColColSpan(String text,int height,int fontSize,String fontType,int colspan){ PdfPCell cell = new PdfPCell(new Paragraph(text,getFont(fontType,fontSize))); cell.setFixedHeight(height); cell.setLeading(1, 1.2f); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setColspan(colspan); return cell; } /** * * @param text * @param height * @param fontSize * @param fontType * @return */ public static PdfPCell createCellCol(String text,int height,int fontSize,String fontType) { PdfPCell cell = new PdfPCell(new Paragraph(text,getFont(fontType,fontSize))); cell.setFixedHeight(height); cell.setLeading(1, 1.2f); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); return cell; } /** * * @param type * @param fontSize * @return */ public static Font getFont(String type, int fontSize) { Font font = null; try { if (" ".equals(type)) { BaseFont bf = BaseFont.createFont("SIMFANG.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //TTF , font=new Font(bf, fontSize); }else if (" ".equals(type)) { BaseFont bf = BaseFont.createFont("SIMKAI.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); font=new Font(bf, fontSize); }else if (" ".equals(type)) { BaseFont bf = BaseFont.createFont("SIMSUN.TTC",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); font=new Font(bf, fontSize); }else if (" ".equals(type)) { BaseFont bf = BaseFont.createFont("STZHONGS.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); font=new Font(bf, fontSize); }else if (" ".equals(type)) { BaseFont bf = BaseFont.createFont("STXINWEI.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); font=new Font(bf, fontSize); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return font; } /** * * @throws Exception */ public void close() throws Exception{ pdfDocument.close(); os.flush(); os.close(); } /** * * @param fontSize * @return */ public static Font getFont2(int fontSize){ BaseFont bfchinese; Font font = null; try { bfchinese = BaseFont.createFont("SIMFANG.TTF",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); font = new Font(bfchinese, fontSize); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return font; } /** * * @param fontSize * @return */ private Font getFont(int fontSize){ Font font = new Font(); font.setColor(Color.black); font.setSize(Float.valueOf(String.valueOf(fontSize))); return font; } }