java操作pdf之iText快速入门
java操作pdf之iText快速入門
iText是著名的開放項目,是用于生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化為PDF文件。
iText官網: http://itextpdf.com/
官網示例: http://itextpdf.com/examples/
更多示例: https://kb.itextpdf.com/home/it5kb/examples
Maven依賴:
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>pdf和iText簡介
第一個iText示例:Hello World
public class HelloWorld {public static final String RESULT= "src/main/resources/hello.pdf";/*** 創建一個pdf文件: hello.pdf*/public static void main(String[] args)throws DocumentException, IOException {new HelloWorld().createPdf(RESULT);}//五個步驟public void createPdf(String filename)throws DocumentException, IOException {// step 1Document document = new Document();// step 2PdfWriter.getInstance(document, new FileOutputStream(filename));// step 3document.open();// step 4document.add(new Paragraph("Hello World!"));// step 5document.close();} }自定義頁面大小
通過 Document構造函數來設置頁面的大小尺寸和 頁面邊距。
Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)
其中通過 Rectangle(寬,高)來指定pdf頁面的尺寸大小,通過marginLeft marginRight marginTop marginBottom來設置頁面邊距.
Rectangle pagesize = new Rectangle(216f, 720f);Document document = new Document(pagesize, 36f, 72f, 108f, 180f);或者
通過PageSize.LETTER來指定頁面的大小。在實際開發中PageSize.A4使用的比較多。如果構造不指定的話默認就是PageSize.A4 頁邊距全是36pt。
Document document = new Document(PageSize.LETTER);常用屬性
//設置作者document.addAuthor("ZBK");//設置創建日期document.addCreationDate();// 設置創建者document.addCreator("張寶奎");// 設置生產者document.addProducer();// 設置關鍵字document.addKeywords("my");//設置標題document.addTitle("標題");//設置主題document.addSubject("主題");最大頁面尺寸
public class HelloWorldMaximum {public static final String RESULT = "src/main/resources/hello_maximum.pdf";public static void main(String[] args)throws DocumentException, IOException {// 第一步//最大頁面尺寸Document document = new Document(new Rectangle(14400, 14400));// 第二步PdfWriter writer =PdfWriter.getInstance(document, new FileOutputStream(RESULT));// 改變用戶單位 UserUnit是定義默認用戶空間單位的值。最小UserUnit為1(1個單位= 1/72英寸)。最大UserUnit為75,000。writer.setUserunit(75000f);// step 3document.open();// step 4document.add(new Paragraph("Hello World"));// step 5document.close();}}橫向顯示pdf
通過**rotate()**方法
Document document = new Document(PageSize.LETTER.rotate());或者
通過設置寬和高
Document document = new Document(new Rectangle(792, 612));設置頁邊距和裝訂格式
? 對于需要裝訂成冊的多頁pdf,如果是左右裝訂(正反面打印)的話我們第一頁的左邊距要與第二頁的右邊距一樣,第一頁的右邊距要與第二頁的左邊距一樣,也就是保證頁邊距要對稱。
通過 setMargins設置頁邊距,通過setMarginMirroring(true) 來設置兩頁的邊距對稱
注意: 如果是上下裝訂的方式裝訂,則通過**setMarginMirroringTopBotton(true)**來設置兩頁的邊距對稱.
public class HelloWorldMirroredMargins {public static final String RESULT= "src/main/resources/hello_mirrored_margins.pdf";public static void main(String[] args)throws DocumentException, IOException {// step 1Document document = new Document();// step 2PdfWriter.getInstance(document, new FileOutputStream(RESULT));document.setPageSize(PageSize.A5);document.setMargins(36, 72, 108, 180);document.setMarginMirroring(true); //設置邊距對稱// step 3document.open();// step 4document.add(new Paragraph("The left margin of this odd page is 36pt (0.5 inch); " +"the right margin 72pt (1 inch); " +"the top margin 108pt (1.5 inch); " +"the bottom margin 180pt (2.5 inch)."));Paragraph paragraph = new Paragraph();paragraph.setAlignment(Element.ALIGN_JUSTIFIED);for (int i = 0; i < 20; i++) {paragraph.add("Hello World! Hello People! " +"Hello Sky! Hello Sun! Hello Moon! Hello Stars!");}document.add(paragraph);document.add(new Paragraph("The right margin of this even page is 36pt (0.5 inch); " +"the left margin 72pt (1 inch)."));// step 5document.close();} }內存中操作pdf文件
public class HelloWorldMemory {public static final String RESULT = "src/main/resources/hello_memory.pdf";public static void main(String[] args)throws DocumentException, IOException {// step 1Document document = new Document();// step 2// we'll create the file in memoryByteArrayOutputStream baos = new ByteArrayOutputStream();PdfWriter.getInstance(document, baos);// step 3document.open();// step 4document.add(new Paragraph("Hello World!"));// step 5document.close();// let's write the file in memory to a file anywayFileOutputStream fos = new FileOutputStream(RESULT);fos.write(baos.toByteArray());fos.close();} }設置pdf版本
通過PdfWriter 的 setPdfVersion 方法來設置我們pdf的版本號
public class HelloWorldVersion_1_7 {public static final String RESULT = "src/main/resources/hello_1_7.pdf";public static void main(String[] args)throws DocumentException, IOException {// step 1Document document = new Document();// step 2// Creating a PDF 1.7 documentPdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));writer.setPdfVersion(PdfWriter.VERSION_1_7);// step 3document.open();// step 4document.add(new Paragraph("Hello World!"));// step 5document.close();} }添加內容
添加內容也有兩個方法
- 往Document類中添加high-level的對象
- 直接用PdfWriter實例添加比較low-level的數據。
和以前代碼不同的是:我們將生成的PdfWriter實例保存到局部變量writer中。因為后續要通過此writer來獲取canvas畫線畫圖。代碼中通過設置CompressionLevel屬性為0可以避免對輸出流的壓縮,我們也可以通過記事本來查看pdf的語法語句。以上canvas的每個方法后面的注釋對應具體的pdf語法,大家用記事本打開就可以看到。
壓縮多個pdf文件
通過ZipEntry 將多個pdf壓縮成一個壓縮文件
public class HelloZip {public static final String RESULT = "src/main/resources/hello.zip";public static void main(String[] args)throws DocumentException, IOException {// creating a zip file with different PDF documentsZipOutputStream zip =new ZipOutputStream(new FileOutputStream(RESULT));for (int i = 1; i <= 3; i++) {ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");zip.putNextEntry(entry);// step 1Document document = new Document();// step 2PdfWriter writer = PdfWriter.getInstance(document, zip);writer.setCloseStream(false);// step 3document.open();// step 4document.add(new Paragraph("Hello " + i));// step 5document.close();zip.closeEntry();}zip.close();} }添加新頁
document.newPage();添加水印
文字水印
public class PdfWaterMark {public static final String RESULT = "src/main/resources/font_water_mark.pdf";public static void main(String[] args) throws FileNotFoundException, DocumentException {Document document = new Document();PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(RESULT));// 打開文檔document.open();// 加入水印PdfContentByte waterMar = pdfWriter.getDirectContentUnder();// 開始設置水印waterMar.beginText();// 設置水印透明度PdfGState gs = new PdfGState();// 設置填充字體不透明度為0.4fgs.setFillOpacity(0.4f);try {// 設置水印字體參數及大小 (字體參數,字體編碼格式,是否將字體信息嵌入到pdf中(一般不需要嵌入),字體大小)waterMar.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 60);// 設置透明度waterMar.setGState(gs);// 設置水印對齊方式 水印內容 X坐標 Y坐標 旋轉角度waterMar.showTextAligned(Element.ALIGN_RIGHT, "www.newland.com", 500, 430, 45);// 設置水印顏色waterMar.setColorFill(BaseColor.GRAY);//結束設置waterMar.endText();waterMar.stroke();} catch (IOException e) {e.printStackTrace();}// 加入文檔內容document.add(new Paragraph("my first pdf demo"));// 關閉文檔document.close();pdfWriter.close();}}圖片水印
public class PdfWaterMark {public static final String RESULT = "src/main/resources/img_water_mark.pdf";public static void main(String[] args) throws FileNotFoundException, DocumentException {Document document = new Document();PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(RESULT));// 打開文檔document.open();// 加入水印PdfContentByte waterMar = pdfWriter.getDirectContentUnder();// 開始設置水印waterMar.beginText();// 設置水印透明度PdfGState gs = new PdfGState();// 設置填充字體不透明度為0.4fgs.setFillOpacity(0.4f);try {Image image = Image.getInstance("D:\\Folder\\image\\logo.png");// 設置坐標 絕對位置 X Yimage.setAbsolutePosition(200, 300);// 設置旋轉弧度image.setRotation(30);// 旋轉 弧度// 設置旋轉角度image.setRotationDegrees(45);// 旋轉 角度// 設置等比縮放image.scalePercent(30);// 依照比例縮放// image.scaleAbsolute(200,100);//自定義大小// 設置透明度waterMar.setGState(gs);// 添加水印圖片waterMar.addImage(image);// 設置透明度waterMar.setGState(gs);//結束設置waterMar.endText();waterMar.stroke();} catch (IOException e) {e.printStackTrace();}// 加入文檔內容document.add(new Paragraph("hello world"));// 關閉文檔document.close();pdfWriter.close();}}給已經存在的pdf添加文字水印
/*** 已有的pdf文件添加水印* @param inputFile:要加水印的源文件的路徑* @param outputFile:加水印后文件的輸出路徑* @param waterMarkName:要加的水印名*/ public static void waterMark(String inputFile,String outputFile, String waterMarkName) {try {PdfReader reader = new PdfReader(inputFile);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);PdfGState gs = new PdfGState();//改透明度gs.setFillOpacity(0.5f);gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;JLabel label = new JLabel();label.setText(waterMarkName);PdfContentByte under;// 添加一個水印for (int i = 1; i < total; i++) {// 在內容上方加水印under = stamper.getOverContent(i);//在內容下方加水印//under = stamper.getUnderContent(i);gs.setFillOpacity(0.5f);under.setGState(gs);under.beginText();//改變顏色under.setColorFill(BaseColor.LIGHT_GRAY);//改水印文字大小under.setFontAndSize(base, 100);under.setTextMatrix(70, 200);//后3個參數,x坐標,y坐標,角度under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 350, 55);under.endText();}stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();} }給已經存在的pdf添加圖片水印
/*** @param inputFile:要加水印的源文件的路徑* @param outputFile:加水印后文件的輸出路徑* @param pocturePath: 圖片的位置*/ public static void pictureWaterMark(String inputFile, String outputFile, String pocturePath) {try {PdfReader reader = new PdfReader(inputFile);PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));PdfGState gs = new PdfGState();//設置透明度gs.setFillOpacity(0.3f);gs.setStrokeOpacity(0.4f);int total = reader.getNumberOfPages() + 1;PdfContentByte waterMar;// 添加一個水印for (int i = 1; i < total; i++) {// 在內容上方加水印waterMar = stamper.getOverContent(i);//在內容下方加水印//under = stamper.getUnderContent(i);waterMar.setGState(gs);waterMar.beginText();Image image = Image.getInstance(pocturePath);// 設置坐標 絕對位置 X Yimage.setAbsolutePosition(150, 250);// 設置旋轉弧度image.setRotation(30);// 旋轉 弧度// 設置旋轉角度image.setRotationDegrees(45);// 旋轉 角度// 設置等比縮放image.scalePercent(30);// 依照比例縮放// image.scaleAbsolute(200,100);//自定義大小// 添加水印圖片waterMar.addImage(image);// 設置透明度waterMar.setGState(gs);//結束設置waterMar.endText();waterMar.stroke();}stamper.close();reader.close();} catch (Exception e) {e.printStackTrace();} }pdf加密
首先要說明的是,itext中對pdf文檔的加密包括兩部分,第一部分是用戶密碼,第二部分是所有者密碼。這兩部分可以簡單的理解為管理員密碼和用戶密碼,因此我們在設置這兩個密碼的權限的時候,往往會將所有者密碼的權限級別設置的更高,而用戶密碼權限往往是“只讀”。
在之前的基礎上添加新的Maven依賴
<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.60</version> </dependency> public class PdfPassword {public static final String RESULT = "src/main/resources/password.pdf";public static void main(String[] args) throws FileNotFoundException,DocumentException {//實現A4紙頁面 并且橫向顯示(不設置則為縱向)Document document = new Document(PageSize.A4.rotate());PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(RESULT));// 設置用戶密碼, 所有者密碼,用戶權限,所有者權限pdfWriter.setEncryption("userpassword".getBytes(), "ownerPassword".getBytes(), PdfWriter.ALLOW_COPY, PdfWriter.ENCRYPTION_AES_128);// 打開文檔document.open();// 加入文檔內容document.add(new Paragraph("hello world"));// 關閉文檔document.close();pdfWriter.close();} }-
然后我們打開我們的pdf文檔,會彈出一個讓你輸入密碼的對話框,我們先用“userpassword”這個用戶密碼去打開。然后再查看文檔的屬性,具體如下:
-
我們可以看到,我們是無法對現在的這個pdf進行打印和修改的。接下來,我們重新打開這個pdf。用“ownerPassword”這個密碼取打開。然后再查看文檔的屬性,具體如下:
權限參數
PdfWriter.ALLOW_MODIFY_CONTENTS
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.ALLOW_COPY
允許復制,簽名 不允許打印,編輯 加密級別:40-bit-RC
PdfWriter.ALLOW_MODIFY_ANNOTATIONS
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.ALLOW_FILL_IN
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.ALLOW_SCREENREADERS
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.ALLOW_ASSEMBLY
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.EMBEDDED_FILES_ONLY
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.DO_NOT_ENCRYPT_METADATA
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
PdfWriter.ENCRYPTION_AES_256
允許打印,編輯,復制,簽名 加密級別:256-bit-AES
PdfWriter.ENCRYPTION_AES_128
允許打印,編輯,復制,簽名 加密級別:128-bit-AES
PdfWriter.STANDARD_ENCRYPTION_128
允許打印,編輯,復制,簽名 加密級別:128-bit-RC4
PdfWriter.STANDARD_ENCRYPTION_40
允許打印,編輯,復制,簽名 加密級別:40-bit-RC4
使用iText的基本構建快
? 現在我們將重點集中到第四步:添加內容。這里說的添加內容都是通過Document.Add()方法調用,也就是通過一些high-level的對象實現內容的添加。這一節如標題要介紹Chunk、Phrase、Paragraph和List對象的屬性和使用。Document的Add方法接受一個IElement的接口,我們先來看下實現此接口的UML圖:
Chunk 對象
? Chunk類是可以添加到Document中最小的文本片段。Chunk類中包含一個StringBuilder對象,其代表了有相同的字體,字體大小,字體顏色和字體格式的文本,這些屬性定義在Chunk類中Font對象中。其它的屬性如背景色(background),text rise(用來模擬下標和上標)還有underline(用來模擬文本的下劃線或者刪除線)都定義在其它一系列的屬性中,這些屬性都可以通過settter 方法進行設置.
public class CountryChunks {public static final String RESULT = "src/main/resources/country_chunks.pdf";public static void main(String[] args)throws IOException, DocumentException, SQLException {new CountryChunks().createPdf(RESULT);}public void createPdf(String filename)throws IOException, DocumentException, SQLException {// step 1Document document = new Document();// step 2PdfWriter.getInstance(document, new FileOutputStream(filename)).setInitialLeading(16);// step 3document.open();// step 4// add a country to the document as a Chunkdocument.add(new Chunk("China"));document.add(new Chunk(" "));Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);Chunk id = new Chunk("cn", font);// with a background colorid.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);// and a text riseid.setTextRise(6);document.add(id);document.add(Chunk.NEWLINE);//換行 NEWLINE = new Chunk("\n");// step 5document.close();} }行間距: LEADING
使用Chunk類要注意的是其不知道兩行之間的行間距(Leading),這就是為什么在代碼中要設置InitialLeading屬性的原因。大家可以試著將其去掉然后再看下重新生存的pdf文檔:你會發現所有的文本都寫在同一行上。
SetTextRise
SetTextRise方法的參數表示的是離這一行的基準線(baseline)的距離,如果為正數就會模擬上標,負數就會模擬為下標。
Phrase 對象
Chunk類在iText是最小的原子文本,而Phrase類被定義為 “a string of word”,因此Phrase是一個組合的對象。轉換為java和iText來說,phrase就是包含了Chunk類的一個ArraryList。
Font bul = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD | Font.UNDERLINE); Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12);Phrase director = new Phrase(); director.Add(new Chunk("China", bul)); director.Add(new Chunk(",", bul)); director.Add(new Chunk("id", normal));document.add(director);Paragraph 對象
可以將Phrase和Paragraph類比HTML中的span和div。如果在前一個例子中用Paragraph代替Phrase,就沒有必要添加document.Add(Chunk.NEWLINE)。
Paragraph繼承Phrase類,因此我們在創建Paragraph類時和創建Phrase完全一致,不過Paragraph擁有更多的屬性設置:定義文本的對齊方式、不同的縮進和設置前后的空間大小。
html轉為pdf
這里需要說明一個情況,如果項目中不引入字體文件,那么生成的pdf將不會顯示文字(因為生產環境的服務器不會有任何字體文件,而本地運行的話,會自動去電腦中的字體文件庫中去尋找字體文件),因此,如果是需要發布的項目,務必將字體文件放到項目中,然后進行使用。
引入Maven依賴
<dependency><groupId>org.xhtmlrenderer</groupId><artifactId>core-renderer</artifactId><version>R8</version> </dependency>index.html
<!DOCTYPE html> <html lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>認識table表標簽</title><style type="text/css">/*解決html轉pdf文件中文不顯示的問題*/body {font-family: SimSun;}/*設定紙張大小*//* A4紙 *//* @page{size:210mm*297mm} */@page{size:a4}p {color: red;}</style> </head> <body><table border="1px"><caption>我的標題</caption><tbody><tr><th>班級</th><th>學生數</th><th>平均成績</th><th>班級</th><th>學生數</th><th>平均成績</th><th>班級</th><th>學生數</th><th>平均成績</th></tr><tr><td>一班</td><td>30</td><td>89</td><td>一班</td><td>30</td><td>89</td><td>一班</td><td>30</td><td>89</td></tr><tr><td>一班</td><td>30</td><td>89</td><td>一班</td><td>30</td><td>89</td><td>一班</td><td>30</td><td>89</td></tr><tr><td>一班</td><td>30</td><td>89</td><td>一班</td><td>30</td><td>89</td><td>一班</td><td>30</td><td>89</td></tr></tbody></table><p>《俠客行》<br/>年代: 唐 作者: 李白<br/>趙客縵胡纓,吳鉤霜雪明。銀鞍照白馬,颯沓如流星。十步殺一人,千里不留行。事了拂衣去,深藏身與名。閑過信陵飲,脫劍膝前橫。將炙啖朱亥,持觴勸侯嬴。三杯吐然諾,五岳倒為輕。眼花耳熱后,意氣素霓生。救趙揮金槌,邯鄲先震驚。千秋二壯士,煊赫大梁城。縱死俠骨香,不慚世上英。誰能書閤下,白首太玄經。</p> </body> </html>PdfUtil.java
public class PdfUtil {/*** @param htmlFile html文件存儲路徑* @param pdfFile 生成的pdf文件存儲路徑* @param chineseFontPath 中文字體存儲路徑*/public static void html2pdf(String htmlFile, String pdfFile, String chineseFontPath){// step 1String url;OutputStream os = null;try {url = new File(htmlFile).toURI().toURL().toString();os = new FileOutputStream(pdfFile);ITextRenderer renderer = new ITextRenderer();renderer.setDocument(url);// 解決中文不顯示問題ITextFontResolver fontResolver = renderer.getFontResolver();fontResolver.addFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);renderer.layout();renderer.createPDF(os);} catch (Exception e) {} finally {if (os != null) {try {os.close();} catch (Exception e) {e.printStackTrace();}}}}public static void main(String[] args) {try {//獲取項目路徑String projectPath = System.getProperty("user.dir");//html文件路徑String htmlFilePath = projectPath + "/src/main/resources/index.html";// 中文字體存儲路徑String chineseFontPath = projectPath +"/src/main/resources/Fonts/simsun.ttc";// html轉pdfhtml2pdf(htmlFilePath, projectPath + "/src/main/resources/html2pdf.pdf", chineseFontPath);System.out.println("轉換成功!");} catch (Exception e) {System.out.println("轉換失敗!");e.printStackTrace();}} }iText轉html為pdf遇到的問題
中文不顯示
原因: iText默認不支持中文
解決方法: 引入中文字體 (本機字體文件庫C:\Windows\Fonts中下載上傳到項目中)
需要注意的是在java代碼中設置好中文字體后,還需要在html引用該字體,否則不會起效果。
// 解決中文不顯示問題 ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); /*解決html轉pdf文件中文不顯示的問題*/ body {font-family: SimSun; }css不起作用
原因: css文件使用的是相對路徑
解決方法: 將相對路徑改為絕對路徑或者把css樣式寫在html內部.
html內容轉為pdf后內容不全
解決方法: 在html中設定紙張大小
/*設定紙張大小*//* A4紙 *//* @page{size:210mm*297mm} */@page{size:a4}iText轉html為pdf遇到的問題
中文不顯示
原因: iText默認不支持中文
解決方法: 引入中文字體 (本機字體文件庫C:\Windows\Fonts中下載上傳到項目中)
需要注意的是在java代碼中設置好中文字體后,還需要在html引用該字體,否則不會起效果。
// 解決中文不顯示問題 ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); /*解決html轉pdf文件中文不顯示的問題*/ body {font-family: SimSun; }css不起作用
原因: css文件使用的是相對路徑
解決方法: 將相對路徑改為絕對路徑或者把css樣式寫在html內部.
html內容轉為pdf后內容不全
解決方法: 在html中設定紙張大小
/*設定紙張大小*//* A4紙 *//* @page{size:210mm*297mm} */@page{size:a4}推薦博客:https://www.cnblogs.com/julyluo/archive/2012/07/29/2613822.html
推薦博客:https://www.cnblogs.com/liaojie970/p/7132475.html
總結
以上是生活随笔為你收集整理的java操作pdf之iText快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 下列字符是c语言保留两位小数,c语言中保
- 下一篇: 计算机毕业设计springboot+vu