您的位置:首页 > 娱乐 > 八卦 > 王爷他精分成疾_ui网页设计公司_seo综合查询是什么意思_怎么做网络营销

王爷他精分成疾_ui网页设计公司_seo综合查询是什么意思_怎么做网络营销

2024/12/25 23:33:59 来源:https://blog.csdn.net/qq_42221396/article/details/143142518  浏览:    关键词:王爷他精分成疾_ui网页设计公司_seo综合查询是什么意思_怎么做网络营销
王爷他精分成疾_ui网页设计公司_seo综合查询是什么意思_怎么做网络营销

pom坐标

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.3</version></dependency>

字体文件可以再电脑系统中复制


public class CreatePdfWithIText {public static void main(String[] args) {String dest = "example.pdf";// 创建 File 对象File file = new File(dest);try {// 注册字体BaseFont baseFont = BaseFont.createFont("src/main/resources/static/STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font font = new Font(baseFont, 12, Font.NORMAL);// 创建输出流FileOutputStream fileOutputStream = new FileOutputStream(file);// 创建 Document 对象Document pdf = new Document();// 创建 PdfWriter 对象PdfWriter writer = PdfWriter.getInstance(pdf, fileOutputStream);// 添加页面事件监听器(例如,添加页眉和页脚)writer.setPageEvent(new HeaderFooterPageEvent());// 打开文档pdf.open();// 添加标题pdf.add(new Paragraph("     测试1111",font));// 添加段落pdf.add(new Paragraph("This is an example of a PDF document created using iText."));pdf.add(new Paragraph(" "));// 添加表格PdfPTable table = new PdfPTable(3);PdfPCell cell = new PdfPCell(new Paragraph("Header 1",font));table.addCell(cell);table.addCell("Header 2");table.addCell("Header 3");table.addCell("Row 1, Cell 1");table.addCell("Row 1, Cell 2");table.addCell("Row 1, Cell 3");// 合并单元格PdfPCell mergedCell = new PdfPCell(new Paragraph("Merged Cell"));mergedCell.setCellEvent(new GradientBackgroundEvent(mergedCell,BaseColor.BLUE, BaseColor.WHITE,writer));mergedCell.setColspan(2); // 合并两列mergedCell.setHorizontalAlignment(Element.ALIGN_CENTER);//内容居中table.addCell(mergedCell);table.addCell(new PdfPCell(new Paragraph("测试数据23车上",font)));table.completeRow();pdf.add(table);// 添加带下划线的文字Chunk underlinedChunk = new Chunk("测试数据.",font);underlinedChunk.setUnderline(0.1f, -2f); // 设置下划线的粗细和位置Phrase underlinedPhrase = new Phrase(underlinedChunk);pdf.add(new Paragraph(underlinedPhrase));// 添加书签PdfOutline root = writer.getRootOutline();PdfOutline bookmark1 = new PdfOutline(root, new PdfDestination(PdfDestination.FITH, pdf.getPageSize().getTop(pdf.getPageSize().getHeight() - 72)), "Title 1");PdfOutline bookmark2 = new PdfOutline(bookmark1, new PdfDestination(PdfDestination.FITH, pdf.getPageSize().getTop(pdf.getPageSize().getHeight() - 144)), "Sub-title 1.1");// 插入图片Image image = Image.getInstance("src/main/resources/static/lQLPJx8kenVARDfNBfrNC0CwMrMiu82UNQUG_l2o6frWAA_2880_1530.png");image.scaleToFit(100, 100); // 设置图片大小image.setAbsolutePosition(50, 750); // 设置图片位置 (x, y)// 设置透明度PdfContentByte canvas = writer.getDirectContent();PdfGState gState = new PdfGState();gState.setFillOpacity(0.5f); // 设置透明度,0.0f 完全透明,1.0f 完全不透明canvas.setGState(gState);canvas.addImage(image);pdf.add(image);// 关闭文档pdf.close();System.out.println("PDF created successfully.");} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}// 自定义页面事件监听器static class HeaderFooterPageEvent extends PdfPageEventHelper {@Overridepublic void onEndPage(PdfWriter writer, Document document) {PdfContentByte cb = writer.getDirectContent();cb.beginText();try {cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);} catch (DocumentException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "Header", document.right()/2 , document.top() + 10, 0);cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "Footer", document.right()/2, document.bottom() - 10, 0);cb.endText();}}// 自定义渐变色背景事件static class GradientBackgroundEvent implements PdfPCellEvent {private BaseColor startColor;private BaseColor endColor;private PdfWriter writer;private PdfPCell rectangleItem;public GradientBackgroundEvent(PdfPCell rectangleItem,BaseColor startColor, BaseColor endColor, PdfWriter writer) {this.startColor = startColor;this.endColor = endColor;this.writer = writer;this.rectangleItem = rectangleItem;}@Overridepublic void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {PdfShading shading = PdfShading.simpleAxial(this.writer,position.getLeft(), position.getBottom(),position.getRight(), position.getTop(),startColor, endColor);PdfShadingPattern pattern = new PdfShadingPattern(shading);PdfGState gstate = new PdfGState();gstate.setFillOpacity(0.5f);//不透明度canvases[PdfPTable.BACKGROUNDCANVAS].saveState();canvases[PdfPTable.BACKGROUNDCANVAS].setGState(gstate);canvases[PdfPTable.BACKGROUNDCANVAS].setShadingFill(pattern);//命中单元格,仅填充单元格填充
//            canvases[PdfPTable.BACKGROUNDCANVAS].paintShading(pattern);//整个文档的填充
//            canvases[PdfPTable.BACKGROUNDCANVAS].setShadingStroke(pattern);// 颜色灰色,仅单元格填充canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());canvases[PdfPTable.BACKGROUNDCANVAS].fill();canvases[PdfPTable.BACKGROUNDCANVAS].restoreState();}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com