使用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);
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值