java 修改pdf文档的页面的大小

maven包 网上找

<dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.office.free</artifactId>
            <version>3.9.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/spire.office.free-3.9.0.jar</systemPath>
        </dependency>

代码

 

/**
     * java修改pdf文档页面的宽高
     * 参数 inPath 修改后的新的绝对路径
     * 参数 outPath pdf的路径
     */
    public static String amendpage(String outPath, String inPath) {
        //创建PdfDocument对象
        PdfDocument originalDoc = new PdfDocument();
        //加载PDF文件
        originalDoc.loadFromFile(outPath);

        //创建一个新的PdfDocument实例
        PdfDocument newDoc = new PdfDocument();

        //遍历所有PDF 页面
        Dimension2D dimension2D = new Dimension();
        for (int i = 0; i < originalDoc.getPages().getCount(); i++) {
            PdfPageBase page = originalDoc.getPages().get(i);

            PdfMargins margins = new PdfMargins(0, 0, 0, 0);
            //设置新文档的页面大小为A4
            PdfPageBase newPage = newDoc.getPages().add(PdfPageSize.Arch_A, margins);
            //调整画布,设置内容也根据页面的大小进行缩放

            double wScale = (PdfPageSize.Arch_A.getWidth()) / PdfPageSize.Arch_A.getWidth();
            double hScale = (PdfPageSize.Arch_A.getHeight()) / PdfPageSize.Arch_A.getHeight();
            newPage.getCanvas().translateTransform(wScale, hScale);
            //复制原文档的内容到新文档
            newPage.getCanvas().drawTemplate(page.createTemplate(), new Point2D.Float());

            //保存PDF
            newDoc.saveToFile(outPath);

        }
        newDoc.close();
        return outPath;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Java修改多页PDF文件中的值,您可以使用Apache PDFBox库中的PDPageContentStream类和PDPage类。以下是一个示例代码,它将在PDF文件的所有页面中替换特定文本: ```java import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripperByArea; public class ModifyPDF { public static void main(String[] args) throws IOException { // Load the PDF document File file = new File("original.pdf"); PDDocument document = PDDocument.load(file); // Get the total number of pages in the document int pageCount = document.getNumberOfPages(); // Define the text to be replaced and the new text String oldText = "Old Text"; String newText = "New Text"; // Iterate through each page of the document for (int i = 0; i < pageCount; i++) { PDPage page = document.getPage(i); // Create a new content stream to add text PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true); // Define the font and font size contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12); // Define the position where the text will be added int x = 100; int y = 700; // Replace the old text with the new text PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); stripper.addRegion("searchRegion", page.getMediaBox()); stripper.extractRegions(page); for (String text : stripper.getTextForRegion("searchRegion").split(oldText)) { contentStream.beginText(); contentStream.newLineAtOffset(x, y); contentStream.showText(text); contentStream.endText(); x += PDType1Font.HELVETICA_BOLD.getStringWidth(text) / 1000 * 12; contentStream.beginText(); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12); contentStream.newLineAtOffset(x, y); contentStream.showText(newText); contentStream.endText(); x += PDType1Font.HELVETICA_BOLD.getStringWidth(newText) / 1000 * 12; } // Close the content stream contentStream.close(); } // Save the modified document document.save("modified.pdf"); // Close the document document.close(); } } ``` 该示例代码将在PDF文件的所有页面中查找名为"Old Text"的文本,并将其替换为"New Text"。请注意,在此示例中,我们使用PDFTextStripperByArea类来查找文本,并使用PDType1Font类来设置字体和字体大小。您可以根据您的需要修改此示例代码以在多页PDF文件中修改其他文本或属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值