2013-08-28 113 views
1

我目前正在編寫一個應用程序,它使用iText 2.1.7將PDF格式「轉換爲」我們的需求。PDF縮放/導入已修剪/裁剪的頁面

我們基本上採用肖像PDF,並縮小頁面大小,因此我們可以在新PDF的一個橫向頁面上放置原始PDF的2頁。我們還在頁面底部留出一些空間用於後期處理。

這個過程的工作時間應該是90%。

但是,我們收到了由內容部門裁剪/裁剪的PDF,當我們在Acrobat中查看此PDF時,它看起來像是被重視。但是,當我們處理它時,新的PDF包含整個原始MediaBox和裁切線。

下面是我們使用的代碼以及問題輸出的外觀。

File tempFile = new File(tempFilename); 
PdfReader reader = new PdfReader(originalPdfFile); 
Document doc = new Document(new RectangleReadOnly(842f, 595f), 0, 0, 0, 0); 
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(tempFile)); 
doc.open(); 
for (int i = 1; i < reader.getNumberOfPages(); i = i + 2) { 
    doc.newPage(); 
    PdfContentByte cb = writer.getDirectContent(); 
    PdfImportedPage page = writer.getImportedPage(reader, i); // page #1 

    float documentWidth = doc.getPageSize().getWidth()/2; 
    float documentHeight = doc.getPageSize().getHeight() - 65f; 

    float pageWidth = page.getWidth(); 
    float pageHeight = page.getHeight(); 

    float widthScale = documentWidth/pageWidth; 
    float heightScale = documentHeight/pageHeight; 
    float scale = Math.min(widthScale, heightScale); 

    float offsetX = (documentWidth - (pageWidth * scale))/2; 
    float offsetY = 65f; //100f 

    cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY); 

    PdfImportedPage page2 = writer.getImportedPage(reader, i+1); // page #2 

    pageWidth = page.getWidth(); 
    pageHeight = page.getHeight(); 

    widthScale = documentWidth/pageWidth; 
    heightScale = documentHeight/pageHeight; 
    scale = Math.min(widthScale, heightScale); 

    offsetX = ((documentWidth - (pageWidth * scale))/2) + documentWidth; 
    offsetY = 65f; //100f 

    cb.addTemplate(page2, scale, 0, 0, scale, offsetX, offsetY);//430f 
    } 

doc.close(); 

原來在Acrobat中:

enter image description here

修改雜技演員,顯示不希望pretrim內容:

enter image description here

回答

1

經過很多挫折之後,我終於得到了這個工作,在做我的縮放和佈局處理之前,通過「硬裁剪」PDF。

硬裁切需要Acrobat裁剪的PDF(裁剪=隱藏),並使用PdfStamper創建一個新的PDF,其中只包含裁剪框內的內容。

public String cropPdf(String pdfFilePath) throws DocumentException, IOException { 
    String filename = FilenameUtils.getBaseName(pdfFilePath) + "_cropped." + FilenameUtils.getExtension(pdfFilePath); 
    filename = FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename); 
    PdfReader reader = new PdfReader(pdfFilePath); 
    try { 
     PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename)); 
     try { 
      for (int i = 1; i <= reader.getNumberOfPages(); i++) { 
       PdfDictionary pdfDictionary = reader.getPageN(i); 
       PdfArray cropArray = new PdfArray(); 
       Rectangle cropbox = reader.getCropBox(i);     
       cropArray.add(new PdfNumber(cropbox.getLeft())); 
       cropArray.add(new PdfNumber(cropbox.getBottom())); 
       cropArray.add(new PdfNumber(cropbox.getLeft() + cropbox.getWidth())); 
       cropArray.add(new PdfNumber(cropbox.getBottom() + cropbox.getHeight())); 
       pdfDictionary.put(PdfName.CROPBOX, cropArray); 
       pdfDictionary.put(PdfName.MEDIABOX, cropArray); 
       pdfDictionary.put(PdfName.TRIMBOX, cropArray); 
       pdfDictionary.put(PdfName.BLEEDBOX, cropArray); 
      } 
      return filename; 
     } finally { 
      stamper.close(); 
     } 
    } finally { 
     reader.close(); 
    } 
} 
3

雖然很難沒有看到PDF本身可以肯定,我懷疑你的問題是,這個PDF至少在一些指定CropBox的頁面。如果是這樣的話,那麼我認爲在獲得頁面引用之後你會想要做類似page.setBoundingBox(reader.getCropBox(i));的事情。

注意的默認值頁面的CropBox是它的MediaBox,所以除上述線路的不應該是沒有指定CropBox PDF頁面的佈局產生負面影響。

(我不是一個iText的用戶,所以這是一個有點在我的部分猜測...)

祝你好運!

1

一個微小但重要的修復程序Kabals答案:盒預期寬度/高度,而不是座標:

  ... 
      cropArray.add(new PdfNumber(cropbox.getLeft())); 
      cropArray.add(new PdfNumber(cropbox.getBottom())); 
      cropArray.add(new PdfNumber(cropbox.getWidth())); 
      cropArray.add(new PdfNumber(cropbox.getHeight())); 
      ...