将PDF页面拆分为两部分 [英] Split a PDF page in two parts

查看:633
本文介绍了将PDF页面拆分为两部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想采用单页PDF,而不是将其拆分为两部分(在中间切割该页面),而不考虑该页面上的文本。我正在使用iText,但我没有找到任何关于如何做到这一点的例子。

I would like to take a single-page PDF, and than split it in two parts (cutting that page in the middle), without considering the text on that page. I'm using iText, but I don't find any examples on how to do this.

推荐答案

你不能真正拆分页面,这将是一项相当困难的任务,你可以做的就是克隆一个内容在原始大小的一半的新页面内,并重复第二页应用内容的翻译。

You cannot really split a page, it would be a quite difficult task, what you can do is to clone content of a page inside a new one with half its original size, and repeat for the second page applying a translation to the content.

我用 PDFBox ,我最近正在使用它,我有一个准备进行测试的沙箱项目,肯定你可以用iText做同样的事情。

I show an example with PDFBox , I'm using it lately and I had a sandbox project ready to do the test, surely you can do the same with iText.

package printit;

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.common.PDRectangle;

public class CutIt {
    public static void main(String[] args) throws IOException {
        PDDocument outdoc = new PDDocument();
        PDDocument doc = PDDocument.load(new File("sample_1.pdf"));
        PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);

        PDRectangle cropBox = page.getCropBox();
        float upperRightY = cropBox.getUpperRightY();
        float lowerLeftY = cropBox.getLowerLeftY();

        cropBox.setLowerLeftY(upperRightY/2);
        page.setCropBox(cropBox);
        outdoc.importPage(page);


        cropBox = page.getCropBox();
        cropBox.setUpperRightY(upperRightY/2);
        cropBox.setLowerLeftY(lowerLeftY);
        page.setCropBox(cropBox);
        outdoc.importPage(page);

        outdoc.save("cut.pdf");
        outdoc.close();


        doc.close();
    }
}

这篇关于将PDF页面拆分为两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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