新知識:Java 利用itext填寫pdf模板並導出(昨天奮戰到深夜四點,知道今天兩點終於弄懂)


2019-09-24更新https://www.cnblogs.com/LUA123/p/11580525.html

廢話少說,不懂itext干啥用的直接去百度吧。

***************制作模板*******************

1.先用word做出界面


2.再轉換成pdf格式


3.用Adobe Acrobat 打開你剛剛用word轉換成的pdf


會出現如下界面

下一步

點擊瀏覽,選擇剛才你轉換好的pdf

下一步

4.打開后它會自動偵測並命名表單域,右鍵表單域,點擊屬性,出現文本域屬性對話框,有的人說要改成中文字體,可是我沒有改一樣成功啦

 

5.一般情況下不需要修改什么東西,至少我沒有修改哦


6.直接另存為就行了

************************上程序********************************

准備:itext的jar包包:官網:http://sourceforge.net/projects/itext/files/

因為是利用模板,所以不需要建立字體來解決中文不顯示的問題

public void fillTemplate(){//利用模板生成pdf
                //模板路徑
		String templatePath = "pdfFolder/template_demo.pdf";
                //生成的新文件路徑
		String newPDFPath = "pdfFolder/newPdf.pdf";
		PdfReader reader;
		FileOutputStream out;
		ByteArrayOutputStream bos;
		PdfStamper stamper;
		try {
			out = new FileOutputStream(newPDFPath);//輸出流
			reader = new PdfReader(templatePath);//讀取pdf模板
			bos = new ByteArrayOutputStream();
			stamper = new PdfStamper(reader, bos);
			AcroFields form = stamper.getAcroFields();
			
			String[] str = {"123456789","小魯","男","1994-00-00",
					"130222111133338888"
					,"河北省唐山市"};
			int i = 0;
			java.util.Iterator<String> it = form.getFields().keySet().iterator();
		    while(it.hasNext()){
		    	String name = it.next().toString();
		    	System.out.println(name);
		    	form.setField(name, str[i++]);
		    }
		    stamper.setFormFlattening(true);//如果為false那么生成的PDF文件還能編輯,一定要設為true
		    stamper.close();
		    
		    Document doc = new Document();
		    PdfCopy copy = new PdfCopy(doc, out);
		    doc.open();
		    PdfImportedPage importPage = copy.getImportedPage(
		    		new PdfReader(bos.toByteArray()), 1);
		    copy.addPage(importPage);
		    doc.close();
			
		} catch (IOException e) {
			System.out.println(1);
		} catch (DocumentException e) {
			System.out.println(2);
		}

		
	}                    

模板如圖:

程序結果如圖:

可以看到,少了一個魯......於是我把模板的表單域的字體改成了宋體,結果中文一個也不顯示了,所以我判斷是他那個字體支持的中文比較少吧,先不管這個了

現在都兩點多了(不是下午兩點多啊。。。)

到此時,利用模板生成pdf已經over了,我再說說入門的hello word 級別的程序吧,反正閑着也是閑着

 

程序一:

 1 public void test1(){//生成pdf
 2         Document document = new Document();
 3         try {
 4             PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/1.pdf"));
 5             document.open();
 6             document.add(new Paragraph("hello word"));
 7             document.close();
 8         } catch (FileNotFoundException | DocumentException e) {
 9             System.out.println("file create exception");
10         }
11     }

結果:

可是如果要輸出中文呢,上面這個就不行了,就要用到語言包了

最新亞洲語言包:http://sourceforge.net/projects/itext/files/extrajars/

 1 public void test1_1(){
 2         BaseFont bf;
 3         Font font = null;
 4         try {
 5             bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
 6                     BaseFont.NOT_EMBEDDED);//創建字體
 7             font = new Font(bf,12);//使用字體
 8         } catch (DocumentException | IOException e) {
 9             e.printStackTrace();
10         }
11         Document document = new Document();
12         try {
13             PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/2.pdf"));
14             document.open();
15             document.add(new Paragraph("hello word 你好 世界",font));//引用字體
16             document.close();
17         } catch (FileNotFoundException | DocumentException e) {
18             System.out.println("file create exception");
19         }
20     }

結果如下:

另外一種方法:我不用第三方語言包:

我是在工程目錄里面新建了一個字體文件夾Font,然后把宋體的字體文件拷貝到這個文件夾里面了

上程序:

 1 public void test1_2(){
 2         BaseFont bf;
 3         Font font = null;
 4         try {
 5             bf = BaseFont.createFont("Font/simsun.ttc,1", //注意這里有一個,1
 6                     BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
 7             font = new Font(bf,12);
 8         } catch (DocumentException | IOException e) {
 9             e.printStackTrace();
10         }
11         Document document = new Document();
12         try {
13             PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/3.pdf"));
14             document.open();
15             document.add(new Paragraph("使用中文另外一種方法",font));
16             document.close();
17         } catch (FileNotFoundException | DocumentException e) {
18             System.out.println("file create exception");
19         }
20     }

結果“:

我如果換成:華康少女文字W5(P).TTC,即

bf = BaseFont.createFont("Font/華康少女文字W5(P).TTC,1", //simsun.ttc
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

哈哈,我喜歡圖文並茂的教程02:44:58


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM