如何在iText中从PDF中删除空白页

如何在iText中从PDF中删除空白页,itext,Itext,我想从使用Java中的iText库生成的PDF中删除一个空白页 我该怎么做呢?我肯定有几种方法。但这里有一个我是如何做到这一点的例子。我只是检查页面上的数据量,如果数据量小于20字节,我不包括: public void removeBlankPdfPages(String pdfSourceFile, String pdfDestinationFile, boolean debug) { try { // step 1: creat

我想从使用Java中的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
        }
    }
public void removebankPDFPages(字符串pdfSourceFile、字符串pdfDestinationFile、布尔调试)
{
尝试
{
//步骤1:创建新的读卡器
PdfReader r=新PdfReader(pdfSourceFile);
RandomAccessFileOrArray raf=新的RandomAccessFileOrArray(pdfSourceFile);
文档=新文档(r.getPageSizeWithRotation(1));
//步骤2:创建侦听文档的编写器
PdfCopy writer=newpdfcopy(文档,新文件输出流(pdfDestinationFile));
//步骤3:我们打开文档
document.open();
//步骤4:我们添加内容
PdfImportedPage=null;
//循环浏览每一页,如果bs大于20,则它不是空的。
//如果小于20,则不包括空白页。
for(int i=1;i blankPdfsize)
{
page=writer.getImportedPage(r,i);
writer.addPage(第页);
}
bs.close();
}
//关闭一切
document.close();
writer.close();
raf.close();
r、 close();
}
捕获(例外e)
{
//在这里做你需要的
}
}
C#(根据kalyan的要求)

publicstaticvoidremovebankpdfpages(stringpdfsourcefile、stringpdfdestinationfile、booldebug){
//步骤0:设置最小页面大小
常数int blankPdfsize=20;
//步骤1:创建新的读卡器
var r=新的PDF阅读器(PDF源文件);
var raf=新的随机访问文件阵列(pdfSourceFile);
var文档=新文档(r.GetPageSizeWithRotation(1));
//步骤2:创建侦听文档的编写器
var writer=newpdfcopy(文档,新文件流(pdfDestinationFile,FileMode.Create));
//步骤3:我们打开文档
document.Open();
//步骤4:我们添加内容
PdfImportedPage=null;
//循环浏览每一页,如果bs大于20,则它不是空的。
//如果小于20,则不包括空白页。
对于(变量i=1;i blankPdfsize)
{
page=writer.GetImportedPage(r,i);
writer.AddPage(第页);
}
bs.Close();
}
//关闭一切
document.Close();
writer.Close();
raf.Close();
r、 Close();}

将字节数组写入memorystream只是为了获得memorystream的长度有什么意义?对不起@Simon,我不记得了。。。这是一个3年前的问题。我直接在B内容上做了比较。长度和效果。不确定u是否想麻烦更新答案:“我认为应该检查PDF页面的内容,在我的例子中,空白页面的大小为20,所以它通过了检查,我建议使用这行代码:String ExpDetry= PDFTEXTracrace.GetTextFromPage(PDFRADADER,PaGeNUM,新LoopTeXExcActron策略());并检查它是否为null或空
public static void removeBlankPdfPages(string pdfSourceFile, string pdfDestinationFile, bool debug) {

// step 0: set minimum page size
const int blankPdfsize = 20;

// step 1: create new reader
var r = new PdfReader(pdfSourceFile);
var raf = new RandomAccessFileOrArray(pdfSourceFile);
var document = new Document(r.GetPageSizeWithRotation(1));

// step 2: create a writer that listens to the document
var writer = new PdfCopy(document, new FileStream(pdfDestinationFile, FileMode.Create));

// 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 (var i=1 ; i <= r.NumberOfPages; i++)
{
    //get the page content
    byte[] bContent = r.GetPageContent(i, raf);
    var bs = new MemoryStream();

    //write the content to an output stream
    bs.Write(bContent, 0, bContent.Length);
    Console.WriteLine("page content length of page {0} = {1}", i, bs.Length);

    //add the page to the new pdf
    if (bs.Length > blankPdfsize)
    {
        page = writer.GetImportedPage(r, i);
        writer.AddPage(page);
    }
    bs.Close();
}
//close everything
document.Close();
writer.Close();
raf.Close();
r.Close();}