2016-12-26 124 views
0

我使用「PDFBOX-APP-2.0.0-RC2.jar」PDFBOX不渲染修改PDF文件正確

我創建了按比例縮小的PDF格式的內容以及與此內容在指定創建新的PDF類座標。

我的問題是如果PDF文件已被修改並添加新的內容,比縮小文件中缺少此內容。

以下是我的方法:

public static String scaleInputPdf(String sourceFileName, float xpos, 
     float ypos, float imgCtrlWidth, float imgCtrlHeight, 
     PDRectangle pageSize) throws IOException { 

    String workdir = "/tmp"; 

    File tmpDir = new File(workdir); 
    if(! tmpDir.exists()) { 
     tmpDir.mkdir(); 
    } 

    String destPath = workdir + "/Result.pdf"; 

    PDDocument sourceDoc = null; 
    try { 
     // load the source PDF 
     sourceDoc = PDDocument.load(new File(sourceFileName)); 

     // create a new PDF and add a blank page 
     PDDocument doc = new PDDocument(); 

     for (int sourcePageNo = 0; sourcePageNo < sourceDoc 
       .getNumberOfPages(); sourcePageNo++) { 

      PDPage page = new PDPage(); 
      page.setMediaBox(pageSize); 

      doc.addPage(page); 

      PDPage currentSourcePage = sourceDoc.getPage(sourcePageNo); 

      PDPageContentStream contents = new PDPageContentStream(doc, 
        page, true, true, true); 

      float scaleFactor = getScalingFactor(currentSourcePage, 
        imgCtrlWidth, imgCtrlHeight, pageSize); 

      PDFormXObject form = null; 

      // Create a Form XObject from the source document using 
      // LayerUtility 
      LayerUtility layerUtility = new LayerUtility(doc); 
      form = layerUtility.importPageAsForm(sourceDoc, 
        sourcePageNo); 

      // draw a scaled form 
      contents.saveGraphicsState(); 

      Matrix scalingMatrix = Matrix.getScaleInstance(scaleFactor, 
        scaleFactor); 
      contents.transform(scalingMatrix); 

      Matrix translatingMatrix = Matrix.getTranslateInstance(
        xpos, 
        getYPosition(currentSourcePage, pageSize, ypos, 
          scaleFactor)); 
      contents.transform(translatingMatrix); 

      contents.drawForm(form); 

      contents.restoreGraphicsState(); 
      contents.close(); 

     } 
     doc.save(destPath); 
     doc.close(); 

    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } finally { 
     if (sourceDoc != null) { 
      sourceDoc.close(); 

     } 
    } 
    System.err.println("Done! /n file is at following location=" +destPath); 
    return destPath; 

} 

private static float getScalingFactor(PDPage sourcePage, 
     float imgCtrlWidth, float imgCtrlHeight, PDRectangle pageSize) { 

    PDRectangle imageSize = sourcePage.getMediaBox(); 

    if (imageSize.getHeight() > 792) { 

     if (pageSize.equals(PDRectangle.LETTER)) { 
      imgCtrlWidth = 838.8f; // 932 * 0.90 
      imgCtrlHeight = 1018f; // 1132 *0.90 
     } else if (pageSize.equals(PDRectangle.A4)) { 
      imgCtrlWidth = 819f; // 910 * 0.90 
      imgCtrlHeight = 972f; // 1080 * 0.90 
     } 
    } 

    float scaleX = imgCtrlWidth/imageSize.getWidth(); 

    float scaleY = imgCtrlHeight/imageSize.getHeight(); 

    float scaleFactor = ((scaleX < scaleY) ? scaleX : scaleY); 

    if (imageSize.getHeight() > 792 && scaleFactor > 0.88) { 
     scaleFactor = 0.88f; 
    } 

    return scaleFactor; 
} 

private static float getYPosition(PDPage sourcePage, 
     PDRectangle finalDocPageSize, float yMarginPos, float scaleFactor) { 

    float yPos = yMarginPos; 

    float imgHeight = sourcePage.getMediaBox().getHeight(); 

    PDRectangle cropBox = sourcePage.getCropBox(); 

    if (imgHeight > 792) { 

     if (cropBox.getLowerLeftY() != 0) { 

      yPos = cropBox.getLowerLeftY() + 40; 
     } 

    } else { 

     yPos = (finalDocPageSize.getHeight() - (imgHeight * scaleFactor + yMarginPos)); 
    } 

    return yPos; 
} 
} 

在所附的圖像ModifiedFile Modified file
有在右上角,這是從上述方法ResultPDf遺漏的內容。 Result pdf from the above method

+1

缺少部分包含單詞「Annotation」。因此,我認爲它實際上是* pdf註釋。註釋不是常規內容的一部分,而是與具有自己內容的頁面鬆散關聯的獨立實體。與之相反,XObjects(您將其導入源頁面內容)的表單不能有註釋。因此,它們被您的代碼忽略。順便說一下,即使2.0.0版本已經發布,爲什麼還要使用2.0.0版本的候選版本呢? – mkl

+0

現在我們在2.0.4。 RC2已經有一年了。讓我想知道爲什麼我們努力工作來製作新版本LOL –

+0

首先非常感謝你回覆:) –

回答

0

添加以顯示新的頁面上標註並調整其大小的值下面的代碼:

列表anotn = currentSourcePage.getAnnotations();

  for(PDAnnotation an :anotn) { 
       PDRectangle rec = an.getRectangle(); 

       PDRectangle currentSourcePageMBox = currentSourcePage.getMediaBox(); 

       boolean isLandscape = currentSourcePageMBox.getWidth() > currentSourcePageMBox.getHeight(); 

       if(isLandscape) { 

        rec.setLowerLeftY(((rec.getLowerLeftY()) * scaleFactor) + 180 + 2 * ypos); 
        rec.setUpperRightY(((rec.getUpperRightY()) * scaleFactor) + 180 + 2*ypos); 
       } 
       else { 
        rec.setLowerLeftY(((rec.getLowerLeftY()) * scaleFactor) + ypos); 
        rec.setUpperRightY(((rec.getUpperRightY()) * scaleFactor) + ypos); 
       } 

       rec.setLowerLeftX((rec.getLowerLeftX() + xpos) * scaleFactor); 
       rec.setUpperRightX((rec.getUpperRightX() + xpos) * scaleFactor);     

       an.setRectangle(rec); 
      } 

      // get all annotations and set on the new page. 
      page.setAnnotations(currentSourcePage.getAnnotations());