spire打印pdf,10页限制

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;
   }

}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Free Spire.PDF for .NET 是 Spire.PDF for .NET 的免费版本,无需购买即可用于个人或商业用途。使用该组件,程序员可以 在.NET 程序中创建、读取、写入、编辑和操作 PDF 文档。这个控件能支持的功能十分全面,例如文档安全性设置(电子签名),提取 PDF 文本、附件、图片,PDF 合并和拆分,更新 Metadata,设置 Section,绘制图形、插入图片、表格制作和加工、导入数据等等。除此以外,Spire.PDF 还可以将 TXT 文本、图片、HTML 高质量地转换为 PDF 文件格式。 主要功能如下: 1.高质量的文档转换。Free Spire.PDF for .NET 支持 PDF 到 Word、XPS、SVG、EMF、Text 和图片(EMF、JPG、PNG、BMP、TIFF)的格式转换。也支持从 XML、HTML、RTF、XPS、Text、图片等格式生成 PDF 文档。 2.文档操作及域功能。支持合并、拆分 PDF 文档,在原有的 PDF 文档添加覆盖。同时,Spire.PDF 提供导入、邮戳、小册子功能,以及帮助用户从数据库读取数据并填充到域的域填写功能。 3. 安全性设置。用户可以通过设置密码和数字签名来保护 PDF 文档。用户密码和所有者密码可以确定加密的 PDF 文档的可读性、可修改性、是否可打印等有选择性的限制。与此同时,数字签名作为一个更有效的方法,可以应用于维护和对PDF文档进行身份验证。 4.数据提取。支持快速高效地从 PDF 文档提取图片、文本、PDF,以及附件。 5.文件属性设置。支持对 Metadata、文件属性、面方向、面大小进行设置。其中文件属性包括文件限制打印面提取、加评论等方面的权限限制)以及文件描述属性(文件名称、作者、主题、关键字等)。使用 Spire.PDF for .NET,用户还可以根据自己阅读喜好设定默认打开码,分模式,缩放比例和打印缩放,等等。 6.其他功能。 支持多种语言,支持字体格式、对齐方式设置。 绘制文字,图片,图形。 支持添加图层,透明图像,Color Space,条形码到 PDF。 支持 PDF/A-1b、PDF/x1a:2001 格式。 添加梯状图形和矢量图像到指定位置。 添加并格式化表格。 插入交互元素,例如添加自定义的 Annotation、Action、JavaScript、附件、书签等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值