使用itext生成pdf文件


  1.使用itext生成pdf首先需要引入itext的包,由於我們的項目時maven項目,所以只需在pom.xml中配置即可:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.0</version>
</dependency>
<dependency>
  <groupId>com.itextpdf.tool</groupId>
  <artifactId>xmlworker</artifactId>
  <version>5.5.0</version>
</dependency>

   2.生成pdf文件:

/**
     * 
     * Description:    公共方法:生成pdf文件<br>
     * @return
     */
    public File pdfInfo() throws Exception {
        
        //生成PDF源文件
        File file=new File("畢結業證書.pdf");
        //文檔生成器--PageSize.A4默認A4打印
        //A4的構造方法為new RectangleReadOnly(595, 842),參數    對調即是做橫向展示
        Document document=new Document(new RectangleReadOnly(842, 595));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        for(DiplomaPrintQB qb:list) {
            PdfContentByte cb = writer.getDirectContent();  
            /*BaseFont chineseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);*/
            String font=getFont(zzBydypz.getXzt());//該處調用getFont方法,獲取需要使用字體
        //設置pdf字體 BaseFont chineseFont
= BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(chineseFont, Integer.parseInt(zzBydypz.getXdx())); //遍歷xxh數組,確定字段絕對位置 for(int i=0;i<xxh.length;i++) {
          //獲取需要打印的內容 String value
=getContent(xxh[i],qb); //添加學生照片 if(xxh[i].equals("24")) { if(StringUtils.isBlank(value)){ HttpServletRequest request=ServletActionContext.getRequest();
              //獲取系統自帶默認照片 String path
= request.getSession().getServletContext().getRealPath("/"); value=path+"images/new/img-show.png"; } Image image=Image.getInstance(value); image.setAbsolutePosition(Integer.parseInt(hzb[i]), (450-Integer.parseInt(zzb[i]))); cb.addImage(image); }else{ cb.showTextAligned(PdfContentByte.ALIGN_LEFT, value, Integer.parseInt(hzb[i]), (580-Integer.parseInt(zzb[i])), 0); } } cb.endText(); //下一頁(用於生成新的PDF頁面) document.newPage(); } document.close(); return file; }

  3.提供一個下載 pdf的方法:

  

/**
     * 
     * Description:    下載PDF<br>
     * @return
     */
    public void downloadDataPdf() throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream(pdfInfo()));
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/pdf");
        OutputStream os = ResponseUtils.renderExcel(response, "畢結業證書.pdf");
        byte[] readByte = new byte[4096];
        int c;
        while ((c = is.read(readByte)) != -1) {
            os.write(readByte,0,c);
        }
        os.flush();
        if (os != null) {
            os.close();
        }
        if (is != null) {
            is.close();
        }
    }

  4.至此就可以通過瀏覽器下載所需的pdf文件,注:該方法使用的為文字絕對定位方法,由

  cb.showTextAligned(PdfContentByte.ALIGN_LEFT,  value, Integer.parseInt(hzb[i]), (580-Integer.parseInt(zzb[i])), 0);第三個和第四個參數確定文字顯示位置,第三個參數決定橫坐標,第四個決定縱坐標,itextpdf坐標以pdf左下角點位原點


免責聲明!

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



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