Code的功能非常简单,需要您使用Itext在现有PDF上标记内容。 Input是InputStream
,并为Servlet响应返回ByteArrayOutputStream
。
不知何故,它返回整个PDF而不标记任何内容并且不返回错误/异常。代码曾经工作当这个函数是servlet的一部分时,但它确实不工作,因为它被转移到一个新类(现在在构造函数中初始化了Watermark属性) )。
public ByteArrayOutputStream watermarkPDFDocument (InputStream WatermarkedFileInputStream) throws DocumentException {
ByteArrayOutputStream bytearrayos =new ByteArrayOutputStream();
try
{
PdfReader reader = new PdfReader(WatermarkedFileInputStream);
PdfStamper stamper = new PdfStamper(reader, bytearrayos);
PdfContentByte PdfContentOver; //PDFContent to get page data and to splash over pages.
Rectangle pagesize;
float WatermarkCenterX, WatermarkCenterY;
// loop over every page
int TotalNoOfPages = reader.getNumberOfPages();
for (int i = 1; i <= TotalNoOfPages; i++) {
// get page size and position
pagesize = reader.getPageSizeWithRotation(i);
WatermarkCenterX = (pagesize.getLeft() + pagesize.getRight()) / 2;
WatermarkCenterY = (pagesize.getTop() + pagesize.getBottom()) / 2;
LOG.debug("Center of page - [" + WatermarkCenterX + " , " + WatermarkCenterY+"]");
//Initializing PageNo Phrase for Footer
FooterPageNoPhrase = new Phrase("Page " + String.valueOf(i) + " of " + String.valueOf(TotalNoOfPages),FooterFont);
PdfContentOver = stamper.getOverContent(i);
PdfContentOver.saveState();
//Setting Watermark opacity
PdfGState CenterState = new PdfGState();
PdfGState FooterState = new PdfGState();
CenterState.setFillOpacity(CenterFontOpacity);
FooterState.setFillOpacity(FooterFontOpacity);
PdfContentOver.setGState(CenterState);//Sets Transparency for Center
//Watermark at center
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_CENTER, CenterPhraseDocState, WatermarkCenterX-(CenterFontSize/2), WatermarkCenterY+(CenterFontSize/2), 45);
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_CENTER, CenterPhraseDocLogic, WatermarkCenterX+(CenterFontSize/2), WatermarkCenterY-(CenterFontSize/2), 45);
LOG.debug("Stamped Center Watermark - " + CenterPhraseDocState + " , " + CenterPhraseDocLogic);
PdfContentOver.setGState(FooterState); //Sets Transparency for Footer
//Watermark at Footer Row 1
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_LEFT, FooterPhraseDocState, (pagesize.getLeft()+ FooterDistanceFromLeft), (pagesize.getBottom() + FooterRow1Height), 0);
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_RIGHT, FooterPhraseDocNumber, (pagesize.getRight()- FooterDistanceFromRight), (pagesize.getBottom() + FooterRow1Height), 0);
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_CENTER, FooterPhraseDocName, WatermarkCenterX, (pagesize.getBottom() + FooterRow1Height), 0);
//Watermark at Footer Row 2
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_CENTER, FooterPhraseConfidential, WatermarkCenterX, (pagesize.getBottom() + FooterRow2Height), 0);
ColumnText.showTextAligned(PdfContentOver, Element.ALIGN_RIGHT, FooterPageNoPhrase, (pagesize.getRight()- FooterDistanceFromRight), (pagesize.getBottom() + FooterRow2Height), 0);
LOG.debug("Stamped Footer Watermark - " + FooterPhraseDocState + " , " + FooterPhraseDocNumber + " , " + FooterPhraseDocName + " , " + FooterPhraseConfidential);
PdfContentOver.restoreState();
}
stamper.close();
reader.close();
}
catch(IOException e)
{
LOG.error("ERROR while retrieving primary content's file ", e);
}
return bytearrayos;
}
当我尝试运行代码时,这是记录器 - Logger Image Link
由于没有捕获Exceptions
并且Servlet响应返回从InputStream
生成的PDF,并且Itext Phrases
返回非空值。我怀疑错误在PDFStates
附近或将短语写在文本上,但在盯着它几个小时后,我无法弄清楚出错的地方。
编辑 - 我将无法附加日志文件,因为它包含来自系统其他部分的详细信息。我正在编写代码中的所有日志调试/错误。
2018-06-14 05:28:45,909 INFO [RMI TCP Connection(150)-132.189.104.47] wt.log4j.jmx.LoggerRepositoryMonitor.levelChanged - Logger=com.bsci.plm.watermark.BSCWatermarkFileGeneration, NewLevel=ALL
796: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Font Size for Center Watermark - 30
797: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Font Size for Footer Watermark - 10
798: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - RGB for Center Watermark - [255, 0, 0]
799: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - RGB for Footer Watermark - [0, 0, 0]
800: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Opacity for Center Watermark - 0.4
801: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Opacity for Footer Watermark - 0.9
802: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Confidential Watermark - Confidential. Unauthorized use is prohibited.
803: 2018-06-14 05:28:56,702 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Watermark BusinessLogic -
804: 2018-06-14 05:28:56,730 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Center of page - [306.0 , 396.0]
805: 2018-06-14 05:28:56,730 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Stamped Center Watermark - [Released (Expiring)] , []
806: 2018-06-14 05:28:56,730 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Stamped Footer Watermark - [Released (Expiring)] , [92001458 B.3] , [Global TMP 2] , [Confidential. Unauthorized use is prohibited.]
807: 2018-06-14 05:28:56,731 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Center of page - [306.0 , 396.0]
808: 2018-06-14 05:28:56,731 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Stamped Center Watermark - [Released (Expiring)] , []
809: 2018-06-14 05:28:56,731 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Stamped Footer Watermark - [Released (Expiring)] , [92001458 B.3] , [Global TMP 2] , [Confidential. Unauthorized use is prohibited.]
810: 2018-06-14 05:28:56,731 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Center of page - [306.0 , 396.0]
811: 2018-06-14 05:28:56,732 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Stamped Center Watermark - [Released (Expiring)] , []
812: 2018-06-14 05:28:56,732 DEBUG [ajp-bio-8011-exec-2] com.bsci.plm.watermark.BSCWatermarkFileGeneration wcadmin - Stamped Footer Watermark - [Released (Expiring)] , [92001458 B.3] , [Global TMP 2] , [Confidential. Unauthorized use is prohibited.]
EDIT2 - 添加在servlet中调用方法的方式。 'IdentifierInfoDocument'和'SourceFileInputStream'正常工作,因为Logger最终得到了正确的水印消息,如果我的InputStream没有生成,我将不会得到响应PDF。 PDF是在InputStream的Response中生成的,并且由于所有Logger都被调用,我仍然认为像短语一样的问题会被标记在PDF上。
ByteArrayOutputStream bytearrayos = null; //
try {
BSCWatermarkFileGeneration WatermarkedFile =new BSCWatermarkFileGeneration(IdentifierInfoDocument(IdentifierInfo));
bytearrayos = WatermarkedFile.watermarkPDFDocument(SourceFileInputStream(SourceFileServlet));
}
catch (DocumentException e) {
LOG.error("PDF Document not found - ", e);
e.printStackTrace();
}
catch (WTException e) {
LOG.error("Error while retrieving WTDOC - ", e);
e.printStackTrace();
}
catch (NullPointerException e) {
LOG.error("Error while accessing watermark properties from property file - ", e);
e.printStackTrace();
}
catch (IOException e) {
LOG.error("Error while accessing the source Servlet ",e);
e.printStackTrace();
}
catch (NumberFormatException e) {
LOG.error("Error while accessing integer/float values from property file - ", e);
e.printStackTrace();
}
ServletOutputStream os = response.getOutputStream();
if(bytearrayos!=null)
bytearrayos.writeTo(os);
os.flush();
最终编辑 - 我弄清楚出了什么问题而且真的很蠢。不透明度初始化为局部变量,而不是我在类中定义的变量。因为我在Logger中使用它们,所以我从未对未使用的变量发出任何警告,也无法追踪原因。