itextpdf導出pdf

    1. 所需依賴
<dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itextpdf</artifactId>
      <version>5.2.0</version>
    </dependency>
    <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>itext-asian</artifactId>
      <version>5.2.0</version>
    </dependency>
    <dependency>
      <groupId>cn.lesper</groupId>
      <artifactId>iTextAsian</artifactId>
      <version>3.0</version>
    </dependency>
  • 2.導出示例

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;

public class PDFUtil {
    private static final Float ROWSPACING = 20f;//行距
    private static final Float HEADINGSPACING = 30f;//標題間距

    /**
     * 創建PDF文檔
     */
    public static String createPDF(PdfVo pdfVo,String pdfPath) throws Exception {

        //子目錄
        String subDir= pdfVo.getDerivedRecordId();
        //文件名
        String fileName= pdfVo.getPatientRegisterNo();
        //輸出路徑
        String outDir = pdfPath+ "/" + subDir+ "/";
        String outPath = outDir + fileName+ ".pdf";

        //設置紙張
        Rectangle rect = new Rectangle(PageSize.A4);

        //創建文檔實例
        Document doc = new Document(rect);

        //添加中文字體
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        //設置字體樣式
        Font textFont = new Font(bfChinese, 13, Font.NORMAL); //正常
        Font redTextFont = new Font(bfChinese, 11, Font.NORMAL); //正常,紅色
        Font boldFont = new Font(bfChinese, 11, Font.BOLD); //加粗
        Font redBoldFont = new Font(bfChinese, 11, Font.BOLD); //加粗,紅色
        Font firsetTitleFont = new Font(bfChinese, 22, Font.BOLD); //一級標題
        Font secondTitleFont = new Font(bfChinese, 15, Font.BOLD); //二級標題
        Font underlineFont = new Font(bfChinese, 13, Font.UNDERLINE); //下劃線斜體

        Chunk separatedBySemicolon = new Chunk(";", textFont);
        Chunk separatedByDots = new Chunk(".", textFont);
        Chunk separatedBySpace = new Chunk("  ", textFont);
        Chunk spaceUnderlineShout = new Chunk("        ", underlineFont).setCharacterSpacing(3);
        Chunk spaceUnderlineMedium = new Chunk("        ", underlineFont).setCharacterSpacing(4);
        Chunk spaceUnderlineLong = new Chunk("          ", underlineFont).setCharacterSpacing(8);
        //創建輸出流
        File dir = new File(outDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        PdfWriter.getInstance(doc, new FileOutputStream(new File(outPath)));

        doc.open();
        doc.newPage();

        //段落
        Paragraph p1 = new Paragraph();
        p1.setLeading(ROWSPACING);
        //短語
        Phrase ph1 = new Phrase();

        p1 = new Paragraph();
        p1.setLeading(ROWSPACING);
        ph1 = new Phrase();
        Chunk c8_1 = new Chunk("身份證號碼:", textFont);
        Chunk c8_2 = null;
        if (!StringUtils.isEmpty(pdfVo.getIdCard())) {
            c8_2 = new Chunk(pdfVo.getIdCard(), underlineFont);
        } else {
            c8_2 = spaceUnderlineLong;
        }
        ph1.add(c8_1);
        ph1.add(c8_2);
        p1.add(ph1);
        doc.add(p1);

        //第九行
        p1 = new Paragraph();
        p1.setLeading(ROWSPACING);
        ph1 = new Phrase();
        Chunk c9_1 = new Chunk("戶口所在地:", textFont);
        Chunk c9_2 = null;
        if (!StringUtils.isEmpty(pdfVo.getMorbidityResidence())) {
            c9_2 = new Chunk(pdfVo.getMorbidityResidence(), underlineFont);
        } else {
            c9_2 = spaceUnderlineLong;
        }
        ph1.add(c9_1);
        ph1.add(c9_2);
        p1.add(ph1);
        doc.add(p1);
//三個下劃線
        p1 = new Paragraph();
        p1.setLeading(ROWSPACING);
        ph1 = new Phrase();
        Chunk c25_1 = new Chunk("類型1/類型2/類型3:", textFont);
        Chunk c25_3 = new Chunk("、", textFont);
        Chunk c25_5 = new Chunk("、", textFont);
        ph1.add(c25_1);
        ph1.add(spaceUnderlineLong);
        ph1.add(c25_3);
        ph1.add(spaceUnderlineLong);
        ph1.add(c25_5);
        ph1.add(spaceUnderlineLong);
        p1.add(ph1);
        doc.add(p1);
 //日期格式處理
        p1 = new Paragraph();
        p1.setLeading(ROWSPACING);
        ph1 = new Phrase();
        Chunk c31_1 = new Chunk("日期:", textFont);
        Chunk c31_3 = new Chunk("年", textFont);
        Chunk c31_5 = new Chunk("月", textFont);
        Chunk c31_7 = new Chunk("日;", textFont);
        ph1.add(c31_1);
        ph1.add(spaceUnderlineShout);
        ph1.add(c31_3);
        ph1.add(spaceUnderlineShout);
        ph1.add(c31_5);
        ph1.add(spaceUnderlineShout);
        ph1.add(c31_7);
        p1.add(ph1);
        doc.add(p1);
        //寫出
        doc.close();
        return outPath;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章