2016-12-26 5 views
0

내가pdfbox 내가이 PDF 내용을 확장하고 지정된에서이 내용으로 새 PDF를 생성하는 클래스를 만든 제대로

을 "pdfbox - 응용 프로그램-2.0.0-RC2.jar"를 사용하고 수정 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 위의 방법에서 ResultPDf에서 누락 된 오른쪽 모서리에서 내용이 Modified file
: 다음

내 방법입니다. 크기 조정 값으로 새 페이지에 주석을 표시하기 위해 이 Result pdf from the above method

+1

누락 된 부분에는 "주석"이라는 단어가 포함되어 있습니다. 따라서 필자는 실제로 * pdf 주석이라고 가정합니다. 주석은 일반 내용의 일부가 아니며 ** 자체 내용이있는 페이지와 느슨하게 연결된 별도의 항목입니다. 반대로 XObjects (소스 페이지 내용을 가져 오는 것과 같은)에는 주석을 사용할 수 없습니다. 따라서 코드에 의해 무시됩니다. 2.0.0 릴리스가 이미 발표되었지만 2.0.0 릴리스 후보를 사용하는 이유는 무엇입니까? – mkl

+0

현재 2.0.4입니다. RC2는 1 살입니다. 새로운 릴리스를 만들기 위해 왜 열심히 노력했는지 궁금합니다. 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());