java 7 pdf_java使用itextpdf7实现导出pdf表格;java使用itextpdf7实现pdf加水印

packagecom.test;importcom.itextpdf.io.font.FontConstants;importcom.itextpdf.kernel.color.Color;importcom.itextpdf.kernel.events.Event;importcom.itextpdf.kernel.events.IEventHandler;importcom.itextpdf.kernel.events.PdfDocumentEvent;importcom.itextpdf.kernel.font.PdfFont;importcom.itextpdf.kernel.font.PdfFontFactory;importcom.itextpdf.kernel.pdf.PdfDocument;importcom.itextpdf.kernel.pdf.PdfPage;importcom.itextpdf.kernel.pdf.PdfWriter;importcom.itextpdf.kernel.pdf.canvas.PdfCanvas;importcom.itextpdf.layout.Canvas;importcom.itextpdf.layout.Document;importcom.itextpdf.layout.element.Cell;importcom.itextpdf.layout.element.Paragraph;importcom.itextpdf.layout.element.Table;importcom.itextpdf.layout.property.TextAlignment;importcom.itextpdf.layout.property.VerticalAlignment;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.util.StringTokenizer;/*** 生成pdf表格(带水印)

*@authorkpzc

* @date 2019年1月21日 下午3:55:43

* @desc*/

public classWatermarking {public static final String DATA = "C:\\Users\\kpzc\\Desktop\\united_states.csv";public static final String DEST = "E:\\bbb\\bb.pdf";public static void main(String[] args) throwsException {

File file= newFile(DEST);

file.getParentFile().mkdirs();newWatermarking().manipulatePdf(DEST);

}public void process(Table table, String line, PdfFont font, booleanisHeader) {

StringTokenizer tokenizer= new StringTokenizer(line, ";");int c = 0;while (tokenizer.hasMoreTokens() && c++ < 3) {if(isHeader) {

table.addHeaderCell(new Cell().add(newParagraph(tokenizer.nextToken()).setFont(font)));

}else{

table.addCell(new Cell().add(newParagraph(tokenizer.nextToken()).setFont(font)));

}

}

}protected void manipulatePdf(String dest) throwsException {

PdfDocument pdfDoc= new PdfDocument(newPdfWriter(DEST));

Document doc= newDocument(pdfDoc);

pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE,newWatermarkingEventHandler());//PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);

PdfFont font =PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);//支持中文//PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);

PdfFont bold =PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);//支持中文

Table table= new Table(new float[]{4, 1, 3});

table.setWidthPercent(100);

BufferedReader br= new BufferedReader(newFileReader(DATA));

String line=br.readLine();

process(table, line, bold,true);while ((line = br.readLine()) != null) {

process(table, line, font,false);

}

br.close();

doc.add(table);

doc.close();

}protected class WatermarkingEventHandler implementsIEventHandler {

@Overridepublic voidhandleEvent(Event event) {

PdfDocumentEvent docEvent=(PdfDocumentEvent) event;

PdfDocument pdfDoc=docEvent.getDocument();

PdfPage page=docEvent.getPage();

PdfFont font= null;try{//font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);

font =PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);//支持中文

} catch(IOException e) {

e.printStackTrace();

}

PdfCanvas pdfCanvas= newPdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);newCanvas(pdfCanvas, pdfDoc, page.getPageSize())

.setFontColor(Color.LIGHT_GRAY)

.setFontSize(60)

.setFont(font)

.showTextAligned(new Paragraph("我是水印\r\t鲲鹏展翅"), 298, 421, pdfDoc.getPageNumber(page),

TextAlignment.CENTER, VerticalAlignment.MIDDLE,45);

}

}

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页