1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JAVA删除pdf空白页_如何从iText中的PDF中删除空白页面

JAVA删除pdf空白页_如何从iText中的PDF中删除空白页面

时间:2023-07-03 01:17:58

相关推荐

JAVA删除pdf空白页_如何从iText中的PDF中删除空白页面

我确信有几种方法.但这是我如何做到这一点的一个例子.我只检查页面上的数据量,如果它是< 20个字节我不包含它:

public void removeBlankPdfPages(String pdfSourceFile, String pdfDestinationFile, boolean debug)

{

try

{

// step 1: create new reader

PdfReader r = new PdfReader(pdfSourceFile);

RandomAccessFileOrArray raf = new RandomAccessFileOrArray(pdfSourceFile);

Document document = new Document(r.getPageSizeWithRotation(1));

// step 2: create a writer that listens to the document

PdfCopy writer = new PdfCopy(document, new FileOutputStream(pdfDestinationFile));

// step 3: we open the document

document.open();

// step 4: we add content

PdfImportedPage page = null;

//loop through each page and if the bs is larger than 20 than we know it is not blank.

//if it is less than 20 than we don't include that blank page.

for (int i=1;i<=r.getNumberOfPages();i++)

{

//get the page content

byte bContent [] = r.getPageContent(i,raf);

ByteArrayOutputStream bs = new ByteArrayOutputStream();

//write the content to an output stream

bs.write(bContent);

logger.debug("page content length of page "+i+" = "+bs.size());

//add the page to the new pdf

if (bs.size() > blankPdfsize)

{

page = writer.getImportedPage(r, i);

writer.addPage(page);

}

bs.close();

}

//close everything

document.close();

writer.close();

raf.close();

r.close();

}

catch(Exception e)

{

//do what you need here

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。