使用iText给pdf文件添加水印不能正常显示 20
我在使用iText给pdf文件添加水印时,文字无法显示,但是添加水印的那块儿区域已经出现了,如下图:(蓝色区域是水印的选中效果)。代码如下:PdfReaderreader...
我在使用iText给pdf文件添加水印时,文字无法显示,但是添加水印的那块儿区域已经出现了,如下图:
(蓝色区域是水印的选中效果)。
代码如下:
PdfReader reader = new PdfReader(pdfPath); int n = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(newPdfPath));
int i = 0;PdfContentByte under;PdfGState gs = new PdfGState();
String textContent = "档案馆馆藏";String angle = "45"; String diaphaneity = "80";
// 转换透明度(默认为50%)float diaphaneityF = 0.5f;
if (!"".equals(diaphaneity) && !"null".equals(diaphaneity)) {
diaphaneityF = (100f - Float.parseFloat(diaphaneity)) / 100;
}
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
while (i < n) {
i++; under = stamp.getUnderContent(i);
Rectangle pageRect = stamp.getReader().getPageSizeWithRotation(i); gs.setFillOpacity(diaphaneityF); gs.setStrokeOpacity(diaphaneityF); under.setGState(gs);under.beginText(); String align = "center"; float x = pageRect.getWidth() / 2;float y = pageRect.getHeight() / 2; under.setFontAndSize(bf, 50); under.setRGBColorFill(40, 125, 231);
under.showTextAligned(Element.ALIGN_CENTER, textContent, x, y, Integer.parseInt(angle));
under.endText();
}
stamp.close(); 展开
(蓝色区域是水印的选中效果)。
代码如下:
PdfReader reader = new PdfReader(pdfPath); int n = reader.getNumberOfPages();
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(newPdfPath));
int i = 0;PdfContentByte under;PdfGState gs = new PdfGState();
String textContent = "档案馆馆藏";String angle = "45"; String diaphaneity = "80";
// 转换透明度(默认为50%)float diaphaneityF = 0.5f;
if (!"".equals(diaphaneity) && !"null".equals(diaphaneity)) {
diaphaneityF = (100f - Float.parseFloat(diaphaneity)) / 100;
}
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
while (i < n) {
i++; under = stamp.getUnderContent(i);
Rectangle pageRect = stamp.getReader().getPageSizeWithRotation(i); gs.setFillOpacity(diaphaneityF); gs.setStrokeOpacity(diaphaneityF); under.setGState(gs);under.beginText(); String align = "center"; float x = pageRect.getWidth() / 2;float y = pageRect.getHeight() / 2; under.setFontAndSize(bf, 50); under.setRGBColorFill(40, 125, 231);
under.showTextAligned(Element.ALIGN_CENTER, textContent, x, y, Integer.parseInt(angle));
under.endText();
}
stamp.close(); 展开
3个回答
展开全部
首先需要的jar包为iText-2.1.2u.jar、iTextAsian.jar。
import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class TestWaterPrint {
public static void main(String[]args) throws DocumentException, IOException{
//要输出的pdf文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/abc.pdf")));
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//将pdf文件先加水印然后输出
setWatermark(bos,"E:/pdf源文件.pdf",format.format(cal.getTime()) + " 下载使用人:" + "测试user", 16);
}
public static void setWatermark(BufferedOutputStream bos, String input,
String waterMarkName, int permission)
throws DocumentException, IOException {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) {
content = stamper.getOverContent(i);//在内容上方加水印
//content = stamper.getUnderContent(i);//在内容下方加水印
gs.setFillOpacity(0.2f);
// content.setGState(gs);
content.beginText();
content.setColorFill(Color.LIGHT_GRAY);
content.setFontAndSize(base, 50);
content.setTextMatrix(70, 200);
content.showTextAligned(Element.ALIGN_CENTER, "公司内部文件,请注意保密!", 300,
350, 55);
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 8);
content.showTextAligned(Element.ALIGN_CENTER, "下载时间:"
+ waterMarkName + "", 300, 10, 0);
content.endText();
}
stamper.close();
}
}
import java.awt.Color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class TestWaterPrint {
public static void main(String[]args) throws DocumentException, IOException{
//要输出的pdf文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/abc.pdf")));
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//将pdf文件先加水印然后输出
setWatermark(bos,"E:/pdf源文件.pdf",format.format(cal.getTime()) + " 下载使用人:" + "测试user", 16);
}
public static void setWatermark(BufferedOutputStream bos, String input,
String waterMarkName, int permission)
throws DocumentException, IOException {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, bos);
int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) {
content = stamper.getOverContent(i);//在内容上方加水印
//content = stamper.getUnderContent(i);//在内容下方加水印
gs.setFillOpacity(0.2f);
// content.setGState(gs);
content.beginText();
content.setColorFill(Color.LIGHT_GRAY);
content.setFontAndSize(base, 50);
content.setTextMatrix(70, 200);
content.showTextAligned(Element.ALIGN_CENTER, "公司内部文件,请注意保密!", 300,
350, 55);
content.setColorFill(Color.BLACK);
content.setFontAndSize(base, 8);
content.showTextAligned(Element.ALIGN_CENTER, "下载时间:"
+ waterMarkName + "", 300, 10, 0);
content.endText();
}
stamper.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2019-01-24
展开全部
给pdf文件添加水印的方法其实并不难,我们先用工具{迅 捷PDF编辑器}打开需要删除页面的文件,然后点击界面上方的文档按钮,再选择其中的水印按钮,最后点击水印中的添加。
点击了添加按钮后,我们可以看到主界面上弹出了一个添加水印菜单窗口。在添加水印菜单中,有两种水印类型可供设置,一种是文本水印,一种是文件水印。
点击了添加按钮后,我们可以看到主界面上弹出了一个添加水印菜单窗口。在添加水印菜单中,有两种水印类型可供设置,一种是文本水印,一种是文件水印。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
肯定不行啊。。。保密文件是不能随意印发的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询