Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用iText从pdf文件中提取一页_Java_Google App Engine_Pdf_Itext - Fatal编程技术网

Java 使用iText从pdf文件中提取一页

Java 使用iText从pdf文件中提取一页,java,google-app-engine,pdf,itext,Java,Google App Engine,Pdf,Itext,我想使用itext库从JavaServlet返回pdf文件中的一页(以减少文件大小下载)。 使用此代码 try { PdfReader reader = new PdfReader(input); Document document = new Document(reader.getPageSizeWithRotation(page_number) ); PdfSmartCopy copy1 = new PdfSmartCopy(do

我想使用itext库从JavaServlet返回pdf文件中的一页(以减少文件大小下载)。 使用此代码

     try {
        PdfReader reader = new PdfReader(input);
        Document document = new Document(reader.getPageSizeWithRotation(page_number) );


        PdfSmartCopy copy1 = new PdfSmartCopy(document, response.getOutputStream());
        copy1.setFullCompression();
        document.open();

        copy1.addPage(copy1.getImportedPage(reader, page_i) );
        copy1.freeReader(reader);
        reader.close();

        document.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    }

此代码返回页面,但文件大小很大,有时甚至只是一个页面,也与原始文件大小相等。

我从您的存储库下载了一个文件:abumbient.pdf

然后,我使用以下代码“爆破”该PDF:

public static void main(String[] args) throws DocumentException, IOException {
    PdfReader reader = new PdfReader("resources/Abdomen.pdf");
    int n = reader.getNumberOfPages();
    reader.close();
    String path;
    PdfStamper stamper;
    for (int i = 1; i <= n; i++) {
        reader = new PdfReader("resources/abdomen.pdf");
        reader.selectPages(String.valueOf(i));
        path = String.format("results/abdomen/p-%s.pdf", i);
        stamper = new PdfStamper(reader,new FileOutputStream(path));
        stamper.close();
        reader.close();
    }
}
publicstaticvoidmain(字符串[]args)抛出DocumentException、IOException{
PdfReader reader=新的PdfReader(“参考资料/accessure.pdf”);
int n=reader.getNumberOfPages();
reader.close();
字符串路径;
PdfStamper压模;

对于(int i=1;i更新多一点,更干净(5.5.6及更高版本):


也许你的每一页都使用大字体或大图像。在这种情况下,如果你在100页中扔掉99页也没关系:那一页仍然需要字体和图像,而且你的PDF文件的大小不会减少。如果你想要一个更有用的答案,你必须给我们更多关于你的PDF文件性质的信息文件的副本在dropbox中,大部分页面都是论文的图片。我下载了Absummit.pdf。它是一个只包含图片的pdf。为什么不提取图片并提供这些图片?如果可以提供单独的图片,为什么需要单页pdf?这些文件附带文本,如(Special Embryology.pdf)返回较小的文件,但我认为返回图像是最好的解决方案,因为大多数文件都是由图像组成的,我想你可能会重复,但对于某些文件,如(Introduction 2013.pdf),生成的文件非常大,请参阅此链接()生成的文件大小约为12 MB,相当于pdf大小。我不再在电脑前,而是在手机上。如果您想让我测试某个特定文件,您应该这样说。这将使您在将来更加精确。@Ahmed在文件简介2013.pdf中,您遇到的问题与问题中讨论的相同:所有页面都是s共享一个资源字典,在您的情况下为4 0 R。因此,拆分会复制每个页面的所有资源。因此,在拆分之前,您应该优化PDF,使每个页面都有单独的资源字典,其中只包含该页面上实际使用的资源,请参阅我对另一个问题的回答。这是一个很好的答案,@mkl a它表明一些PDF并没有按照它们应该的方式创建。。。
/**
 * Manipulates a PDF file src with the file dest as result
 * @param src the original PDF
 * @param dest the resulting PDF
 * @throws IOException
 * @throws DocumentException
 */
public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    SmartPdfSplitter splitter = new SmartPdfSplitter(reader);
    int part = 1;
    while (splitter.hasMorePages()) {
        splitter.split(new FileOutputStream("results/merge/part_" + part + ".pdf"), 200000);
        part++;
    }
    reader.close();
}