Itext PDF生成 加水印图片文字
2013-06-06 15:02
671 查看
由于项目中用到,所以网上找了很多,但或多或少有些问题,我整理更新了一下。
Itext架包下载地址:
http://download.csdn.net/detail/qm4050/5531349
PDF生成代码如下:
Itext架包下载地址:
http://download.csdn.net/detail/qm4050/5531349
PDF生成代码如下:
import java.io.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; /** * PDF生成 */ public class PDFBuild { public static void buidPDF(String pdfFile, String imageFile, String waterMarkName, int permission) { try { File file = File.createTempFile("tempFile", ".pdf"); // 创建临时文件 // 生成PDF if (createPDFFile(file)) { waterMark(file.getPath(), imageFile, pdfFile, waterMarkName, permission); // 添加水印 } } catch (Exception e) { e.printStackTrace(); } } /** * 创建PDF文件 * * @param file * 临时文件 * @return 成功/失败 */ public static boolean createPDFFile(File file) { Rectangle rect = new Rectangle(PageSize.A4); Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F); try { PdfWriter.getInstance(doc, new FileOutputStream(file)); doc.open(); BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体 Font font = new Font(bf, 14.0F, 0); font.setStyle(37); // 设置样式 font.setFamily("宋体"); // 设置字体 Paragraph p = new Paragraph("付 款 通 知 书\r\n", font); p.setAlignment(1); doc.add(p); doc.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public static void waterMark(String inputFile, String imageFile, String outputFile, String waterMarkName, int permission) { try { PdfReader reader = new PdfReader(inputFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream( outputFile)); BaseFont base = BaseFont.createFont( "C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体 int total = reader.getNumberOfPages() + 1; Image image = Image.getInstance(imageFile); // 图片位置 image.setAbsolutePosition(400, 480); PdfContentByte under; int j = waterMarkName.length(); char c = 0; int rise = 0; for (int i = 1; i < total; i++) { rise = 400; under = stamper.getUnderContent(i); under.beginText(); 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 + ""); } } else { under.setTextMatrix(240, 100); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 18; } } // 添加水印文字 under.endText(); // 添加水印图片 under.addImage(image); // 画个圈 under.ellipse(250, 450, 350, 550); under.setLineWidth(1f); under.stroke(); } stamper.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String imageFilePath = "D:/itext2.png"; // 水印图片路径 String pdfFilePath = "D:/itext.pdf"; // 文件生成路径 buidPDF(pdfFilePath, imageFilePath, "正版授权", 16); } }
相关文章推荐
- Itext PDF生成 加水印图片文字
- pdf加水印,文字和图片,超链接, java itext, 这里有你想要的一切
- itext 5.3.0实现对pdf文件添加(文字和图片)水印
- iText7 生成 PDF 文件,解决各种操作系统中文问题,控制指定位置显示图片、文字、图文混排
- java 通过 itext生成pdf添加,插入文字,图片
- c# pdf生成图片文字水印类
- asp.net 上传图片,生成缩略图,生成图片水印,文字水印
- itext应用HTML模版生成PDF并加水印,处理了中文问题
- Java在已存在的pdf文件中生成文字和图片--基础
- [导入]使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- php生成 文字水印 图片水印
- ASP.NET 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字水印和图片的水印图
- Itext生成PDF文件加密与加水印
- 使用FileUpload控件上传图片并自动生成缩略图、带文字和图片的水印图
- ASP.NET 上传图片 生成文字 图片水印 (单图片水印 or 全覆盖图片水印)
- 用AspJpeg调整文字水印透明,生成图片水印的效果
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- asp.net 上传图片,生成缩略图,生成图片水印,文字水印
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图