使用pdfbox将PDF文件一页拆分为两页

使用pdfbox将PDF文件一页拆分为两页,自测是可以正常拆分的。

代码如下:

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

import java.io.File;
import java.io.IOException;

/**
 * PDF工具类
 *
 * @author zuoy 2021-03-05
 */
@Slf4j
public class PdfUtil {
    /**
     * 平均拆分第一页PDF为两页
     *
     * @param path    原pdf路径
     * @param curPath 拆分后路径
     * @throws IOException
     */
    public static void avgSplitFirstPage(String path, String curPath) {
        try (PDDocument document = new PDDocument();
             PDDocument doc = PDDocument.load(new File(path))) {
            // 裁剪上半部分
            PDPage topHalfPage = doc.getDocumentCatalog().getPages().get(0);
            PDRectangle topHalfRectangle = topHalfPage.getCropBox();
            float upperRightY1 = topHalfRectangle.getUpperRightY();
            topHalfRectangle.setLowerLeftY(upperRightY1 / 2);
            document.importPage(topHalfPage);

            // 裁剪下半部分
            PDPage bottomHalfPage = doc.getDocumentCatalog().getPages().get(0);
            PDRectangle bottomHalfRectangle = bottomHalfPage.getCropBox();
            float upperRightY = bottomHalfRectangle.getUpperRightY();
            float lowerLeftY = bottomHalfRectangle.getLowerLeftY();
            bottomHalfRectangle.setUpperRightY(upperRightY / 2);
            bottomHalfRectangle.setLowerLeftY(lowerLeftY);
            bottomHalfPage.setCropBox(bottomHalfRectangle);
            document.importPage(bottomHalfPage);

            // 保存拆分后pdf文件
            document.save(curPath);
        } catch (Exception e) {
            log.error("avgSplitFirstPage拆分错误", e);
        }
    }
    
 	/**
     * 将每页pdf文件平均拆分为两页
     *
     * @param path    原pdf路径
     * @param curPath 拆分后路径
     * @throws IOException
     */
    public static void avgSplitEveryPage(String path, String curPath) {
        try (PDDocument document = new PDDocument();
             PDDocument doc = PDDocument.load(new File(path))) {
            
            PDPageTree pages = doc.getDocumentCatalog().getPages();
            for (int i = 0; i < pages.getCount(); i++) {
                // 裁剪上半部分
                PDPage topHalfPage = pages.get(i);
                PDRectangle topHalfRectangle = topHalfPage.getCropBox();
                float upperRightY1 = topHalfRectangle.getUpperRightY();
                topHalfRectangle.setLowerLeftY(upperRightY1 / 2);
                document.importPage(topHalfPage);

                // 裁剪下半部分
                PDPage bottomHalfPage = pages.get(i);
                PDRectangle bottomHalfRectangle = bottomHalfPage.getCropBox();
                float upperRightY = bottomHalfRectangle.getUpperRightY();
                float lowerLeftY = bottomHalfRectangle.getLowerLeftY();
                bottomHalfRectangle.setUpperRightY(upperRightY / 2);
                bottomHalfRectangle.setLowerLeftY(lowerLeftY);
                bottomHalfPage.setCropBox(bottomHalfRectangle);
                document.importPage(bottomHalfPage);
            }

            // 保存拆分后pdf文件
            document.save(curPath);
        } catch (Exception e) {
            log.error("avgSplitEveryPage拆分错误", e);
        }
    }
    public static void main(String[] args) {
        String path = "C:\\Users\\Administrator\\Desktop\\2.pdf";
        String cutPath = "C:\\Users\\Administrator\\Desktop\\cut.pdf";
        PdfUtil.avgSplitFirstPage(path, cutPath);
    }
}


版权声明:本文为u012735965原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。