PDF缩放/导入已裁剪/裁剪的页面 [英] PDF scaling/importing pages that have been trimmed/cropped

查看:704
本文介绍了PDF缩放/导入已裁剪/裁剪的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个应用程序,根据我们的要求格式化PDF文件,使用iText 2.1.7。

I am currently in the process of writing an app that "formats" PDF's to our requirements, using iText 2.1.7.

我们基本上采用肖像PDF,并缩小页面,因此我们可以在新PDF的一个横向页面上放置2页原始PDF。我们还在页面底部留出一些用于后期处理的空间。

We basically take a portrait PDF, and scale down the pages, so we can fit 2 pages of the original PDF, on one landscape page of the new PDF. We also leave some space at the bottom of the page which is used for post processing.

这个过程可以在90%的时间内正常工作。

This process works 90% of the time as it should.

但是,我们收到了内容部门裁剪/裁剪过的PDF文件,当我们在Acrobat中查看此PDF文件时,它看起来就像是一样。但是,当我们处理它时,新PDF包括整个原始MediaBox和裁剪线。

However, we received a PDF that has been cropped/trimmed by the content department, and when we view this PDF in Acrobat, it looks as excpected. However, when we process it, the new PDF includes the entire original MediaBox, and the crop lines.

这是我们使用的代码,以及问题输出的外观。

Here is the code we use, and how a problem output looks.

File tempFile = new File(tempFilename);
PdfReader reader = new PdfReader(originalPdfFile);
Document doc = new Document(new RectangleReadOnly(842f, 595f), 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(tempFile));
doc.open();
for (int i = 1; i < reader.getNumberOfPages(); i = i + 2) {
    doc.newPage();
    PdfContentByte cb = writer.getDirectContent();
    PdfImportedPage page = writer.getImportedPage(reader, i); // page #1

    float documentWidth = doc.getPageSize().getWidth() / 2;
    float documentHeight = doc.getPageSize().getHeight() - 65f;

    float pageWidth = page.getWidth();
    float pageHeight = page.getHeight();

    float widthScale = documentWidth / pageWidth;
    float heightScale = documentHeight / pageHeight;
    float scale = Math.min(widthScale, heightScale);

    float offsetX = (documentWidth - (pageWidth * scale)) / 2;
    float offsetY = 65f; //100f

    cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);

    PdfImportedPage page2 = writer.getImportedPage(reader, i+1); // page #2

    pageWidth = page.getWidth();
    pageHeight = page.getHeight();

    widthScale = documentWidth / pageWidth;
    heightScale = documentHeight / pageHeight;
    scale = Math.min(widthScale, heightScale);

    offsetX = ((documentWidth - (pageWidth * scale)) / 2) + documentWidth;
    offsetY = 65f; //100f

    cb.addTemplate(page2, scale, 0, 0, scale, offsetX, offsetY);//430f
    }

doc.close();

原创杂技:

在acrobat中修改,显示不需要的pretrim内容:

modified in acrobat, showing unwanted pretrim content:

推荐答案

在经历了很多挫折后,我终于通过硬裁剪PDF来进行缩放和布局处理。

after a lot of frustration, I finally got this to work, by "hard cropping" the PDF before doing my scaling and layout processing.

硬裁剪采用Acrobat裁剪PDF(裁剪=隐藏),并使用 PdfStamper 创建新PDF仅包含裁剪框内的内容。

The hard cropping takes an Acrobat cropped PDF (cropped = hidden), and uses PdfStamper to create a new PDF only containing the contents from inside the crop box.

public String cropPdf(String pdfFilePath) throws DocumentException, IOException {
    String filename = FilenameUtils.getBaseName(pdfFilePath) + "_cropped." + FilenameUtils.getExtension(pdfFilePath);
    filename = FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename);
    PdfReader reader = new PdfReader(pdfFilePath);
    try {
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
        try {
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                PdfDictionary pdfDictionary = reader.getPageN(i);
                PdfArray cropArray = new PdfArray();
                Rectangle cropbox = reader.getCropBox(i);                   
                cropArray.add(new PdfNumber(cropbox.getLeft()));
                cropArray.add(new PdfNumber(cropbox.getBottom()));
                cropArray.add(new PdfNumber(cropbox.getLeft() + cropbox.getWidth()));
                cropArray.add(new PdfNumber(cropbox.getBottom() + cropbox.getHeight()));
                pdfDictionary.put(PdfName.CROPBOX, cropArray);
                pdfDictionary.put(PdfName.MEDIABOX, cropArray);
                pdfDictionary.put(PdfName.TRIMBOX, cropArray);
                pdfDictionary.put(PdfName.BLEEDBOX, cropArray);
            }
            return filename;
        } finally {
            stamper.close();
        }
    } finally {
        reader.close();
    }
}

这篇关于PDF缩放/导入已裁剪/裁剪的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆