package test1; import java.awt.Color; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; 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; import com.lowagie.text.pdf.PdfWriter; public class PdfConvertor { // txt原始文件的路径 private static final String txtFilePath = "c:/test/01.txt"; // 生成的pdf文件路径 private static final String pdfFilePath = "c:/test/01.pdf"; // 添加水印图片路径 private static final String imageFilePath = "c:/test/images/psue.jpg"; // 生成临时文件前缀 private static final String prefix = "tempFile"; // 所有者密码 private static final String OWNERPASSWORD = "123456"; public static void generatePDFWithTxt(String txtFile, String pdfFile, String userPassWord, String waterMarkName, int permission) { try { // 生成临时文件 File file = File.createTempFile(prefix, ".pdf"); // 创建pdf文件到临时文件 if (createPDFFile(txtFile, file)) { // 增加水印和加密 waterMark(file.getPath(), pdfFile, userPassWord, OWNERPASSWORD, waterMarkName, permission); } } catch (Exception e) { e.printStackTrace(); } } private static boolean createPDFFile(String txtFilePath, File file) { // 设置纸张 Rectangle rect = new Rectangle(PageSize.A4); // 设置页码 HeaderFooter footer = new HeaderFooter(new Phrase("页码:", PdfConvertor .setChineseFont()), true); footer.setBorder(Rectangle.NO_BORDER); // step1 Document doc = new Document(rect, 50, 50, 50, 50); doc.setFooter(footer); try { FileReader fileRead = new FileReader(txtFilePath); BufferedReader read = new BufferedReader(fileRead); // 设置pdf文件生成路径 step2 PdfWriter.getInstance(doc, new FileOutputStream(file)); // 打开pdf文件 step3 doc.open(); // 实例化Paragraph 获取写入pdf文件的内容,调用支持中文的方法. step4 while (read.ready()) { // 添加内容到pdf(这里将会按照txt文件的原始样式输出) doc.add(new Paragraph(read.readLine(), PdfConvertor .setChineseFont())); } // 关闭pdf文件 step5 doc.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } private static void waterMark(String inputFile, String outputFile, String userPassWord, String ownerPassWord, String waterMarkName, int permission) { try { PdfReader reader = new PdfReader(inputFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( outputFile)); // 设置密码 //stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(), permission, false); BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); int total = reader.getNumberOfPages() + 1; Image image = Image.getInstance(imageFilePath); image.setAbsolutePosition(50, 400);//坐标 image.setRotation(-20);//旋转 弧度 image.setRotationDegrees(-45);//旋转 角度 // image.scaleAbsolute(200,100);//自定义大小 image.scalePercent(50);//依照比例缩放 PdfContentByte under; int j = waterMarkName.length(); char c = 0; int rise = 0; for (int i = 1; i < total; i++) { rise = 500; under = stamper.getUnderContent(i); // 添加图片 under.addImage(image); PdfGState gs = new PdfGState(); gs.setFillOpacity(0.2f);// 设置透明度为0.2 under.setGState(gs); under.beginText(); under.setColorFill(Color.CYAN); under.setFontAndSize(base, 30); // 设置水印文字字体倾斜 开始 if (j >= 15) { under.setTextMatrix(200, 120); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 20; } } else { under.setTextMatrix(180, 100); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 18; } } // 字体设置结束 under.endText(); // 画一个圆 // under.ellipse(250, 450, 350, 550); // under.setLineWidth(1f); // under.stroke(); } stamper.close(); } catch (Exception e) { e.printStackTrace(); } } private static Font setChineseFont() { BaseFont base = null; Font fontChinese = null; try { base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); fontChinese = new Font(base, 12, Font.NORMAL); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fontChinese; } public static void main(String[] args) { generatePDFWithTxt(txtFilePath, pdfFilePath, "123", "", 16); } }
相关推荐
itext5生成PDF(含水印文字及图片)的源代码,操作说明详细,代码完整可用。
使用itext开发包对pdf文件进行去水印操作,包括删除图片水印和文字水印。通过遍历pdf的contents对象和xobjects对象,进行对象删除,速度快,效率高
一个使用itext给PDF加上水印的
java使用itextpdf、itext-asian对pdf文件加中文文字水印,此demo只提供参考,希望给需要的朋友带来帮助,谢谢支持!
pdf添加水印的jar包,支持中文水印 pdf添加水印的jar包,支持中文水印
itext 增加水印 C#代码如何程序
IText5 Excel转pdf ,带有添加文字水印,图片水印实例。 文件中含有依赖的jar,代码,下载后你直接可以用了,花了2天时间整理完成。 共3个方法,一个是生成pdf,第2个是加文字水印,第3个是加图片水印 public static...
itext 对JAVA的操作 纯JAVA 操作标准的 DOC文件
利用itext读取PDF后使用pdfbox将PDF转为图片。压缩包内附有相应JAR文件
这是一个itext生成pdf水印的例子。其中需要几个文件。
itext生成PDF图片文档
itext-pdf添加pdf水印的jar包,实例代码,支持中文水印
利用iText五步创建一个PDF文件:helloword。 第一步,创建一个 iTextSharp.text.Document对象的实例: Document document = new Document(); 第二步,为该Document创建一个Writer实例: PdfWriter.getInstance...
IText5 Excel转pdf ,带有添加文字水印,图片水印实例。 * jar版本:poi-3.9-20121203.jar, poi-ooxml-3.9-20121203.jar, * poi-ooxml-schemas-3.9-20121203.jar, itextpdf-5.5.9.jar, itext-asian-5.2.0.jar ...
在已有pdf文件中,指定页及绝对位置添加图片(包含itextpdf.jar及实例)
itext将图片生成PDF(高清版),可按比例缩放,三种可选的缩放方式,导出高清版的PDF,直接可导入使用的demo文件,内含jar包,无坑版!
itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框.itext对pdf进行编辑.使用Adobe对pdf创建表单,itext对表单填充,下拉选,单选,文本框.复选框
itext 导出word itext 导出word
不好意思,之前说的iText包含源码,但是没有,自己搞错了。这个是真的包含源码。
iText pdf文档生成,内含itext5.5.11.zip所有包,itxt-asian5.20.jar,加水印去水印代码等