1. 程式人生 > >itextPdf 可以寫pdf檔案,主要是文字的問題

itextPdf 可以寫pdf檔案,主要是文字的問題

package com.winchampion.credit.bank.modules.preloan.service;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.winchampion.credit.bank.modules.preloan.entity.CreditReportApply; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners
; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.support.DirtiesContextTestExecutionListener; import java.io.*; import java.nio.charset.Charset;
import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"/spring-context.xml"}) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class CreditReportApplyServiceJUnit { @Autowired private CreditReportApplyService creditReportApplyService; @Test public void testFindList() throws Exception { CreditReportApply creditReportApply = new CreditReportApply(); creditReportApply.setSearchCompanyName("AAA"); List<CreditReportApply> list = creditReportApplyService.findList(creditReportApply); System.out.println(list.size()); } @Test public void testPdf() throws Exception{ Document document=new Document(); File file5=new File("C:\\Users\\wc\\Desktop\\test.pdf");//檔案流 if(!file5.exists()){ file5.createNewFile(); };// File file6=new File("C:\\Users\\wc\\Desktop\\hero.html"); OutputStream f1o=new FileOutputStream(file5);//輸出流 InputStream f2=new FileInputStream(file6); PdfWriter writer=PdfWriter.getInstance(document, f1o); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer,document,f2, Charset.forName("UTF-8")); // document5.add(new Paragraph("Hello Hello")); // document5.add(new Paragraph("遼寧省本溪市平山區人民法院")); // document5.add(new Paragraph("遼寧省本溪市平山區人民法院")); // HTMLWorker htmlWorker = new HTMLWorker(document5); // StringBuilder s1=new StringBuilder(); // s1.append("<h1>This is a test!</h1>"); // StringBuilder s2=new StringBuilder(); // s2.append("<p>遼寧省本溪市平山區人民法院</p>"); // document.newPage(); // Paragraph title = new Paragraph("Title"); // // Chapter chapter = new Chapter(title, 1); // // title = new Paragraph("Section A"); // Section section = chapter.addSection(title); // section.setBookmarkTitle("bmk"); // section.setIndentation(30); // section.setBookmarkOpen(false); // section.setNumberStyle( // Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); // // Section subsection = section.addSection(new Paragraph("Sub Section A")); // subsection.setIndentationLeft(20); // subsection.setNumberDepth(1); //document.add(chapter); // document.add(s1); // document.add(s2); // 建立一個Document物件 // document.add(par); // BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", // "UniGB-UCS2-H", false); // String contextPath= // contextPath = request.getContextPath().substring(1);//miss // reportPath = pathString.replaceAll(contextPath, "") + _separator + "CREDIT_REPORT";//轉換到專案的web容器webapps路徑下的imgs // fontBasePath = pathString.replaceAll(contextPath, "") + "fonts"; String fontsPath="/fonts/MSYHL.TTC,1";//總體PDF字型路徑 微軟雅黑 BaseFont bfChinese = BaseFont.createFont(fontsPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //"C:/WINDOWS/Fonts/MSYHL.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED Font bold_fontChinese = new Font(bfChinese, 12, Font.BOLD); Font italic_fontChinese = new Font(bfChinese, 12, Font.ITALIC); Font impressFont = new Font(bfChinese, 16, Font.BOLDITALIC); // 開啟文件,將要寫入內容 document.open(); // 插入一個段落 // Paragraph par = new Paragraph("我們", fontChinese); // document.add(par); String str="\\n遼寧省本溪市平山區人民法院\\n刑 事 判 決 書\\n(2014)平刑初字第00054號\\n公訴機關本溪市平山區人民檢察院。\\n被告人楊某林,男,1989年5月20日出生,漢族,中專文化,……(略)。因涉嫌犯信用卡詐騙罪於2013年11月19日被刑事拘留,同年12月3日被逮捕。現羈押於本溪市看守所。\\n本溪市平山區人民檢察院以本平檢刑訴(2014)57號起訴書指控被告人楊某林犯信用卡詐騙罪,於2014年2月24日向本院提起公訴。本院受理後,依法適用簡易程式,實行獨任審判,於2014年2月27日公開開庭審理了本案。本溪市平山區人民檢察院指派代理檢察員施越卓出庭支援公訴,被告人楊某林到庭參加訴訟。現已審理終結。\\n公訴機關指控,被告人楊某林於2013年3月至4月間,以其朋友張某名義補辦中國工商銀行股份有限公司本溪分行信用卡一張,後惡意透支人民幣49953.25元,經發卡行多次催收超過三個月仍未歸還。案發後,經被害單位報案,公安機關偵查,被告人楊某林被抓獲歸案。贓款全部被揮霍。\\n上述事實,被告人楊某林在開庭審理過程中亦無異議,並有證人張某、代某的證言;被告人楊某林在偵查機關的供述;案件來源及抓捕經過、戶籍證明、信用卡交易明細及催收情況等證據證實,足以認定。\\n本院認為,被告人楊某林以非法佔有為目的,惡意透支,數額較大,經發卡銀行兩次催收後超過三個月仍不歸還,其行為已構成信用卡詐騙罪。公訴機關指控罪名成立。被告人楊某林歸案後如實供述犯罪事實,且在庭審中認罪態度較好,可以從輕處罰。本院為維護國家的金融管理制度,依照《中華人民共和國刑法》第一百九十六條第一款(四)項、第二款,第六十七條第三款,第六十四條之規定,判決如下:\\n一、被告人楊某林犯信用卡詐騙罪,判處有期徒刑二年六個月(刑期從判決執行之日起計算,判決執行以前先行羈押的,羈押一日折抵刑期一日,即自2013年11月19日起至2016年5月18日止),並處罰金人民幣十萬元(罰金自本判決生效後第二日起三個月內繳納)。\\n二、被告人楊某林信用卡詐騙所得的贓款繼續責令退賠給被害單位。\\n如不服本判決,可在接到本判決書的第二日起十日內,通過本院或者直接向本溪市中級人民法院提出上訴。書面上訴的,應交上訴狀正本一份、副本兩份。\\n代理審判員  郭薇薇\\n二〇一四年二月二十七日\\n書 記 員  潘營慧\\n附:相關法律條文\\n《中華人民共和國刑法》\\n第一百九十六條有下列情形之一,進行信用卡詐騙活動,數額較大的,處五年以下有期徒刑或者拘役,並處二萬元以上二十萬元以下罰金;數額巨大或者有其他嚴重情節的,處五年以上十年以下有期徒刑,並處五萬元以上五十萬元以下罰金;數額特別巨大或者有其他特別嚴重情節的,處十年以上有期徒刑或者無期徒刑,並處五萬元以上五十萬元以下罰金或者沒收財產:\\n(一)使用偽造的信用卡,或者使用以虛假的身份證明騙領的信用卡;\\n(二)使用作廢的信用卡的;\\n(三)冒用他人信用卡的;\\n(四)惡意透支的。\\n前款所稱惡意透支,是指持卡人以非法佔有為目的,超過規定限額或者規定期限透支,並且經發卡銀行催收後仍不歸還的行為。\\n盜竊信用卡並使用的,依照本法第二百六十四條的規定定罪處罰。\\n第六十七條犯罪以後自動投案,如實供述自己的罪行的,是自首。對於自首的犯罪分子,可以從輕或者減輕處罰。其中,犯罪較輕的,可以免除處罰。\\n被採取強制措施的犯罪嫌疑人、被告人和正在服刑的罪犯,如實供述司法機關還未掌握的本人其他罪行的,以自首論。\\n犯罪嫌疑人雖不具有前兩款規定的自首情節,但是如實供述自己罪行的,可以從輕處罰;因其如實供述自己罪行,避免特別嚴重後果發生的,可以減輕處罰。\\n第六十四條犯罪分子違法所得的一切財物,應當予以追繳或者責令退賠;對被害人的合法財產,應當及時返還;違禁品和供犯罪所用的本人財物,應當予以沒收。沒收的財物和罰金,一律上繳國庫,不得挪用和自行處理。"; //str.replaceAll("[\\t\\n\\r]",""); str.replaceAll("\\\n",""); System.out.println(str); document.add(new Paragraph(str,bold_fontChinese)); document.add(new Paragraph(" 這是一個空箱子", bold_fontChinese)); document.add(new Paragraph(" 這是一個空箱子", bold_fontChinese)); document.add(new Paragraph(" 這是一個空箱子", bold_fontChinese)); document.close(); f1o.close(); } }