iText簡介
iText是著名的開放原始碼的站點sourceforge一個專案,是用于生成PDF檔案的一個java類別庫,通過iText不僅可以生成PDF或rtf的檔案,而且可以將XML、Html檔案轉化為PDF檔案,
iText的安裝非常方便,可以在maven中央倉庫找到所需要的版本,只需要pom.xml檔案添加依賴即可,
使用itext生成pdf,所需jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
附一個完整的將資料寫到pdf源代碼
package com.ruoyi.test;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* TODO
*
* @author linfeng
* @date 2021/1/6 14:29
*/
public class PDFTest {
private static Font headFont;// 設定字體大小
private static Font textFont;// 設定字體大小
private static Font paperNameFont;//試卷名稱
static {
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
headFont = new Font(bfChinese, 10, Font.BOLD);// 設定字體大小
textFont = new Font(bfChinese, 8, Font.BOLD);// 設定字體大小
paperNameFont = new Font(bfChinese, 18, Font.BOLD);// 設定字體大小
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//pdf檔案保存路徑
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("F:/test.pdf"));
document.open();
Paragraph paragraph = new Paragraph("成都職業技術學院2019 2020 學年度下期期末考試",headFont);
// 設定文字居中 0靠左 1,居中 2,靠右
paragraph.setAlignment(1);
document.add(paragraph);
//添加換行符
document.add(new Paragraph("\n"));
Paragraph paperName = new Paragraph("2021英語期末考試",paperNameFont);
paperName.setAlignment(1);
document.add(paperName);
document.add(new Paragraph("\n"));
Paragraph propositionPeople = new Paragraph("命題人:林鋒 審核人:陳從亮",headFont);
propositionPeople.setAlignment(1);
document.add(propositionPeople);
document.add(new Paragraph("\n"));
PdfPTable table = createTable(8);
table.addCell(createCell("題號",headFont,Element.AALIGN_CENTER));
table.addCell(createCell("一",headFont,Element.ALIGN_CENTER));
table.addCell(createCell("二",headFont,Element.ALIGN_CENTER));
table.addCell(createCell("三",headFont,Element.ALIGN_CENTER));
table.addCell(createCell("四",headFont,Element.ALIGN_CENTER));
table.addCell(createCell("五",headFont,Element.ALIGN_CENTER));
table.addCell(createCell("六",headFont,Element.ALIGN_CENTER));
table.addCell(createCell("總分",headFont,Element.ALIGN_CENTER));
document.add(table);
PdfPTable table2 = createTable(8);
table2.addCell(createCell("得分",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
table2.addCell(createCell("",headFont,Element.ALIGN_CENTER));
document.add(table2);
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("一、單選題",headFont));
document.add(new Paragraph("\n"));
//題的所有資訊
document.add(new Paragraph("1.【互聯網環境下的典型營銷組合】杜爾于2011年提出移動互聯營銷SoLoMo模式,這一營銷組合重點突出哪些方面?",textFont));
document.add(new Paragraph("A:社交",textFont));
document.add(new Paragraph("B:本地化或精準化",textFont));
document.add(new Paragraph("C:移動",textFont));
document.add(new Paragraph("D:渠道",textFont));
document.close();
writer.close();
}
public static PdfPTable createTable(int colNumber){
PdfPTable table = new PdfPTable(colNumber);
try{
table.setTotalWidth(520);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public static PdfPCell createCell(String value, Font font, int align){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value,font));
return cell;
}
}
檔案已生成

效果圖

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/249056.html
標籤:java
