Java 處理Pdf尺寸

HuDu 發表於 2021-01-27

一 、匯入依賴

匯入iText相關的依賴

<!--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>

二、封裝的工具類

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;

public class ModifyPdfUtil {

    /**
     * 自動修改pdf大小為A4大小
     * @param srcPdfPath 需要修改的pdf的路徑
     * @param newPdfPath 生成的新的pdf的路徑
     */
    public static void PdfTest(String srcPdfPath,String newPdfPath) {
        try {
            Document document = new Document();
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(newPdfPath));
            PdfImportedPage page1 = pdfWriter.getImportedPage(new PdfReader(srcPdfPath), 1);
            if (Image.getInstance(page1).getScaledWidth()>Image.getInstance(page1).getScaledHeight()) {
                document.setPageSize(PageSize.A4.rotate());
            } else {
                document.setPageSize(PageSize.A4);
            }
            document.open();
            PdfReader pdfReader = new PdfReader(srcPdfPath);
            int n = pdfReader.getNumberOfPages();
            PdfImportedPage page;
            PdfImportedPage page2;
            for (int i = 1;i <= n;i++) {
                int m = 0;
                page = pdfWriter.getImportedPage(pdfReader,i);
                Image image = Image.getInstance(page);
                if (i != n) {
                    page2 = pdfWriter.getImportedPage(pdfReader,i+1);
                } else {
                    page2 = pdfWriter.getImportedPage(pdfReader,i);
                }
                Image image2 = Image.getInstance(page2);
                float scaledWidth = image2.getScaledWidth();
                float scaledHeight = image2.getScaledHeight();
                System.out.println(i+"--原先圖片大小是寬:"+scaledWidth+",高是:"+scaledHeight);
                if (scaledWidth > scaledHeight) {
                    while (scaledWidth>842 || scaledHeight>595) {
                        image.scalePercent(100-m);
                        m++;
                        scaledHeight = image.getScaledHeight();
                        scaledWidth = image.getScaledWidth();
                    }
                    document.setPageSize(PageSize.A4.rotate());
                    image.setAlignment(Image.ALIGN_CENTER);
                    document.add(image);
                    document.newPage();
                } else {
                    while (scaledHeight>842 || scaledWidth>595) {
                        image.scalePercent(100-m);
                        m++;
                        scaledHeight = image.getScaledHeight();
                        scaledWidth = image.getScaledWidth();
                    }
                    // image.setAbsolutePosition(0,0);
                    document.setPageSize(PageSize.A4);
                    image.setAlignment(Image.ALIGN_CENTER);
                    document.add(image);
                    document.newPage();
                }
            }
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

實現效果

將原先的pdf先轉換為圖片,然後通過itext建立一個空的A4大小的pdf,通過判斷等比例縮放圖片的大小,新增到A4大小的pdf上,最終將原先的pdf縮放為A4大小
本作品採用《CC 協議》,轉載必須註明作者和本文連結