添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
public static PdfFont getChineseFont() { try { return PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false); } catch (IOException e) { e.printStackTrace(); return null;

绘制PDF方法

* @param document public void drawBaseInfo(Document document) { DeviceRgb black = new DeviceRgb(0, 0, 0); DeviceRgb gray = new DeviceRgb(72, 127, 255); // 黑色虚线边框 DashedBorder grayBorder = new DashedBorder(gray, 0.5F); SolidBorder solidBorder = new SolidBorder(black, 0.5F); Table table = new Table(4).setMarginLeft(50).setMarginRight(50); table.setBorder(null); table.setMarginTop(50); PdfDocument pdfDocument = document.getPdfDocument(); // 条码 PdfPage page = pdfDocument.addNewPage(); Barcode128 barcode128 = new Barcode128(pdfDocument); barcode128.setCode("072201130001"); barcode128.setCodeType(Barcode128.CODE128); barcode128.fitWidth(180); PdfFormXObject object = barcode128.createFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDocument); float width = object.getWidth(); float height = object.getHeight(); float x = 200; float y = 780; PdfCanvas canvas = new PdfCanvas(page); canvas.saveState(); canvas.setFillColor(ColorConstants.WHITE); canvas.rectangle(x, y, width, height); canvas.fill(); canvas.restoreState(); canvas.addXObjectAt(object, x, y); document.setFontSize(18); // 头部 table.addCell( new Cell(6, 4).setBorder(null).setMargin(0).setPadding(0).add( new Table(4).setMargin(0).setPadding(0) .setTextAlignment(TextAlignment.LEFT) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("发车站:").setFont(PDFUtil.getChineseFont()) .add(new Text(" 郑州高铁长途枢纽站").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("")) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("发车时间:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" 19:00").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("")) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("班次号:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" HJ03").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("车牌号:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" 豫GH6666").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("线路:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" 郑州-漯河").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("")) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("单据号:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" 072201130001").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("检票人数:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" 12").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("类别:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text(" 固定班").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell(new Cell(1, 2).setPaddingLeft(0) .setBorder(null) .add(new Paragraph("")) // 中间部分 Table detail = new Table(4).setMarginLeft(50).setMarginRight(50).setMarginTop(12); detail.addCell(new Cell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE).setTextAlignment(TextAlignment.CENTER) .setWidth(70).setBorder(null).setBorder(solidBorder).add(new Paragraph("到达站").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell( new Cell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE).setTextAlignment(TextAlignment.CENTER) .setWidth(70).setBorder(null).setBorder(solidBorder).add(new Paragraph("票价").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell( new Cell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE).setTextAlignment(TextAlignment.CENTER) .setWidth(70).setBorder(null).setBorder(solidBorder).add(new Paragraph("人数").setFont(PDFUtil.getChineseFont()).setFontSize(18))) .addCell( new Cell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE).setTextAlignment(TextAlignment.CENTER) .setWidth(120).setBorder(null).setBorder(solidBorder).add(new Paragraph("检票收入").setFont(PDFUtil.getChineseFont()).setFontSize(18))); for (int i = 0; i < 10; i++) { detail.addCell(new Cell(1, 1).add(new Paragraph("") .add(new Text("站点" + i).setFont(PDFUtil.getChineseFont()).setFontSize(16))).setTextAlignment(TextAlignment.CENTER)); detail.addCell(new Cell(1, 1).add(new Paragraph("") .add(new Text(i + "元").setFont(PDFUtil.getChineseFont()).setFontSize(16))).setTextAlignment(TextAlignment.CENTER)); detail.addCell(new Cell(1, 1).add(new Paragraph("") .add(new Text(i + "人").setFont(PDFUtil.getChineseFont()).setFontSize(16))).setTextAlignment(TextAlignment.CENTER)); detail.addCell(new Cell(1, 1).add(new Paragraph("") .add(new Text(i + "收入").setFont(PDFUtil.getChineseFont()).setFontSize(16))).setTextAlignment(TextAlignment.CENTER)); // 尾部 Table tail = new Table(1).setMarginLeft(50).setMarginRight(50).setMarginTop(15).setBorder(null); tail.addCell(new Cell(1, 1).add(new Paragraph("总人数:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text("10").setFont(PDFUtil.getChineseFont()).setFontSize(16))).setBorder(null)) .addCell(new Cell(1, 1).add(new Paragraph("结算金额:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text("10").setFont(PDFUtil.getChineseFont()).setFontSize(16))).setBorder(null)) .addCell(new Cell(1, 1).add(new Paragraph("大写:").setFont(PDFUtil.getChineseFont()).setMarginRight(4) .add(new Text("10").setFont(PDFUtil.getChineseFont()).setFontSize(16))).setBorder(null)) .addCell(new Cell(1, 1).add(new Paragraph("电子结算单生成时间:").setFont(PDFUtil.getChineseFont()).setMarginRight(4).setMarginTop(10) .add(new Text("2022-01-11 12:00:59").setFont(PDFUtil.getChineseFont()))).setBorder(null).setFontColor(Color.convertRgbToCmyk(gray)).setFontSize(16)); // 整体水平居中 table.setHorizontalAlignment(HorizontalAlignment.LEFT); detail.setHorizontalAlignment(HorizontalAlignment.LEFT); tail.setHorizontalAlignment(HorizontalAlignment.LEFT); document.add(table); document.add(detail); document.add(tail);

测试类调用生成PDF文件

@SpringBootTest
class SpringbootPdfApplicationTests {
    @Test
    void contextLoads() throws FileNotFoundException {
        //首先你需要一个writer对象
        PdfWriter writer = new PdfWriter("D:/test.pdf");
        //需要一个pdf的对象来操作pdf
        PdfDocument pdf = new PdfDocument(writer);
        //需要一个document的对象来操作数据 指定为A4大小
        Document document = new Document(pdf, PageSize.A4);
        drawBaseInfo(document);
        document.close();

引入图片(盖章)

Image maru = new Image(ImageDataFactory.create("imgurl图片地址", true));
        maru.setFixedPosition(320, 728);// 控制图片位置
        maru.scaleAbsolute(120, 80); // 控制图片大小
        document.add(maru);
 

itextpdf的确定页面上的具体坐标是通过坐标来确定的。
它们的坐标轴是这样的:以左下角为原点的xy坐标轴。

完整代码下载

地址:https://www.wxy97.com

简介:全栈工程师一枚, Java, Golang, Python, Web, 运维技术分享。