Java PDFBox-从页面中删除图像,并在不同位置添加相同的图像

Java PDFBox-从页面中删除图像,并在不同位置添加相同的图像,java,pdfbox,Java,Pdfbox,我正在使用PDFBox 2.x版本,需要重新定位页面中的现有图像。为此,我将从页面中删除图像,然后重新添加。但是,在我当前的实现中,这两种图像都没有显示 请帮我解决这个问题,让我知道哪里出了问题 try { PDDocument document = PDDocument.load(new File("..\\sampleWithImage_with_barcode_img.pdf")); PDDocument newDocument = new PDDocumen

我正在使用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);
}
试试看{
PDDocument document=PDDocument.load(新文件(“..\\sampleWithImage\u with\u barcode\u img.pdf”);
PDDocument newDocument=新PDDocument();
对于(int i=0;itokens=parser.getTokens();
System.out.println(“Total Tokens=“+Tokens.size());
ListnewTokens=newarraylist();
对于(int j=0;jPDPageContentStream的构造如下:

PDPageContentStream contents = new PDPageContentStream(newDocument, pdPage,
  PDPageContentStream.AppendMode.APPEND, true);
Tilman Hausherr在评论中建议添加第五个“true”参数:

这将导致以前存在的页面内容被封装在“保存图形状态/恢复图形状态”框架中,该框架将最后的图形状态重置为原始状态,以防止更改(尤其是当前转换矩阵的更改)影响新添加的内容

Ronak还根据其要求的注释将第三个参数值中的AppendMode从APPEND更改为PREPEND。这将导致他的更改添加到背景中,而不是前景中

因此,
PDPageContentStream
结构的最终版本如下:

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

对于PDPageContentStream构造函数,请尝试添加第五个“true”parameter@TilmanHausherr感谢您的回复,但不幸的是,它没有起作用。请检查页面的裁剪框:您是否可能将图像放置在其外部?@mkl下面的代码帮助了我。根据@Tilman Hausherr的建议,在PDPageContentStream构造函数中添加true作为第五个参数值,该构造函数重置图形上下文,并根据我的要求,在第三个参数中将AppendMode从APPEND更改为PREPEND价值观<代码>新建PDPageContentStream(newDocument、pdPage、PDPageContentStream.AppendMode.PREPEND、true、true)请将此添加为答案,因为我的帐户不接受答案。
PDPageContentStream contents = new PDPageContentStream(newDocument, pdPage,
  PDPageContentStream.AppendMode.PREPEND, true, true);