ITextPDF7
ITextPDF
前言
版本說明
itext7-core=7.1.13相關(guān)鏈接:
- itextpdf 官網(wǎng)地址:https://itextpdf.com/en
- itextpdf 官方文檔:https://kb.itextpdf.com/home/it7kb
- itextpdf 官方 github 地址:https://github.com/itext/itext7
- itextpdf maven 地址:https://mvnrepository.com/artifact/com.itextpdf/itext7-core
核心pom依賴
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core --> <dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.1.13</version><type>pom</type> </dependency>入門示例
package top.simba1949;import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.UnitValue;import java.io.File; import java.io.FileNotFoundException;/*** @author Anthony* @date 2020/12/8 10:03*/ public class Application {public static final String FILE_PATH = "D:\\IDE\\IDEA\\workspace\\learn\\test-spring-boot\\itextpdf-learn\\PDF.pdf";public static void main(String[] args) throws FileNotFoundException {// 創(chuàng)建一個(gè)要生成的PDF文件對(duì)象FileFile file = new File(FILE_PATH);// 創(chuàng)建PDF輸出流PdfWriter pdfWriter = new PdfWriter(file);// 創(chuàng)建文檔對(duì)象PdfDocument pdfDocument = new PdfDocument(pdfWriter);Document document = new Document(pdfDocument);// 8 表示一行多少列Table table = new Table(UnitValue.createPercentArray(8)).useAllAvailableWidth();for (int i = 0; i < 16; i++) {table.addCell("hi" + i);}document.add(table);// 關(guān)閉文檔document.close();} }添加表格
package top.simba1949;import com.itextpdf.io.font.constants.StandardFonts; import com.itextpdf.io.image.ImageData; import com.itextpdf.io.image.ImageDataFactory; import com.itextpdf.kernel.colors.ColorConstants; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.xobject.PdfImageXObject; import com.itextpdf.layout.Document; import com.itextpdf.layout.borders.SolidBorder; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.BackgroundImage; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.property.UnitValue;import java.io.File; import java.io.IOException;/*** @author Anthony* @date 2020/12/9 19:10*/ public class ColoredBackground {public static final String FILE_PATH = "D:\\IDE\\IDEA\\workspace\\learn\\test-spring-boot\\itextpdf-learn\\PDF.pdf";public static final String IMG_PATH = "itextpdf-learn/src/main/resources/static/image-0.jpg";public static void main(String[] args) throws IOException {// 創(chuàng)建一個(gè)要生成的PDF文件對(duì)象FileFile file = new File(FILE_PATH);// 創(chuàng)建PDF輸出流PdfWriter pdfWriter = new PdfWriter(file);// 創(chuàng)建文檔對(duì)象PdfDocument pdfDocument = new PdfDocument(pdfWriter);Document document = new Document(pdfDocument);// 創(chuàng)建字體PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// 創(chuàng)建背景圖片ImageData imageData = ImageDataFactory.create(IMG_PATH);PdfImageXObject pdfImageXObject = new PdfImageXObject(imageData);BackgroundImage.Builder backgroundImageBuilder = new BackgroundImage.Builder();backgroundImageBuilder.setImage(pdfImageXObject);BackgroundImage backgroundImage = backgroundImageBuilder.build();// 創(chuàng)建tableTable table = new Table(UnitValue.createPercentArray(8)).useAllAvailableWidth();for (int i = 0; i < 16; i++) {Cell cell = new Cell();// 設(shè)置段落,設(shè)置段落的字體和字體顏色Paragraph paragraph = new Paragraph("hi" + i).setFont(font).setFontColor(ColorConstants.WHITE);cell.add(paragraph);// 設(shè)置背景顏色// cell.setBackgroundColor(ColorConstants.RED);// 設(shè)置邊框樣式SolidBorder solidBorder = new SolidBorder(ColorConstants.BLACK, 1);cell.setBorder(solidBorder);// 設(shè)置文本對(duì)齊方式cell.setTextAlignment(TextAlignment.CENTER);table.addCell(cell);}// 設(shè)置表格背景圖片table.setBackgroundImage(backgroundImage);// 添加表格document.add(table);document.close();} }document對(duì)象
document 元素只能添加 AreaBreak 、 Image 對(duì)象和 IBlockElement 接口的實(shí)現(xiàn)類對(duì)象
 IBlockElement 的實(shí)現(xiàn)類如下圖:
 
進(jìn)階PDF
package top.simba1949;import com.itextpdf.kernel.colors.ColorConstants; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.element.Text; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.property.UnitValue;import java.io.File; import java.io.IOException;/*** @author anthony* @date 2021/1/26 23:12*/ public class Application {public static void main(String[] args) throws IOException {// 文件名String fileName = getPdfFileName();// 文件對(duì)象File file = new File(fileName);// pdf 輸出流PdfWriter pdfWriter = new PdfWriter(file);// 處理 pdf 的主入口點(diǎn)PdfDocument pdfDoc = new PdfDocument(pdfWriter);// 設(shè)置pdf的頁面大小PageSize pageSize = new PageSize(PageSize.A4);// 文檔對(duì)象,用于添加文檔中的各種元素Document document = new Document(pdfDoc, pageSize);// document 元素只能添加 AreaBreak、Image對(duì)象和IBlockElement接口的實(shí)現(xiàn)類對(duì)象// document.add(createParagraph());// 對(duì)比是否存在首行縮進(jìn)// document.add(new Paragraph("君不見黃河之水天上來,奔流到海不復(fù)回").setFont(createPdfFont()));document.add(createTable());// 文檔的最后處理document.close();}/*** 創(chuàng)建字體對(duì)象* @return* @throws IOException*/public static PdfFont createPdfFont() throws IOException {// 使用 PdfFontFactory 創(chuàng)建字體// 使用下面字體可以處理中文不顯示的問題return PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);}/*** 創(chuàng)建 Table 對(duì)象* @return*/public static Table createTable() throws IOException {// 創(chuàng)建幾列的表格對(duì)象Table table = new Table(4);// 設(shè)置table表格寬度table.setWidth(UnitValue.POINT).setWidth(520);for (int i = 0; i < 2; i++) {if (i == 0){// 第一行數(shù)據(jù),創(chuàng)建 Cell 對(duì)象,默認(rèn)一行一列Cell cell00 = new Cell();cell00.add(new Paragraph("姓名").setFont(createPdfFont()));table.addCell(cell00);table.addCell(new Cell().add(new Paragraph("李白").setFont(createPdfFont()).setFontColor(ColorConstants.BLACK)));table.addCell(new Cell().add(new Paragraph("性別").setFont(createPdfFont())).setFontSize(24));table.addCell(new Cell().add(new Paragraph("男").setFont(createPdfFont())));}else if (i == 1){// 第二行數(shù)據(jù)table.addCell(new Cell().add(new Paragraph("代表作").setFont(createPdfFont())));// 第二行數(shù)據(jù),創(chuàng)建 Cell 對(duì)象,默認(rèn)一行三列table.addCell(new Cell(1, 3).add(new Paragraph("《將進(jìn)酒》《蜀道難》").setFont(createPdfFont())));}}return table;}/*** 創(chuàng)建段落* Paragraph 和 Text 關(guān)系,* 同一個(gè)設(shè)置如果 Text 存在,則以 Text 設(shè)置為顯示方式* 如果 Text 沒有設(shè)置,以 Paragraph 設(shè)置為顯示方式* 對(duì)齊模式以 Paragraph 對(duì)齊模式設(shè)置為顯示方式* @return* @throws IOException*/public static Paragraph createParagraph() throws IOException {// 可以通過構(gòu)造方法添加問題Paragraph paragraph = new Paragraph("段落內(nèi)容");// 也可以通過添加 Text 對(duì)象添加文字paragraph.add(createText());// 段落設(shè)置字體paragraph.setFont(createPdfFont());// 段落加粗paragraph.setBold();// 段落設(shè)置字體大佬paragraph.setFontSize(24);// 段落設(shè)置顏色paragraph.setFontColor(ColorConstants.RED);// 段落設(shè)置下劃paragraph.setUnderline();// 段落首行縮進(jìn)paragraph.setFirstLineIndent(40);// 設(shè)置段落對(duì)齊模式,對(duì)齊模式以段落對(duì)齊模式設(shè)置而顯示paragraph.setTextAlignment(TextAlignment.CENTER);return paragraph;}/*** 創(chuàng)建文本對(duì)象** 注意要點(diǎn):文本對(duì)象不能直接添加到document** Paragraph 和 Text 關(guān)系,* 同一個(gè)設(shè)置如果 Text 存在,則以 Text 設(shè)置為顯示方式* 如果 Text 沒有設(shè)置,以 Paragraph 設(shè)置為顯示方式* @return*/public static Text createText() throws IOException {Text text = new Text("將進(jìn)酒");// 字體text.setFont(createPdfFont());// 字體加粗text.setBold();// 字體顏色text.setFontColor(ColorConstants.BLACK);// 字體大小text.setFontSize(24);// 字體添加下劃線text.setUnderline();// 字體設(shè)置文本對(duì)齊模式text.setTextAlignment(TextAlignment.LEFT);return text;}/*** 獲取臨時(shí)文件路徑* @return*/private static String getPdfFileName(){String userDir = System.getProperty("user.dir");String separator = System.getProperty("file.separator");return userDir + separator + "learn.pdf";} }合并PDF
package top.simba1949;import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.utils.PdfMerger;import java.io.IOException;/*** @author Anthony* @date 2020/12/9 19:48*/ public class AddCover {public static final String DEST = "D:\\IDE\\IDEA\\workspace\\learn\\test-spring-boot\\itextpdf-learn\\src\\main\\resources\\static\\dest.pdf";public static final String RESOURCE_ONE = "D:\\IDE\\IDEA\\workspace\\learn\\test-spring-boot\\itextpdf-learn\\src\\main\\resources\\static\\resource1.pdf";public static final String RESOURCE_TWO = "D:\\IDE\\IDEA\\workspace\\learn\\test-spring-boot\\itextpdf-learn\\src\\main\\resources\\static\\resource2.pdf";public static void main(String[] args) throws IOException {// 目標(biāo)pdfPdfDocument dest = new PdfDocument(new PdfWriter(DEST));// 源pdfPdfDocument cover = new PdfDocument(new PdfReader(RESOURCE_ONE));PdfDocument resource = new PdfDocument(new PdfReader(RESOURCE_TWO));PdfMerger merger = new PdfMerger(dest);// 將源pdf文件指定位置寫入到目標(biāo)pdf中merger.merge(cover, 1, 1);merger.merge(resource, 1, 1);cover.close();resource.close();merger.close();} }總結(jié)
 
                            
                        - 上一篇: Quartus17.0 + ModelS
- 下一篇: 基于javaweb的本科生实习管理系统
