![](/img/trans.png)
[英]How to move image to the top of the PDF page using Apache PDFBox?
[英]Problem with empty page when using Apache PDFBox to add image to PDF
我正在使用此代碼: https : //www.tutorialspoint.com/pdfbox/pdfbox_inserting_image.htm
幫助我將圖像添加到現有 PDF。 問題是它創建的文件是一個空白頁,上面只有圖像。
這是我的代碼:
public void signPDF(PdfDTO pdfDTO) throws IOException{
//Loading an existing document
File file = new File(getAbsolutePdfPath(pdfDTO));
PDDocument doc = PDDocument.load(file);
//Retrieving the page
PDPage page = doc.getPage(0);
//a test to ensure the doc is loading correctly
PDDocument testDoc = new PDDocument();
testDoc.addPage(page);
testDoc.save("C:" + File.separator + "Users" + File.separator + "kdotson" + File.separator + "Documents" + File.separator + "test.pdf");
testDoc.close(); //this file is good so I know the doc is loading correctly
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile("C://test_images/signature.pdf", doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page);
//Drawing the image in the PDF document
contents.drawImage(pdImage, 0, 0);
//Closing the PDPageContentStream object
contents.close();
//Saving the document
doc.save(new File(getSignedPdfLocation(pdfDTO))); //the created file has the image on it, so I know the image is loading correctly
//Closing the document
doc.close();
}
據我所知,我正在做的事情應該可以工作,而且我沒有收到任何錯誤,那么有什么用呢?
還請查看您嘗試使用的庫的 JavaDoc 和源代碼。 您創建一個PDPageContentStream
:
PDPageContentStream contents = new PDPageContentStream(doc, page);
該指揮被記錄為覆蓋此頁面的所有現有內容流:
/**
* Create a new PDPage content stream. This constructor overwrites all existing content streams
* of this page.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException
因此,您必須使用不同的構造函數來保留當前頁面內容,例如
/**
* Create a new PDPage content stream.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @param appendContent Indicates whether content will be overwritten, appended or prepended.
* @param compress Tell if the content stream should compress the page contents.
* @param resetContext Tell if the graphic context should be reset. This is only relevant when
* the appendContent parameter is set to {@link AppendMode#APPEND}. You should use this when
* appending to an existing stream, because the existing stream may have changed graphic
* properties (e.g. scaling, rotation).
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
boolean compress, boolean resetContext) throws IOException
因此
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
應該使您的代碼按需要工作。
或者,如果您想要背景中的圖像,請嘗試
PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.PREPEND, true, true);
但是請注意,在某些情況下,圖像在背景中不可見,例如,如果現有內容以用白色填充整個頁面區域的指令開始。 在這種情況下,水印必須以某種透明度應用在現有內容之上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.