分享

itext pdf添加文字水印 打印时水印遮住下面实体数据

 liuguichuan 2014-03-20
for (int i = 1; i <= total; i++) // 每一页都加水印
            {
                pageWidth = reader.getPageSize(i).getWidth();
                pageHeight = reader.getPageSize(i).getHeight();
  
                under = stamp.getOverContent(i);
  
                // 加入文字
                // gs.setFillOpacity(10f);//设置透明度为0.8
                under.setGState(gs);
                under.setColorFill(BaseColor.RED);
                under.setFontAndSize(bfChinese, fontSize);
  
                under.beginText();
                // 用来设置文字的位置和字体---开始
                // 横向居中
                under.setTextMatrix(
                        (pageWidth - fontSize * stampStr.length()) / 2,
                        pageHeight - margin_top);
                // 用来设置文字的位置和字体---结束
                //换行输出水印
                for (int j = 0; j < lines_count; j++) {
                    String temp_line=stampArray.get(j);
                    if(temp_line!=null && temp_line.length()>0){
//                      under.showTextAligned(Element.ALIGN_CENTER, temp_line,
//                              pageWidth / 2, pageHeight / 2, Integer
//                                      .parseInt(StaticValue.Font_Rotation));
                                              //关键代码在这里---开始
                                               //先写入文字
                        under.showTextAligned(Element.ALIGN_CENTER, temp_line,
                                pageWidth / 2, pageHeight / 2-j*(fontSize+StaticValue.Stamp_Line_Length), Integer
                                        .parseInt(StaticValue.Font_Rotation));
                                          //关键代码在这里---结束
                                  
                    }
                }
                under.endText();
                under.saveState();
            }
----------------------------------------------------------------------循环加载图片
for (int i = 1; i <= total; i++) // 每一页都加水印
            {
                pageWidth = reader.getPageSize(i).getWidth();
                pageHeight = reader.getPageSize(i).getHeight();
  
                under = stamp.getOverContent(i);
                           for (int j = 0; j < lines_count; j++) {
                    String temp_line=stampArray.get(j);
                    if(temp_line!=null && temp_line.length()>0){
//                      under.showTextAligned(Element.ALIGN_CENTER, temp_line,
//                              pageWidth / 2, pageHeight / 2, Integer
//                                      .parseInt(StaticValue.Font_Rotation));
                                              //关键代码在这里---开始
                                //逐页写入图片
                                  under.setGState(gs);
                image.setAlignment(Image.UNDERLYING);//图片置底,而图片本身为透明图片
                image.setAbsolutePosition(0, 0);
                image.scaleAbsolute(pageWidth, pageHeight);//这里要注意,遮住整个page,即作为背景存在
                under.addImage(image);
                under.saveState();
                                              //关键代码在这里---结束
                                  
                    }
                }
                under.endText();
                under.saveState();
            }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约