Apache?PDFBox?是一個(gè)用于處理?PDF?文檔的?Java?庫。它提供了許多功能和方法來讀取、創(chuàng)建、操作和提取?PDF?文檔的內(nèi)容。
引入?maven?依賴
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
pdfbox?生成?pdf?實(shí)例
try {
// 創(chuàng)建一個(gè)空白的PDF文檔
PDDocument document = new PDDocument();
// 創(chuàng)建一個(gè)頁面
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// 創(chuàng)建一個(gè)內(nèi)容流
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// 設(shè)置字體和字號(hào)
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
// 在頁面上繪制文本
contentStream.beginText();
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello, World!");
contentStream.endText();
// 關(guān)閉內(nèi)容流
contentStream.close();
// 保存PDF文檔
document.save("output.pdf");
// 關(guān)閉PDF文檔
document.close();
System.out.println("PDF生成成功!");
} catch (IOException e) {
e.printStackTrace();
}
常用方法
PDDocument?類
引用源碼中對(duì)PDDocument?類的描述
This?is?the?in-memory?representation?of?the?PDF?document
這是PDF文檔的內(nèi)存表示,在?java?程序中,你可以簡(jiǎn)單理解為他就是?pdf?文檔,后續(xù)對(duì)他的一系列操作就是對(duì)?pdf?文檔的一系列操作。
創(chuàng)建全新的?pdf?文檔:文檔中無任何頁面
PDDocument document=new PDDocument();
如果你想對(duì)原有的?pdf?模板進(jìn)行動(dòng)態(tài)數(shù)據(jù)的填充,可以使用PDDocument.load()方法來加載已經(jīng)制作好的?pdf?模板,
PDDocument document = PDDocument.load(new ClassPathResource("/static/reportTemplate.pdf").getInputStream());
你也可以用文件形式來加載?pdf?模板,不過更推薦文件流的形式
PDDocument document = PDDocument.load(new ClassPathResource("/static/reportTemplate.pdf").getFile());
如果你想對(duì)你生成的?pdf?進(jìn)行加密操作,你可以使用PDDocument?load(InputStream?input,?String?password)方法,如下設(shè)置了解密的密碼為?123456.
PDDocument document = PDDocument.load(new ClassPathResource("/static/reportTemplate.pdf").getInputStream(),"123456");
PDDocument.load()中有好多重載的方法,這里就不一一列出。感興趣的可以查看?pdfbox?的源碼,
ByteArrayOutputStream baos = new ByteArrayOutputStream();;
document.save(baos); //保存文件到文件流
document.save("output.pdf"); //保存文件到文件
保存成文件流之后,有時(shí)候我們需要將文件傳輸?shù)角岸诉M(jìn)行下載,
// 將PDF文件轉(zhuǎn)換為字節(jié)數(shù)組
byte[] pdfBytes = baos.toByteArray();
// 創(chuàng)建InputStreamResource對(duì)象
ByteArrayInputStream bis = new ByteArrayInputStream(pdfBytes);
InputStreamResource resource = new InputStreamResource(bis);
// 設(shè)置HTTP響應(yīng)頭信息
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=output.pdf");
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE);
// 返回帶有PDF內(nèi)容的響應(yīng)實(shí)體
return ResponseEntity.ok()
.headers(headers)
.body(resource);
在對(duì)document?操作完成之后,一定要執(zhí)行document.close()方法關(guān)閉?pdf?文檔。
document.close();
PDPage?類
PDPage?屬于?pdf?文檔中的的頁面,
int pageNumber=document.getNumberOfPages();
獲取指定頁面,
PDPage page = document.getPage(0);
如果你是對(duì)?pdf?模板進(jìn)行操作,你可以通過document.getPage(index)方法來獲取?pdf?文檔的指定頁面,并對(duì)其進(jìn)行操作(index?從?0?開始)。你也可以通過new?PDPage();創(chuàng)建一個(gè)全新的?page,
PDPage newPage = new PDPage(PDRectangle.A4);
如果我們是通過?new?PDPage()的方式生成?page?頁面時(shí),我們需要將?page?頁面添加到?pdf?文檔中去(document),
document.addPage(newPage);
不過這種方式會(huì)將?page?添加到?pdf?文檔的末尾,我們有時(shí)候需要將?page?添加到指定的位置,可以使用以下方法。
PDPage page=document.getPage(1); //獲取第2頁
PDPage newPage = new PDPage(PDRectangle.A4);
PDPageTree pages = document.getPages();
pages.insertAfter(newPage,page); //插入到第2頁后面
pages.insertBefore(newPage,page); //插入到第2頁前面
獲取?page?頁面總高度和總寬度,這個(gè)在后續(xù)的文字坐標(biāo)定位中很有用,在?page?中原點(diǎn)坐標(biāo)位于左下角,如果你想你的元素左邊距為?10,上邊距為?10,那么你的坐標(biāo)將是(10,pageHeight-10)
float pageWidth = page.getMediaBox().getWidth();
float pageHeight = page.getMediaBox().getHeight();
PDPageContentStream
PDPageContentStream?類提供寫入頁面內(nèi)容流的功能,它需要綁定?pdf?文檔和指定的?page?頁面,這樣相當(dāng)于創(chuàng)建了?page?當(dāng)前頁面的內(nèi)容流。
PDPageContentStream contentStream = new PDPageContentStream(document, page);
如果不指定PDPageContentStream.AppendMode,默認(rèn)會(huì)以重寫模式執(zhí)行,后續(xù)對(duì)?page?頁面添加元素會(huì)覆蓋現(xiàn)有頁面內(nèi)容流。
PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
模式代碼 |
模式 |
注釋 |
PDPageContentStream.AppendMode.OVERWRITE |
重寫模式 |
覆蓋現(xiàn)有頁面內(nèi)容流 |
PDPageContentStream.AppendMode.APPEND |
追加模式 |
將內(nèi)容流附加到所有現(xiàn)有頁面內(nèi)容流之后 |
PREPENDPDPageContentStream.AppendMode. |
準(zhǔn)備模式 |
在所有其他頁面內(nèi)容流之前插入 |
對(duì)contentStream?操作完成之后,需要關(guān)閉內(nèi)容流。
contentStream.close();
pdf?寫入內(nèi)容
關(guān)于字體
在?Apache?PDFBox?中,字體相關(guān)的類主要位于?org.apache.pdfbox.pdmodel.font?包下。下面是一些常用的字體類:
-
PDType1Font:這個(gè)類表示?Type?1?字體,它是一種基于輪廓的字體格式。Type?1?字體常用于?PDF?文檔中,如?Helvetica、Times?Roman?和?Courier?等。
示例:
PDType1Font font = PDType1Font.HELVETICA_BOLD;
public static final PDType1Font TIMES_ROMAN = new PDType1Font("Times-Roman");
public static final PDType1Font TIMES_BOLD = new PDType1Font("Times-Bold");
public static final PDType1Font TIMES_ITALIC = new PDType1Font("Times-Italic");
public static final PDType1Font TIMES_BOLD_ITALIC = new PDType1Font("Times-BoldItalic");
public static final PDType1Font HELVETICA = new PDType1Font("Helvetica");
public static final PDType1Font HELVETICA_BOLD = new PDType1Font("Helvetica-Bold");
public static final PDType1Font HELVETICA_OBLIQUE = new PDType1Font("Helvetica-Oblique");
public static final PDType1Font HELVETICA_BOLD_OBLIQUE = new PDType1Font("Helvetica-BoldOblique");
public static final PDType1Font COURIER = new PDType1Font("Courier");
public static final PDType1Font COURIER_BOLD = new PDType1Font("Courier-Bold");
public static final PDType1Font COURIER_BOLD_OBLIQUE = new PDType1Font("Courier-BoldOblique");
public static final PDType1Font SYMBOL = new PDType1Font("Symbol");
public static final PDType1Font ZAPF_DINGBATS = new PDType1Font("ZapfDingbats");
-
PDTrueTypeFont:這個(gè)類表示?TrueType?字體,也是一種基于輪廓的字體格式。TrueType?字體在?PDF?中也很常見。
PDTrueTypeFont font = PDType1Font.TIMES_ROMAN;
-
PDType0Font:這個(gè)類表示?Type?0?字體,它是一種復(fù)合字體格式,可以包含多個(gè)子字體。Type?0?字體通常用于支持多語言和復(fù)雜字形需求,你可以使用它來加載自己自定義的字體文件。
PDType0Font font = PDType0Font.load(document, new ClassPathResource("/static/wryhRegular.ttf").getInputStream());
寫入單行文本
contentStream.setFont(PDType1Font.COURIER_BOLD_OBLIQUE, 16);
contentStream.beginText();
contentStream.newLineAtOffset(50, pageHeight-50);
contentStream.showText("測(cè)試文本");
contentStream.endText();
在寫入文本之前需要通過contentStream.setFont(PDFont?font,?float?fontSize)?方法設(shè)置字體和字號(hào),并通過beginText()方法開始一個(gè)新的文本段落,通過newLineAtOffset(x,?y);方法設(shè)置文本的坐標(biāo)位置,這里設(shè)置(50,?pageHeight-50)表示文本位置位于左上角,離上面和左邊?50?個(gè)單位。然后通過showText(String?text)顯示你需要展示的文本,最后用endText()方法結(jié)束文本段落。
連續(xù)寫入多行文本
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
// 設(shè)置文本起始坐標(biāo)
float startX = 50;
float startY = page.getMediaBox().getHeight() - 50;
// 設(shè)置行間距
float leading = 15;
// 寫入多行文本
String[] lines = {
"第一行文本",
"第二行文本",
"第三行文本"
};
contentStream.beginText();
contentStream.newLineAtOffset(startX, startY);
for (String line : lines) {
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
}
contentStream.endText();
寫入多行文本和單行文本流程差不多,都需要先設(shè)置字體和字號(hào),確定寫入文字的坐標(biāo),不同的是,我們?cè)?beginText()方法和endText()方法之間,多次執(zhí)行了showText()和newLineAtOffset(),newLineAtOffset(0,?-leading)方法代表著在上一行的位置基礎(chǔ)上,X?軸不變,Y?軸向下移動(dòng)leading?個(gè)單位。多次循環(huán)之后將多行文本添加到?pdf?文檔中。文章來源:http://www.zghlxwxcb.cn/news/detail-739412.html
插入圖片
PDImageXObject image = PDImageXObject.createFromFileByExtension(new File("path/to/image.jpg"), document);
float imageWidth = image.getWidth();
float imageHeight = image.getHeight();
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(image, x, y, imageWidth, imageHeight);
這里我們使用PDImageXObject.createFromFileByExtension()方法加載圖片文件,創(chuàng)建一個(gè)PDImageXObject對(duì)象。確保將"path/to/image.jpg"替換為實(shí)際圖片文件的路徑,這里我將圖片的寬度和高度設(shè)置為真實(shí)圖片的寬高,在實(shí)際情況中你也可以自定義寬高,最后通過drawImage(image,?x,?y,?imageWidth,?imageHeight)方法將圖片寫入到?pdf?文檔中,x,y?代表其?xy?坐標(biāo),后面的imageWidth,?imageHeight?分別代表圖片的寬度和高度。文章來源地址http://www.zghlxwxcb.cn/news/detail-739412.html
添加矩形框
//設(shè)置邊框顏色
contentStream.setStrokingColor(new Color(213, 213, 213));
//設(shè)置邊框?qū)挾葹?
contentStream.setLineWidth(1);
// 添加矩形框到頁面內(nèi)容流
contentStream.addRect(50, pageHeight-50, 100, 100);
// 繪制矩形框的邊框
contentStream.stroke();
//恢復(fù)原來的顏色,否則會(huì)影響文字顏色
contentStream.setStrokingColor(Color.BLACK);
文字坐標(biāo)計(jì)算常用方法
/**
* 獲取字體高度
* */
float getFontHeight(PDType0Font customFont,float fontSize){
return customFont.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
}
/**
* 計(jì)算文本寬度
* */
float getTextWidth(String text,float fontSize){
return fontSize * text.length();
}
附件
PDFBox官方文檔(2.0.24)
到了這里,關(guān)于java利用pdfbox動(dòng)態(tài)生成PDF的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!