如何在 运行 次叠加两个文档
How to Overlay two documents at run time
我需要在 运行 时间添加水印文本,即。在创建文档时,我需要添加水印文本。我最初的方法是从文档中获取所有页面并在这些页面上添加我的文本。我确实工作了,但问题是我的水印消息出现在哪里它隐藏了我的页面内容。请查看我的初始方法的代码。
List pages = document.getDocumentCatalog().getAllPages();
float fontSize = 70.0f;
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) pages.get(i);
PDRectangle pageSize = page.findMediaBox();
float stringWidth = pdfFont.getStringWidth(text) * fontSize
/ 1000f;
// calculate to center of the page
int rotation = page.findRotation();
boolean rotate = degree > 0;
float pageWidth = rotate ? pageSize.getHeight() : pageSize
.getWidth();
float pageHeight = rotate ? pageSize.getWidth() : pageSize
.getHeight();
double centeredXPosition = rotate ? pageHeight / 2f
: (pageWidth - stringWidth) / 2f;
double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f
: pageHeight / 2f;
// append the content to the existing stream
PDPageContentStream contentStream = new PDPageContentStream(
document, page, true, true, true);
contentStream.beginText();
// set font and font size
contentStream.setFont(pdfFont, fontSize);
// set text color to red
contentStream.setNonStrokingColor(240, 240, 240);
if (rotate) {
// rotate the text according to the page rotation
contentStream.setTextRotation(degree, x, y);
} else {
contentStream.setTextTranslation(centeredXPosition,
centeredYPosition);
}
contentStream.drawString(text);
contentStream.endText();
contentStream.close();
我已经阅读了有关 Overlay 的信息并且已经尝试过,所以我尝试改变我的方法,因为我认为只有 Overlay 可以满足我的要求。我目前的做法是:
public PDDocument createWatermarkText() {
PDDocument watermarkDoc = new PDDocument();
PDPage watermarkPage = new PDPage();
try {
watermarkDoc.addPage(watermarkPage);
PDPageContentStream content = new PDPageContentStream(watermarkDoc,
watermarkPage);
content.setFont(pdfFont, fontSize);
content.beginText();
content.moveTextPositionByAmount(x, y);
content.setNonStrokingColor(255, 0, 0);
PDRectangle pageSize = watermarkPage.findMediaBox();
float stringWidth = pdfFont.getStringWidth(text) * fontSize / 1000f;
// int rotation = page.findRotation();
boolean rotate = degree > 0;
float pageWidth = rotate ? pageSize.getHeight() : pageSize
.getWidth();
float pageHeight = rotate ? pageSize.getWidth() : pageSize
.getHeight();
double centeredXPosition = rotate ? pageHeight / 2f
: (pageWidth - stringWidth) / 2f;
double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f
: pageHeight / 2f;
content.setTextRotation(degree, x, y);
content.drawString(text);
content.endText();
content.close();
// ColumnText.showTextAligned(writer.getDirectContentUnder(), align,
// new Phrase(text, pdfFont), x, y,
// degree);
} catch (IOException e) {
e.printStackTrace();
}
return watermarkDoc;
}
然后调用这个方法
PDDocument wDoc = createWatermarkText();
//document.addPage(page);
Overlay overlay = new Overlay();
overlay.overlay(wDoc, document);
但这种方法行不通,我得到的是空白 pdf。非常感谢任何帮助。
这个答案试图使 OP 的原始方法起作用。
原方法的问题,
where ever my watermark message comes it is hiding my page content.
是由于 PDFBox PDPageContentStream
构造函数将新流添加为 last 内容流,因此它的操作也是最后执行的,因此会覆盖绘制的内容之前。
因此,要将新内容推送到现有内容之下,我们必须将新流移动到页面内容流中的最前面。
为了能够做到这一点,我首先更改现有代码:我将水印绘制代码附在saveGraphicsState
和restoreGraphicsState
中。这是必要的,以保护原始内容不受标记绘制代码的状态变化的影响,例如文本颜色更改。
...
PDPageContentStream contentStream = new PDPageContentStream(
document, page, true, true, true);
contentStream.saveGraphicsState();
contentStream.beginText();
// set font and font size
contentStream.setFont(pdfFont, fontSize);
// set text color to red
contentStream.setNonStrokingColor(240, 240, 240);
if (rotate) {
// rotate the text according to the page rotation
contentStream.setTextRotation(degree, x, y);
} else {
contentStream.setTextTranslation(centeredXPosition,
centeredYPosition);
}
contentStream.drawString(text);
contentStream.endText();
contentStream.restoreGraphicsState();
contentStream.close();
...
有了这个改变,我们只需要调用以下方法将水印推送到已有内容下:
void pushUnder(PDDocument document)
{
List<?> pages = document.getDocumentCatalog().getAllPages();
float fontSize = 70.0f;
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) pages.get(i);
COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS);
if (contents instanceof COSStreamArray)
{
COSStreamArray contentsArray = (COSStreamArray) contents;
COSArray newArray = new COSArray();
newArray.add(contentsArray.get(0));
newArray.add(contentsArray.get(contentsArray.getStreamCount() - 1));
for (int j = 1; j < contentsArray.getStreamCount() - 1; j++)
{
newArray.add(contentsArray.get(j));
}
COSStreamArray newStreamArray = new COSStreamArray(newArray);
page.getCOSDictionary().setItem(COSName.CONTENTS, newStreamArray);
}
}
}
(如果你仔细观察这个方法,你会发现我们没有将新流移动到第一个位置,而是只移动到第二个位置。我们这样做是因为 new PDPageContentStream(document, page, true, true, true)
构造函数实际上调用了创建两个新流,一个在第一个位置,一个在最后一个,第一个保存图形状态,最后一个恢复它然后包含您的操作。将后者移动到前者之前将导致恢复图形状态操作在开头会出错。)
我需要在 运行 时间添加水印文本,即。在创建文档时,我需要添加水印文本。我最初的方法是从文档中获取所有页面并在这些页面上添加我的文本。我确实工作了,但问题是我的水印消息出现在哪里它隐藏了我的页面内容。请查看我的初始方法的代码。
List pages = document.getDocumentCatalog().getAllPages();
float fontSize = 70.0f;
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) pages.get(i);
PDRectangle pageSize = page.findMediaBox();
float stringWidth = pdfFont.getStringWidth(text) * fontSize
/ 1000f;
// calculate to center of the page
int rotation = page.findRotation();
boolean rotate = degree > 0;
float pageWidth = rotate ? pageSize.getHeight() : pageSize
.getWidth();
float pageHeight = rotate ? pageSize.getWidth() : pageSize
.getHeight();
double centeredXPosition = rotate ? pageHeight / 2f
: (pageWidth - stringWidth) / 2f;
double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f
: pageHeight / 2f;
// append the content to the existing stream
PDPageContentStream contentStream = new PDPageContentStream(
document, page, true, true, true);
contentStream.beginText();
// set font and font size
contentStream.setFont(pdfFont, fontSize);
// set text color to red
contentStream.setNonStrokingColor(240, 240, 240);
if (rotate) {
// rotate the text according to the page rotation
contentStream.setTextRotation(degree, x, y);
} else {
contentStream.setTextTranslation(centeredXPosition,
centeredYPosition);
}
contentStream.drawString(text);
contentStream.endText();
contentStream.close();
我已经阅读了有关 Overlay 的信息并且已经尝试过,所以我尝试改变我的方法,因为我认为只有 Overlay 可以满足我的要求。我目前的做法是:
public PDDocument createWatermarkText() {
PDDocument watermarkDoc = new PDDocument();
PDPage watermarkPage = new PDPage();
try {
watermarkDoc.addPage(watermarkPage);
PDPageContentStream content = new PDPageContentStream(watermarkDoc,
watermarkPage);
content.setFont(pdfFont, fontSize);
content.beginText();
content.moveTextPositionByAmount(x, y);
content.setNonStrokingColor(255, 0, 0);
PDRectangle pageSize = watermarkPage.findMediaBox();
float stringWidth = pdfFont.getStringWidth(text) * fontSize / 1000f;
// int rotation = page.findRotation();
boolean rotate = degree > 0;
float pageWidth = rotate ? pageSize.getHeight() : pageSize
.getWidth();
float pageHeight = rotate ? pageSize.getWidth() : pageSize
.getHeight();
double centeredXPosition = rotate ? pageHeight / 2f
: (pageWidth - stringWidth) / 2f;
double centeredYPosition = rotate ? (pageWidth - stringWidth) / 2f
: pageHeight / 2f;
content.setTextRotation(degree, x, y);
content.drawString(text);
content.endText();
content.close();
// ColumnText.showTextAligned(writer.getDirectContentUnder(), align,
// new Phrase(text, pdfFont), x, y,
// degree);
} catch (IOException e) {
e.printStackTrace();
}
return watermarkDoc;
}
然后调用这个方法
PDDocument wDoc = createWatermarkText();
//document.addPage(page);
Overlay overlay = new Overlay();
overlay.overlay(wDoc, document);
但这种方法行不通,我得到的是空白 pdf。非常感谢任何帮助。
这个答案试图使 OP 的原始方法起作用。
原方法的问题,
where ever my watermark message comes it is hiding my page content.
是由于 PDFBox PDPageContentStream
构造函数将新流添加为 last 内容流,因此它的操作也是最后执行的,因此会覆盖绘制的内容之前。
因此,要将新内容推送到现有内容之下,我们必须将新流移动到页面内容流中的最前面。
为了能够做到这一点,我首先更改现有代码:我将水印绘制代码附在saveGraphicsState
和restoreGraphicsState
中。这是必要的,以保护原始内容不受标记绘制代码的状态变化的影响,例如文本颜色更改。
...
PDPageContentStream contentStream = new PDPageContentStream(
document, page, true, true, true);
contentStream.saveGraphicsState();
contentStream.beginText();
// set font and font size
contentStream.setFont(pdfFont, fontSize);
// set text color to red
contentStream.setNonStrokingColor(240, 240, 240);
if (rotate) {
// rotate the text according to the page rotation
contentStream.setTextRotation(degree, x, y);
} else {
contentStream.setTextTranslation(centeredXPosition,
centeredYPosition);
}
contentStream.drawString(text);
contentStream.endText();
contentStream.restoreGraphicsState();
contentStream.close();
...
有了这个改变,我们只需要调用以下方法将水印推送到已有内容下:
void pushUnder(PDDocument document)
{
List<?> pages = document.getDocumentCatalog().getAllPages();
float fontSize = 70.0f;
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) pages.get(i);
COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS);
if (contents instanceof COSStreamArray)
{
COSStreamArray contentsArray = (COSStreamArray) contents;
COSArray newArray = new COSArray();
newArray.add(contentsArray.get(0));
newArray.add(contentsArray.get(contentsArray.getStreamCount() - 1));
for (int j = 1; j < contentsArray.getStreamCount() - 1; j++)
{
newArray.add(contentsArray.get(j));
}
COSStreamArray newStreamArray = new COSStreamArray(newArray);
page.getCOSDictionary().setItem(COSName.CONTENTS, newStreamArray);
}
}
}
(如果你仔细观察这个方法,你会发现我们没有将新流移动到第一个位置,而是只移动到第二个位置。我们这样做是因为 new PDPageContentStream(document, page, true, true, true)
构造函数实际上调用了创建两个新流,一个在第一个位置,一个在最后一个,第一个保存图形状态,最后一个恢复它然后包含您的操作。将后者移动到前者之前将导致恢复图形状态操作在开头会出错。)