<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>
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;
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;
}
}