日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

ITextPDF7

發(fā)布時間:2023/12/19 综合教程 17 生活家
生活随笔 收集整理的這篇文章主要介紹了 ITextPDF7 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)建一個要生成的PDF文件對象File
File file = new File(FILE_PATH);
// 創(chuàng)建PDF輸出流
PdfWriter pdfWriter = new PdfWriter(file);
// 創(chuàng)建文檔對象
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)建一個要生成的PDF文件對象File
File file = new File(FILE_PATH);
// 創(chuàng)建PDF輸出流
PdfWriter pdfWriter = new PdfWriter(file);
// 創(chuàng)建文檔對象
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)建table
Table 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è)置文本對齊方式
cell.setTextAlignment(TextAlignment.CENTER);
table.addCell(cell);
}
// 設(shè)置表格背景圖片
table.setBackgroundImage(backgroundImage);
// 添加表格
document.add(table);
document.close();
}
}

document對象

document 元素只能添加 AreaBreak 、 Image 對象和 IBlockElement 接口的實現(xiàn)類對象
IBlockElement 的實現(xià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();
// 文件對象
File file = new File(fileName);
// pdf 輸出流
PdfWriter pdfWriter = new PdfWriter(file);
// 處理 pdf 的主入口點
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
// 設(shè)置pdf的頁面大小
PageSize pageSize = new PageSize(PageSize.A4);
// 文檔對象,用于添加文檔中的各種元素
Document document = new Document(pdfDoc, pageSize);
// document 元素只能添加 AreaBreak、Image對象和IBlockElement接口的實現(xiàn)類對象
// document.add(createParagraph());
// 對比是否存在首行縮進
// document.add(new Paragraph("君不見黃河之水天上來,奔流到海不復回").setFont(createPdfFont()));
document.add(createTable());
// 文檔的最后處理
document.close();
}
/** * 創(chuàng)建字體對象 * @return * @throws IOException */
public static PdfFont createPdfFont() throws IOException { 

// 使用 PdfFontFactory 創(chuàng)建字體
// 使用下面字體可以處理中文不顯示的問題
return PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
}
/** * 創(chuàng)建 Table 對象 * @return */
public static Table createTable() throws IOException { 

// 創(chuàng)建幾列的表格對象
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 對象,默認一行一列
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 對象,默認一行三列
table.addCell(new Cell(1, 3).add(new Paragraph("《將進酒》《蜀道難》").setFont(createPdfFont())));
}
}
return table;
}
/** * 創(chuàng)建段落 * Paragraph 和 Text 關(guān)系, * 同一個設(shè)置如果 Text 存在,則以 Text 設(shè)置為顯示方式 * 如果 Text 沒有設(shè)置,以 Paragraph 設(shè)置為顯示方式 * 對齊模式以 Paragraph 對齊模式設(shè)置為顯示方式 * @return * @throws IOException */
public static Paragraph createParagraph() throws IOException { 

// 可以通過構(gòu)造方法添加問題
Paragraph paragraph = new Paragraph("段落內(nèi)容");
// 也可以通過添加 Text 對象添加文字
paragraph.add(createText());
// 段落設(shè)置字體
paragraph.setFont(createPdfFont());
// 段落加粗
paragraph.setBold();
// 段落設(shè)置字體大佬
paragraph.setFontSize(24);
// 段落設(shè)置顏色
paragraph.setFontColor(ColorConstants.RED);
// 段落設(shè)置下劃
paragraph.setUnderline();
// 段落首行縮進
paragraph.setFirstLineIndent(40);
// 設(shè)置段落對齊模式,對齊模式以段落對齊模式設(shè)置而顯示
paragraph.setTextAlignment(TextAlignment.CENTER);
return paragraph;
}
/** * 創(chuàng)建文本對象 * * 注意要點:文本對象不能直接添加到document * * Paragraph 和 Text 關(guān)系, * 同一個設(shè)置如果 Text 存在,則以 Text 設(shè)置為顯示方式 * 如果 Text 沒有設(shè)置,以 Paragraph 設(shè)置為顯示方式 * @return */
public static Text createText() throws IOException { 

Text text = new Text("將進酒");
// 字體
text.setFont(createPdfFont());
// 字體加粗
text.setBold();
// 字體顏色
text.setFontColor(ColorConstants.BLACK);
// 字體大小
text.setFontSize(24);
// 字體添加下劃線
text.setUnderline();
// 字體設(shè)置文本對齊模式
text.setTextAlignment(TextAlignment.LEFT);
return text;
}
/** * 獲取臨時文件路徑 * @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 { 

// 目標pdf
PdfDocument dest = new PdfDocument(new PdfWriter(DEST));
// 源pdf
PdfDocument cover = new PdfDocument(new PdfReader(RESOURCE_ONE));
PdfDocument resource = new PdfDocument(new PdfReader(RESOURCE_TWO));
PdfMerger merger = new PdfMerger(dest);
// 將源pdf文件指定位置寫入到目標pdf中
merger.merge(cover, 1, 1);
merger.merge(resource, 1, 1);
cover.close();
resource.close();
merger.close();
}
}

總結(jié)

以上是生活随笔為你收集整理的ITextPDF7的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。