将一页PDF文件分为两页PDF文件 [英] Divide one page PDF file in two pages PDF file

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

问题描述

我正在使用iTextSharp来处理pdf文件。我想知道如何将页面分成两半并从两个页面中分成两个不同的页面。我尝试了很多,但现在似乎没有任何工作。

I'm using iTextSharp to handle pdf files. I'd like to know how I can split a page in half and make 2 different pages from the two pieces. I tried a lot but nothing seems to work right now.

首次尝试

iTextSharp.text.Rectangle size = new iTextSharp.text.Rectangle(0, pdfReader.GetPageSize(1).Height / 2, pdfReader.GetPageSize(1).Width, 0);

第二次尝试

iTextSharp.text.Rectangle size = pdfReader.GetPageSizeWithRotation(1);
iTextSharp.text.Document document = new iTextSharp.text.Document(size.GetRectangle(0, size.Height / 2));

还有其他几个。结果总是一样的:我的文件只有原始页面的后半部分。

And several others. The results are always the same: I have a file with just the second half of the original page.

推荐答案

我不知道理解你的代码片段,然后再说一遍:可能你也不理解它们,所以我们不要看你到目前为止所写的内容,让我们仔细看看 TileInTwo 示例:

I don't understand your code snippets, but then again: probably you don't understand them either, so let's not look at what you've written so far, and let's take a closer look at the TileInTwo example:

public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    // Creating a reader
    PdfReader reader = new PdfReader(src);
    int n = reader.getNumberOfPages();
    // step 1
    Rectangle mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(1)));
    Document document = new Document(mediabox);
    // step 2
    PdfWriter writer
        = PdfWriter.getInstance(document, new FileOutputStream(dest));
    // step 3
    document.open();
    // step 4
    PdfContentByte content = writer.getDirectContent();
    PdfImportedPage page;
    int i = 1;
    while (true) {
        page = writer.getImportedPage(reader, i);
        content.addTemplate(page, 0, -mediabox.getHeight());
        document.newPage();
        content.addTemplate(page, 0, 0);
        if (++i > n)
            break;
        mediabox = new Rectangle(getHalfPageSize(reader.getPageSizeWithRotation(i)));
        document.setPageSize(mediabox);
        document.newPage();
    }
    // step 5
    document.close();
    reader.close();
}

public Rectangle getHalfPageSize(Rectangle pagesize) {
    float width = pagesize.getWidth();
    float height = pagesize.getHeight();
    return new Rectangle(width, height / 2);
}

在这个例子中,我们要求 PdfReader 第一页的页面大小的实例,我们创建一个宽度相同但只有一半高度的新矩形。

In this example, we ask the PdfReader instance for the page size of the first page and we create a new rectangle with the same width and only half the height.

然后我们导入每个页面在文档中,我们在不同的页面上添加两次:

We then import each page in the document, and we add it twice on different pages:


  • 一次在奇数页面上,负数 y 显示原始页面上半部分的值,

  • 一次在偶数页面上 y = 0 显示原始页面的下半部分。

  • once on the odd pages with a negative y value to show the upper half of the original page,
  • once on the even pages with y = 0 to show the lower half of the original page.

由于原始文档中的每个页面都可以有不同的大小,我们可能需要更改每个新页面的页面大小。

As every page in the original document can have a different size, we may need to change the page size for every new couple of pages.

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

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