Java processing PDF size

Time:2021-7-28

1、 Import dependency

Import iText related dependencies

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

2、 Encapsulated tool classes

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 {

    /**
     *Automatically modify PDF size to A4 size
     *@ param srcpdfpath path path of PDF to be modified
     *@ param newpdfpath the path to the new PDF generated
     */
    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 + "-- the original picture size is width:" + scaledwidth + ", height:" + 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();
        }
    }
}

Realization effect

First convert the original PDF into a picture, and then create an empty A4 size PDF through iText. By judging the size of the picture in equal scale, add it to the A4 size PDF, and finally scale the original PDF to A4 size

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]