簡體   English   中英

PDFBox - 從頁面中刪除圖像並在不同位置添加相同的圖像

[英]PDFBox - Delete image from the page and add same image on different position

我使用的是 PDFBox 2.x 版本,需要重新定位頁面中的現有圖像 為此,我從頁面中刪除圖像並重新添加。 但是在我當前的實現中,它沒有顯示任何圖像。

請幫我解決這個問題,讓我知道我哪里出錯了。

try {
  PDDocument document = PDDocument.load(new File("..\\sampleWithImage_with_barcode_img.pdf"));

  PDDocument newDocument = new PDDocument();
  for (int i = 0; i < document.getNumberOfPages(); i++) {
    PDPage sourcePage = document.getPage(i);
    PDPage pdPage = newDocument.importPage(sourcePage);
    pdPage.setResources(sourcePage.getResources());

    //To remove existing from the page
    stripUnusedImages(pdPage, newDocument);

    //ADD OTHER IMAGE
    PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\copy\\pic.jpg", newDocument);

    PDPageContentStream contents = new PDPageContentStream(newDocument, pdPage,
      PDPageContentStream.AppendMode.APPEND, true);

    // Drawing the image in the PDF document
    contents.drawImage(pdImage, 0, 0, 50, 30);

    System.out.println("Image inserted Successfully.");

    // Closing the PDPageContentStream object
    contents.close();
  }
  newDocument.save("..\\RemovedImage.pdf");
  document.close();
  newDocument.close();

} catch (Exception e) {
  e.printStackTrace();
}

//Method to remove image
protected void stripUnusedImages(PDPage page, PDDocument document) throws IOException, XmpParsingException {
  PDResources resources = copyResources(page);
  PDFStreamParser parser = new PDFStreamParser(page);
  parser.parse();

  List < Object > tokens = parser.getTokens();
  System.out.println("Total Tokens=" + tokens.size());
  List < Object > newTokens = new ArrayList < Object > ();
  for (int j = 0; j < tokens.size(); j++) {
    Object token = tokens.get(j);
    if (token instanceof COSName) {
      COSName cosname = (COSName) token;
      PDXObject o = resources.getXObject(cosname);
      if (o instanceof PDImageXObject) {
        PDImageXObject pdImageXObject = (PDImageXObject) o;
        if (pdImageXObject.getMetadata() != null) {
          System.out.println("pdImageXObjec metadata exist");
          newTokens.remove(newTokens.size() - 1);
          continue;
        }
      }
    }

    newTokens.add(token);
  }

  PDStream newContents = new PDStream(document);
  OutputStream outputStream = newContents.createOutputStream();
  ContentStreamWriter writer = new ContentStreamWriter(outputStream);
  writer.writeTokens(newTokens);
  outputStream.close();
  newContents.addCompression();
  page.setContents(newContents);
}

PDPageContentStream的構造如下:

PDPageContentStream contents = new PDPageContentStream(newDocument, pdPage,
  PDPageContentStream.AppendMode.APPEND, true);

Tilman Hausherr 在評論中提議添加第五個“true”參數

PDPageContentStream contents = new PDPageContentStream(newDocument, pdPage,
  PDPageContentStream.AppendMode.APPEND, true, true);

這會導致先前存在的頁面內容被封裝在保存圖形狀態/恢復圖形狀態框架中,該框架將最后的圖形狀態重置為原始狀態,以防止特別是當前轉換矩陣的變化影響新的補充。

此外,Ronak 根據他的要求的評論第三個參數值中的 AppendMode從 APPEND更改為 PREPEND 這會導致他的更改添加到后台,而不是前台。

所以PDPageContentStream構造的最終版本是這樣的:

PDPageContentStream contents = new PDPageContentStream(newDocument, pdPage,
  PDPageContentStream.AppendMode.PREPEND, true, true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM