import com.itextpdf.text.BaseColor; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.barcode.PdfCodabarBarcode; import com.spire.pdf.barcode.TextLocation; import com.spire.pdf.graphics.PdfRGBColor; import javax.swing.*; import java.awt.*; import java.awt.geom.Point2D; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; /** * pdf文件添加水印 * * @author hech <br> * 参考文章:https://blog.csdn.net/sjzs5590/article/details/7735149 * */ public class PdfWaterMarkFi { // 水印文字 private static String WATER_MARK_TEXT = "连络"; // 源pdf文件 private static String SRC_PDF_PATH = "/Users/liubin01/Desktop/组合 1.pdf"; // 输出pdf文件 private static String OUT_PDF_PATH = "/Users/liubin01/Desktop/会议室技术文档.shuiy.pdf"; private static String INAGE_PATH = "/Users/liubin01/Documents/GitHub/flow/barcode.png"; public static void main(String[] args) { PdfWaterMarkFi pdfWaterMark = new PdfWaterMarkFi(); // 在pdf文件中添加水印 pdfWaterMark.waterMark(SRC_PDF_PATH, OUT_PDF_PATH, WATER_MARK_TEXT); //加载PDF文档 PdfDocument pdf = new PdfDocument(); pdf.loadFromFile(SRC_PDF_PATH); //添加一个空白页,目的为了删除jar包添加的水印,后面再移除这一页 pdf.getPages().add(); //遍历文档中的页 for (int i = 0; i < pdf.getPages().getCount(); i++) { addBarcode(pdf.getPages().get(i)); } //移除第一个页 pdf.getPages().remove(pdf.getPages().get(pdf.getPages().getCount()-1)); //保存为另外一个文档 pdf.saveToFile(OUT_PDF_PATH); System.out.println("添加完成!"); } /** * 在pdf文件中添加水印 * * @param srcPdfPath * 原始文件 * @param outPdfPath * 水印输出文件 * @param waterMarkText * 水印文本 */ public void waterMark(String srcPdfPath, String outPdfPath, String waterMarkText) { try { PdfReader reader = new PdfReader(srcPdfPath); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outPdfPath)); BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); int total = reader.getNumberOfPages() + 1; Rectangle pageRect = null; PdfGState gs = new PdfGState(); // 这里是透明度设置 gs.setFillOpacity(Float.parseFloat("0.15")); // 这里是条纹不透明度 gs.setStrokeOpacity(0.2f); JLabel label = new JLabel(); FontMetrics metrics; int textH = 0; int textW = 0; int heightdensity = 6; int widthdensity = 5; int angle = 19; label.setText(waterMarkText); metrics = label.getFontMetrics(label.getFont()); textH = metrics.getHeight(); // 字符串的高, 只和字体有关 textW = metrics.stringWidth(label.getText()); // 字符串的宽 PdfContentByte under; // 这个循环是确保每一张PDF都加上水印 for (int i = 1; i < total; i++) { pageRect = reader.getPageSizeWithRotation(i); under = stamper.getOverContent(i); // 在内容上方添加水印 // under = stamper.getUnderContent(i); //在内容下方添加水印 under.saveState(); under.setGState(gs); under.beginText(); under.setColorFill(BaseColor.RED); // 添加文字颜色 不能动态改变 放弃使用 under.setFontAndSize(base, 15); // 这里是水印字体大小 for (int height = textH; height < pageRect.getHeight() * 2; height = height + textH * heightdensity) { for (int width = textW; width < pageRect.getWidth() * 1.5 + textW; width = width + textW * widthdensity) { // rotation:倾斜角度 under.showTextAligned(Element.ALIGN_LEFT, waterMarkText, width - textW, height - textH, angle); } } // 添加水印文字 under.endText(); } stamper.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 条形码 */ static void addBarcode(PdfPageBase page){ //初始化y变量 double y = 15; //绘制Codebar条码到PDF PdfCodabarBarcode codebar= new PdfCodabarBarcode("00:12-3456/7890"); codebar.setBarcodeToTextGapHeight(1f); codebar.setBarHeight(50f); codebar.setEnableCheckDigit(true); codebar.setShowCheckDigit(true); codebar.setTextDisplayLocation(TextLocation.Bottom); PdfRGBColor blue = new PdfRGBColor(Color.blue); codebar.setTextColor(blue); Point2D.Float point = new Point2D.Float(); point.setLocation(200,y); codebar.draw(page,point); y = codebar.getBounds().getY()+ codebar.getBounds().getHeight() + 5; } }
spire打印pdf,10页限制
最新推荐文章于 2024-05-29 15:44:47 发布