itextPdf 可以写pdf文件,主要是文字的问题

来源:互联网 发布:高级java工程师脱产班 编辑:程序博客网 时间:2024/06/05 08:39
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();    }}
0 0
原创粉丝点击