您使用
为相关页面创建额外的内容流
PDPageContentStream contentStream = 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
因此,在创建此内容流时,您会丢弃该页面上的所有内容。
每当您想添加到现有内容时,请使用不同的构造函数,例如
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);
记录为
/**
* 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
顺便说一句:您提到您是PDFBox 和 Boxable 的新手,所以我假设您使用的是当前版本,尤其是 PDFBox 2.0.x。如果出于某种原因您选择使用旧版本(例如 1.8.x),则需要
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true, true);
改为。