其他分享
首页 > 其他分享> > pdf文件基本操作

pdf文件基本操作

作者:互联网

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("执行完成!");
		}
		
	}

}

  

标签:文件,document,BaseFont,import,pdf,add,new,基本操作,Font
来源: https://www.cnblogs.com/BoKe-LY/p/11841414.html