• Itext操作PDF


    <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.13</version>
    </dependency>
    //源码
    package test;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.Rectangle;
    import com.itextpdf.text.RectangleReadOnly;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfWriter;
    
    public class ReadPDF {
    	
    	private final static String FILE_DIR="D:\";
    	
    	/**
    	 * pdf文件基本操作
    	 * @param args
    	 * @throws documentumentException 
    	 * @throws FileNotFoundException 
    	 * @throws DocumentException 
    	 */
    	public static void main(String[] args) throws FileNotFoundException, DocumentException {
    		try {
    			//页面大小  
    			//Rectangle rect = new Rectangle(PageSize.B5.rotate());  //rotate()宽和高交替数值
    			Rectangle rect = new Rectangle(new RectangleReadOnly(282.86f,424.99f));//宽、高            单位/厘
    			//创建
    			Document document = new Document(rect);
    			//写入pdf
    			PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createSamplePDF.pdf"));  
    			 //页边空白 
                document.setMargins(0, 0, 0, 0);
    			document.open();  //开始向文档正文添加内容
    			
    			//内容:——————————————
    			PdfContentByte cbHG = writer.getDirectContent();
    			BaseFont bfHG= BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    			cbHG.beginText();
    			cbHG.setFontAndSize(bfHG, 15.6f);
    			cbHG.setTextMatrix(0, 52);
    			cbHG.showText("————————————————————————————————————————————");
    			cbHG.endText();
    			
    			//内容:YYZ
    			PdfContentByte cbYYZ = writer.getDirectContent();
    			BaseFont bfYYZ = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    			cbYYZ.beginText();
    			cbYYZ.setFontAndSize(bfYYZ, 15.6f);
    			cbYYZ.setTextMatrix(225, 10);
    			cbYYZ.showText("YYZ");
    			cbYYZ.endText();
    			
    			//内容:Order NO:
    			PdfContentByte cbOrderNO = writer.getDirectContent();
    			BaseFont bfOrderNO = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    			cbOrderNO.beginText();
    			cbOrderNO.setFontAndSize(bfOrderNO, 9.36f);
    			cbOrderNO.setTextMatrix(133, 40);
    			cbOrderNO.showText("Order NO:");
    			cbOrderNO.endText();
    			
    			//内容:Order NO:
    			PdfContentByte cbNO = writer.getDirectContent();
    			BaseFont bfNO = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    			cbNO.beginText();
    			cbNO.setFontAndSize(bfNO, 9.36f);
    			cbNO.setTextMatrix(180, 40);
    			cbNO.showText("JCS0858491222WS");
    			cbNO.endText();
    			
    			//内容:SKU: 6204430090*1;
    			PdfContentByte cbSKU = writer.getDirectContent();
    			BaseFont bfSKU = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    			cbSKU.beginText();
    			cbSKU.setFontAndSize(bfSKU, 9.36f);
    			cbSKU.setTextMatrix(133, 30);
    			cbSKU.showText("SKU:6204430090*1;");
    			cbSKU.endText();
    			
    			//内容:IN/NIF 442 SPEC 3696 V2
    			PdfContentByte cbNIF = writer.getDirectContent();
    			BaseFont bfNIF = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    			cbNIF.beginText();
    			cbNIF.setFontAndSize(bfNIF, 10.32f);
    			cbNIF.setTextMatrix(5, 45);
    			cbNIF.showText("IN/NIF 442 SPEC 3696 V2");
    			cbNIF.endText();
    			
    			
    			
    			/*
    			 * Paragraph tParagraphYYZ = new Paragraph(); Font fontYYZ = new
    			 * Font(Font.FontFamily.UNDEFINED, 15.6f, Font.BOLD, BaseColor.BLACK);
    			 * //字体,字体大小,粗体、字体选择黑色 Chunk YYZ = new Chunk("YYZ", fontYYZ);
    			 * tParagraphYYZ.setIndentationLeft(228f);// 左缩进
    			 * //tParagraph.setIndentationRight(12);// 右缩进
    			 * //tParagraph.setFirstLineIndent(228);// 首行缩进 //tParagraph.setLeading(400f);//
    			 * 行间距 YYZ.setTextRise(-383f); tParagraphYYZ.add(YYZ);
    			 */
    			
                Image imgOIT = Image.getInstance("D:\1573461775(1).png");
                imgOIT.setAlignment(Image.LEFT);
                imgOIT.setBorder(Image.BOX);
                imgOIT.setBorderColor(BaseColor.WHITE);
                imgOIT.scaleToFit(35, 17);// 大小
                imgOIT.setAbsolutePosition(160,10);
                
                Image imgYIWEI = Image.getInstance("D:\barcode.png");
                imgYIWEI.setAlignment(Image.LEFT);
                imgYIWEI.setBorder(Image.BOX);
                imgYIWEI.setBorderColor(BaseColor.WHITE);
                imgYIWEI.scaleToFit(200, 30);// 大小
                imgYIWEI.setAbsolutePosition(13,8);
                
    			/*
    			 * Paragraph tParagraphOrderNO = new Paragraph(); Font fontOrderNO = new
    			 * Font(Font.FontFamily.UNDEFINED, 9.36f, Font.NORMAL, BaseColor.BLACK);
    			 * //字体,字体大小,粗体、字体选择黑色 Font fontNO = new Font(Font.FontFamily.UNDEFINED, 10.32f,
    			 * Font.NORMAL, BaseColor.BLACK); //字体,字体大小,粗体、字体选择黑色 Chunk orderNO = new
    			 * Chunk("Order NO: ",fontOrderNO);
    			 * tParagraphOrderNO.setIndentationLeft(130f);// 左缩进
    			 * //tParagraphOrderNO.setLeading(350f);//行间距 orderNO.setTextRise(-365f);
    			 * tParagraphOrderNO.add(orderNO); Chunk NO = new
    			 * Chunk("JCS0858491222WS",fontNO); NO.setTextRise(-365f);
    			 * tParagraphOrderNO.add(NO);
    			 * 
    			 * 
    			 * 
    			 * 
    			 * Paragraph tParagraphSKU = new Paragraph(); Chunk SKU = new
    			 * Chunk("SKU: 6204430090*1;",fontOrderNO);
    			 * tParagraphSKU.setIndentationLeft(130f);// 左缩进 SKU.setTextRise(-345f);
    			 * tParagraphSKU.add(SKU);
    			 */
                
    			/*
    			 * Paragraph tParagraphNIF= new Paragraph(); Chunk NIF = new
    			 * Chunk("IN/NIF 442 SPEC 3696 V2",fontNO); //NIF.setTextRise(-350f);
    			 * tParagraphNIF.add(NIF);
    			 * 
    			 * document.add(tParagraphNIF);
    			 */
                document.add(imgOIT);
                document.add(imgYIWEI);
    			/*
    			 * document.add(tParagraphOrderNO); document.add(tParagraphYYZ);
    			 * document.add(tParagraphSKU);
    			 */
    			document.close(); 
    		}catch (Exception e) {
    			// TODO: handle exception
    			System.out.println(e);
    		}finally {
    			System.out.println("执行完成!");
    		}
    		
    	}
    
    }
    

     

    package test;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.Rectangle;
    import com.itextpdf.text.RectangleReadOnly;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfImportedPage;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfStamper;
    import com.itextpdf.text.pdf.PdfTemplate;
    import com.itextpdf.text.pdf.PdfWriter;
    import com.itextpdf.text.pdf.parser.PdfTextExtractor;
    
    public class Test {
    public static void main(String[] args) throws DocumentException, IOException {
    PdfStamper stamper = null;
    try {
    PdfReader reader = new PdfReader("D://JCS1104017038IN.pdf");
    String pageContent = PdfTextExtractor.getTextFromPage(reader, 1);
    stamper = new PdfStamper(reader,new FileOutputStream("d:/help2.pdf"));
    stamper.setRotateContents(true);
    //获得pdfstamper在当前页的上层打印内容.也就是说 这些内容会覆盖在原先的pdf内容之上.
    PdfContentByte over = stamper.getOverContent(1);
    //开始写入文本
    over.beginText();
    //设置字体和大小
    BaseFont bfSKU = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    over.setFontAndSize(bfSKU, 10);
    over.setColorFill(BaseColor.RED);
    //设置字体的输出位置
    over.setTextMatrix(0, -10);
    //要输出的text
    over.showText("YYN");
    over.endText();
    System.out.println("执行完成");
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }finally {
    stamper.close();
    }
    
    
    }
    /**
    * 获取来的pdf 更改页面大小
    * @param args
    * @throws DocumentException
    * @throws IOException
    */
    public static void main(String[] args) throws DocumentException, IOException {
    PdfReader reader = new PdfReader("D://JCS1104017038IN.pdf");
    Document doc = new Document(new RectangleReadOnly(282.86f,424.99f), 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("d:/help2.pdf"));
    doc.open();
    PdfContentByte cb = writer.getDirectContent();
    for(int i=1; i<=reader.getNumberOfPages(); i++){
    doc.newPage();
    PdfImportedPage page = writer.getImportedPage(reader, i);
    // cb.addTemplate(page, 1, 0, 0, 1, 1, 1);
    cb.addTemplate(page, 0,45);//调整原有pdf页面的格式改变坐标位置
    }
    //内容:——————————————
    PdfContentByte cbHG = writer.getDirectContent();
    BaseFont bfHG= BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cbHG.beginText();
    cbHG.setFontAndSize(bfHG, 15.6f);
    cbHG.setTextMatrix(0, 52);
    cbHG.showText("————————————————————————————————————————————");
    cbHG.endText();
    
    //内容:YYZ
    PdfContentByte cbYYZ = writer.getDirectContent();
    BaseFont bfYYZ = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cbYYZ.beginText();
    cbYYZ.setFontAndSize(bfYYZ, 15.6f);
    cbYYZ.setTextMatrix(225, 10);
    cbYYZ.showText("YYZ");
    cbYYZ.endText();
    
    //内容:Order NO:
    PdfContentByte cbOrderNO = writer.getDirectContent();
    BaseFont bfOrderNO = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cbOrderNO.beginText();
    cbOrderNO.setFontAndSize(bfOrderNO, 9.36f);
    cbOrderNO.setTextMatrix(133, 40);
    cbOrderNO.showText("Order NO:");
    cbOrderNO.endText();
    
    //内容:Order NO:
    PdfContentByte cbNO = writer.getDirectContent();
    BaseFont bfNO = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cbNO.beginText();
    cbNO.setFontAndSize(bfNO, 9.36f);
    cbNO.setTextMatrix(180, 40);
    cbNO.showText("JCS8888888888WS");
    cbNO.endText();
    
    //内容:SKU: 6204430090*1;
    PdfContentByte cbSKU = writer.getDirectContent();
    BaseFont bfSKU = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cbSKU.beginText();
    cbSKU.setFontAndSize(bfSKU, 9.36f);
    cbSKU.setTextMatrix(133, 30);
    cbSKU.showText("SKU:688888888*1;");
    cbSKU.endText();
    
    //内容:IN/NIF 442 SPEC 3696 V2
    PdfContentByte cbNIF = writer.getDirectContent();
    BaseFont bfNIF = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cbNIF.beginText();
    cbNIF.setFontAndSize(bfNIF, 10.32f);
    cbNIF.setTextMatrix(5, 45);
    cbNIF.showText("IN/NIF 888 SPEC 8888 V2");
    cbNIF.endText();
    
    Image imgOIT = Image.getInstance("D:\1573461775(1).png");
    imgOIT.setAlignment(Image.LEFT);
    imgOIT.setBorder(Image.BOX);
    imgOIT.setBorderColor(BaseColor.WHITE);
    imgOIT.scaleToFit(35, 17);// 大小
    imgOIT.setAbsolutePosition(160,10);
    
    Image imgYIWEI = Image.getInstance("D:\barcode.png");
    imgYIWEI.setAlignment(Image.LEFT);
    imgYIWEI.setBorder(Image.BOX);
    imgYIWEI.setBorderColor(BaseColor.WHITE);
    imgYIWEI.scaleToFit(200, 30);// 大小
    imgYIWEI.setAbsolutePosition(13,8);
    
    doc.add(imgOIT);
    doc.add(imgYIWEI);
    doc.close();
    }
    
    }
    

      效果图:

    原图:

     更改过:

      

  • 相关阅读:
    详解快速排序算法
    json和字符串、数组
    晚上回去搞这个
    json2使用方法
    mysql格式化时间函数:FROM_UNIXTIME
    java链接sqlite
    第七章 闭包
    产品家:你的闷骚,我的产品!
    关于json的格式
    JQuery选择器$()的工作原理浅析
  • 原文地址:https://www.cnblogs.com/BoKe-LY/p/11841506.html
Copyright © 2020-2023  润新知