天天看点

java: itext 7.2.1 using jdk 17.0.1

/**

* iText 7.2.1

* bouncycastle 1.7.0

* slf4j-api-1.6.1

* slf4j-log4j12-1.6.1

* log4j-1.2.16

*

* */

/**
	 * 
	 * 
	 * 
	 * */
   public static PdfFont getDefaultFont() {
       try {
//         return PdfFontFactory.createFont("C://windows//fonts//simsun.ttc,1", PdfEncodings.IDENTITY_H, false);// 引用系统字体
           return PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");// 使用itext字体
       } catch (IOException e) {
           // 记录日志
           e.printStackTrace();
       }
       return null;
   }
    
	/**
	 * 
	 * 
	 * 
	 * */
   public static PdfFont getImportFont(String fontName) {
       // 获取resource下文件夹路径
       String path = "src/resource/";
       String fontPath = path + fontName;
       try {
           // 处理中文乱码(支持Linux系统)
           //FontProgram fontProgram = FontProgramFactory.createFont(fontPath, false);
           PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H);
           return font;
       } catch (IOException e) {
           // 记录日志
           e.printStackTrace();
       }
       return null;
   }

    
   PdfFont hfFont;
   {
       try {
           hfFont =PdfFontFactory.createFont(path);// new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.NORMAL);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
 
   /**
    * 图片水印
    *
    * */
   public static void addImageToPDFDU(String PDFPath){      
	   
       try{     
            

           // PdfWriter writer = new PdfWriter(new File(PDFPath));            //new FileOutputStream();
            // writer = new PdfWriter(new File(PDFPath));
           // PdfDocument pdfDoc = new PdfDocument(writer);
 
 
 
           // PdfWriter pdfwriter = PdfWriter(new File(PDFPath);
            PdfDocument pdf = new PdfDocument(new PdfWriter(PDFPath));
            PageSize pageSize = new PageSize(PageSize.A4).rotate();

            //设置文档属性
            pdf.getDocumentInfo().setAuthor("涂聚文 Geovin Du");
            pdf.getDocumentInfo().setTitle("iText7 java测试PDF");
            pdf.getDocumentInfo().setSubject("涂聚文公司");
            pdf.getDocumentInfo().setMoreInfo("Geovin Du","1201");
            pdf.getDocumentInfo().setCreator("geovindu 涂聚文");
            pdf.getDocumentInfo().setKeywords("iText,涂聚文 塗聚文 Geovin Du");
            
            
            Document document = new Document(pdf,PageSize.A4,true);

            TextFooterEventHandler eventHandler = new TextFooterEventHandler(document);
            pdf.addEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
            for (int i = 0; i < 12; i++) {
            	document.add(new Paragraph("Test " + i + " 涂聚文 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.").setFont(getDefaultFont()));
            }

            // the currently last page is also the last page of the document, so inform the event listener 
            eventHandler.lastPage = pdf.getLastPage();
            
            
            PdfPage page1 = pdf.addNewPage();
            PdfCanvas canvas = new PdfCanvas(page1);
 
 
            // creating image data instance by passing the path to image
            ImageData img = ImageDataFactory.create("src/resource/gogo.png");
            canvas.saveState();
            // graphic state
            PdfExtGState state = new PdfExtGState();
            state.setFillOpacity(0.2f);
            canvas.setExtGState(state);
            canvas.addImageAt(img, 20, pageSize.getWidth()/2, false); //650,
            canvas.restoreState();
            document.add(new Paragraph("Adding image to PDF Example,涂聚文").setFont(getDefaultFont()));
            
            pdf.addNewPage();
           
            PdfFont headerFont = PdfFontFactory.createFont(path);
            PdfFont cellFont = PdfFontFactory.createFont(path);
 
     
           Table table = new Table(new float[]{4, 4, 4});
           table.setWidth(UnitValue.createPercentValue(100));
           // adding header
           table.addHeaderCell(new Cell().add(new Paragraph(
                   "姓").setFont(headerFont)));
           table.addHeaderCell(new Cell().add(new Paragraph(
                   "名").setFont(headerFont)));
           table.addHeaderCell(new Cell().add(new Paragraph(
                   "区域名称").setFont(headerFont)));          
            
            
    
           table.addCell(new Paragraph("1.0 geovindu").setFont(cellFont));
           table.addCell(new Paragraph("1.1 涂聚文").setFont(cellFont));
           table.addCell(new Paragraph("1.2 上海市").setFont(cellFont));
           
           table.addCell(new Paragraph("2.1 北京市").setFont(cellFont));
           table.addCell(new Paragraph("2.2 重庆市").setFont(cellFont));
           table.addCell(new Paragraph("2.3 宜春市").setFont(cellFont));
            
           document.add(table);
            
            
            
           document.close();
            
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
      

  

java: itext 7.2.1 using jdk 17.0.1
java: itext 7.2.1 using jdk 17.0.1
java: itext 7.2.1 using jdk 17.0.1
import java.io.IOException;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
/**
*geovindu 
*Gevoin Du
**/
public class ReadPDF {
  public static final String CREATED_PDF = "geovindu.pdf";
  final static String USER_PASSWORD = "geovindu";
  final static String OWNER_PASSWORD = "geovindu";
  public static void main(String[] args) {
    try {
      PdfWriter writer = new PdfWriter(CREATED_PDF, new WriterProperties()
                    .setStandardEncryption(USER_PASSWORD.getBytes(), 
                    OWNER_PASSWORD.getBytes(), 
                    EncryptionConstants.ALLOW_PRINTING, 
                    EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
      PdfDocument pdf = new PdfDocument(writer);	
      Document document = new Document(pdf); 
      document.add(new Paragraph("Password protected PDF where only content printing is permitted content can't be copied."));
      document.close();		
    }catch (IOException e) {
      System.out.println("Exception occurred " + e.getMessage());
    }
  }
}
      

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)