提取pdf页面并插入现有的pdf [英] Extract pdf page and insert into existing pdf

查看:119
本文介绍了提取pdf页面并插入现有的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 iText 代码,我想将一页从src pdf文件复制到其他pdf文件(我已经存在 PdfStamper ,这里是 mainPdfStamper )。

I have below iText code, I want to copy one page from src pdf file to other pdf file(I have existing PdfStamper, here it is mainPdfStamper).

PdfReader srcReader = new PdfReader(new FileInputStream("source.pdf"));

File file = File.createTempFile("temporary", ".pdf");

PdfStamper pdfStamper = new PdfStamper(srcReader, new FileOutputStream(file));
PdfImportedPage importedPage = pdfStamper.getImportedPage(srcReader, 1);

// copying extracted page from src pdf to existing pdf
mainPdfStamper.getOverContent(1).addTemplate(importedPage, 10,10);
pdfStamper.close();
srcReader.close();

这不起作用,我不知道如何实现这一点。简而言之,我想从源pdf复制一页到现有的pdf。请帮忙。

This is not working and I am not aware of how to achieve this. In short, I want to copy one page from source pdf to existing pdf. Please help.

更新

以下代码根据Bruno的回答工作。

Below code worked as per the answer from Bruno.

    PdfReader reader2 = new PdfReader(srcPdf.getAbsolutePath());
    PdfImportedPage page = pdfStamper.getImportedPage(reader2, 1);
    stamper.insertPage(1, reader2.getPageSize(1));
    pdfStamper.getUnderContent(1).addTemplate(page, 100, 100);
    // Close the stamper and the readers
    pdfStamper.close();
    reader2.close();


推荐答案

请阅读文档,例如 iText in Action的第6章。如果你转到第6.3.4节(将页面插入现有文档),你会发现 InsertPages 示例。

Please read the documentation, for instance chapter 6 of iText in Action. If you go to section 6.3.4 ("Inserting pages into an existing document"), you'll find the InsertPages example.

如果 p 是页码,则只需要此代码在您要插入页面的位置, main_file 是主文件的路径, to_be_inserted 该文件的路径需要插入( dest 是生成文件的路径):

You only need this code if p is the page number indicating where you want to insert the page, main_file is the path to your main file and to_be_inserted the path to the file that needs to be inserted (dest is the path to the resulting file):

PdfReader reader = new PdfReader(main_file);
PdfReader reader2 = new PdfReader(to_be_inserted);
// Create a stamper
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// Create an imported page to be inserted
PdfImportedPage page = stamper.getImportedPage(reader2, 1);
stamper.insertPage(p, reader2.getPageSize(1));
stamper.getUnderContent(i).addTemplate(page, 0, 0);
// Close the stamper and the readers
stamper.close();
reader.close();
reader2.close();

这只是一种组合来自两个文件的页面的方法。您也可以使用 PdfCopy 来实现此目的。使用 PdfCopy 的优点是您将保留交互式页面的交互功能。使用 PdfStamper 时,您将丢失插入页面中出现的任何互动功能(例如所有链接)。

This is only one way to combine pages from two files. You can also use PdfCopy for this purpose. The advantage of using PdfCopy is the fact that you'll preserve the interactive features of the interactive page. When using PdfStamper, you'll lose any interactive feature (e.g. all links) that were present in the inserted page.

这篇关于提取pdf页面并插入现有的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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