iText Pdf页面字节大小 [英] iText Pdf Page Byte Size

查看:434
本文介绍了iText Pdf页面字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项业务要求,要求我将pdf拆分为多个文档。
假设我有一个100MB的pdf,为了简单起见,我需要将其分成多个不超过10MB的pdf文件。

I have a business requirement that requires me to splits pdfs into multiple documents. Lets say I have a 100MB pdf, I need to split that into for simplicity sake, into multiple pdfs no larger than 10MB a piece.

我正在使用iText。

I am using iText.

我将获取原始的pdf,并循环浏览页面,但如何确定每个页面的文件大小而不将其单独写入磁盘?

I am going to get the original pdf, and loop through the pages, but how can I determine the file size of each page without writing it separately to the disk?

简单示例代码



int numPages = reader.getNumberOfPages();

PdfImportedPage page;
for (int currentPage = 0; currentPage < numPages; ){
    ++currentPage;
    //Get page from reader
    page = writer.getImportedPage(reader, currentPage);

// I need the size in bytes here of the page

}   


推荐答案

我认为最简单的方法是将其写入磁盘并在之后删除它:

I think the easiest way is to write it to the disk and delete it afterwards:

Document document = new Document();
File f= new File("C:\\delete.pdf");  //for instance
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
document.open();
document.add(page);
document.close();
long filesize = f.length();   //this is the filesize in byte
f.delete();

我承认,我不是很确定,但我不知道应该怎么做如果文件不存在,找出文件大小。

I'm not absolutely sure, I admit, but I don't know how it should be possible to figure out the filesize if the file is not existing.

这篇关于iText Pdf页面字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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