C#(按照kalyan的要求)
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();}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16380 次 |
最近记录: |