java利用apache pdfbox工具裁剪PDF文件

java利用apache pdfbox工具裁剪PDF文件

一、导入apache pdfbox工具jar包

maven仓库导入工具包

<dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>2.0.25</version>
    </dependency>
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>fontbox</artifactId>
      <version>2.0.25</version>
    </dependency>

二、裁剪pdf工具类

package com.mhx.info.service.util;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.junit.Test;

import java.io.File;

/**
 * @Description: pdf文件裁剪工具类
 * @BelongsProject: mhx
 * @BelongsPackage: com.mhx.info.service.util
 * @ClassName: PdfBoxChangUtilTest
 * @Author: MHX
 * @CreateTime: 2022/11/25
 */
public class PdfBoxChangUtilTest {

    /**
     * pdf格式改变
     *
     * @throws Exception 异常
     */
    @Test
    public void pdfToChange() throws Exception {
        String sourceFilePath = "D:/zhxcmfs";
        String sourceFileName = "证明.pdf";
        PDDocument outdoc = new PDDocument();
        PDDocument doc = PDDocument.load(new File(sourceFilePath + "/" + sourceFileName));
        //获取PDF第一页
        PDPage page = doc.getDocumentCatalog().getPages().get(0);
        PDRectangle artBox = page.getArtBox();
        //切割PDF的左部坐标,只需要X坐标
        artBox.setUpperRightX(312.0F);
        //切割PDF的上右部坐标,只需要X坐标
        artBox.setLowerLeftX(839.0F);
//        切割pdf的左下角坐标,只需Y坐标
        artBox.setLowerLeftY(50F);
        //        切割pdf的右上角坐标,只需Y坐标
        artBox.setUpperRightY(500F);
        page.setArtBox(artBox);
        outdoc.importPage(page);
        //希望切割后的PDF存储的位置
        String savePath = "D:\\zhxcmfs\\myFiles";
        String fileName = "裁剪后的证明.pdf";
//        该文件如果存在不创建,不存在则创建
        File destDir = new File(savePath + "/" + fileName);
        if (!destDir.exists()) {
            destDir.mkdir();
        }
        //保存输出切割后的PDF
        outdoc.save(savePath + "/" + fileName);
        outdoc.close();
        doc.close();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 Apache PDFBox 可以很方便地裁剪 PDF。以下是一个示例程序,演示如何使用 PDFBox 裁剪 PDF: ```java import java.io.File; import java.io.IOException; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.graphics.color.PDColor; import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB; import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState; import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageXYZDestination; import org.apache.pdfbox.util.Matrix; public class PdfCropper { public static void main(String[] args) throws IOException { String inputFileName = "input.pdf"; String outputFileName = "output.pdf"; // Create a PDF parser and parse the input file PDFParser parser = new PDFParser(new File(inputFileName)); parser.parse(); // Load the parsed document PDDocument document = parser.getPDDocument(); // Get the first page of the document PDPage page = document.getPage(0); // Set the crop box to be a smaller rectangle PDRectangle cropBox = new PDRectangle(72, 72, 468, 648); page.setCropBox(cropBox); // Set the media box to be the same as the crop box PDRectangle mediaBox = new PDRectangle(cropBox.getWidth(), cropBox.getHeight()); page.setMediaBox(mediaBox); // Set the page display mode to fit the width of the screen PDPageFitWidthDestination fitWidth = new PDPageFitWidthDestination(); fitWidth.setPageNumber(0); page.setDisplayMode(PDPageFitWidthDestination.DISPLAY_MODE_FIT_WIDTH); // Set the page zoom to 100% PDPageXYZDestination xyz = new PDPageXYZDestination(); xyz.setPageNumber(0); xyz.setZoom(1.0f); page.setCurrentZoom(1.0f); // Set the page background color to white PDColor white = new PDColor(new float[]{1, 1, 1}, PDDeviceRGB.INSTANCE); PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingColor(white); PDGraphicsState state = new PDGraphicsState(); state.setNonStrokingColorSpace(PDDeviceRGB.INSTANCE); state.setNonStrokingColor(white); state.setStrokingColorSpace(PDDeviceRGB.INSTANCE); state.setStrokingColor(white); page.setGraphicsStateParameters(state, graphicsState); // Save the modified document document.save(outputFileName); // Close the document document.close(); } } ``` 此示例程序使用 `setCropBox()` 方法将 PDF裁剪框设置为 `(72, 72, 468, 648)`,这意味着裁剪框的左下角在 `(72, 72)`,右上角在 `(72+468, 72+648)`。然后使用 `setMediaBox()` 方法将媒体框设置为与裁剪框相同的大小。页面显示模式设置为适合屏幕宽度,页面缩放设置为 100%,页面背景色设置为白色。最后,使用 `save()` 方法保存修改后的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值