67,513
社区成员
发帖
与我相关
我的任务
分享
//*************************给PDF添加水印******************************************************************************************************************
String zkzPdf1=user_name+"-"+UUID.randomUUID().toString()+".pdf";
String outputFile1 = zkzpath+zkzPdf1;
try {
PdfReader reader = new PdfReader(outputFile, "PDF".getBytes());//获取要加水印的原pdf文件路径
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outputFile1)); //加了水印后要输出的路径
String markImagePath =ImageSYPaht + "view/front/20141210/images/shuiyin_1.png";
Image shuiyinImg = Image.getInstance(markImagePath);//水印图片路径
shuiyinImg.setAbsolutePosition(110, 200);
int pageSize = reader.getNumberOfPages();
for(int i = 1; i <= pageSize; i++) {
PdfContentByte under = stamp.getUnderContent(i);
under.addImage(shuiyinImg);
}
stamp.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
}// 关闭
File tempfile = new File(outputFile);
if(tempfile.exists()) {
tempfile.delete();
}
//**************************添加水印完成******************************************************************************************************************
int i= webImpl.saveZkzPDF(decID, zkzPdf1);
if(i>0){
json.put("success", true);
json.put("zkzPdf", zkzPdf1);
}else{
json.put("success", false);
json.put("msg", "生成模板失败,请重新生成!");
}
response.getWriter().write(json.toString());
response.getWriter().close();
private static void insertWatermarkText(Document doc, String watermarkText) throws Exception
{
// Create a watermark shape. This will be a WordArt shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
// Insert the watermark into all headers of each document section.
for (Section sect : doc.getSections())
{
// There could be up to three different headers in each section, since we want
// the watermark to appear on all pages, insert into all headers.
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
// insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
// insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}
private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception
{
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
// Insert a clone of the watermark into the header.
header.appendChild(watermarkPara.deepClone(true));
}