java生成pdf

1.pdf预览

2.maven

bash 复制代码
        <!--pdf-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.9</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

3.代码

java 复制代码
package mis.shared.file;

import com.bestvike.linq.Linq;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

import java.io.FileOutputStream;

/**
 * pdf工具类
 */
public class PdfUtil {

    public static void main(String[] args) throws Exception {
        Demo();
    }

    public static void Demo() throws Exception {
        Document document = new Document(PageSize.A4, 50, 50, 30, 30); // 指定页面大小为A4,且自定义页边距(marginLeft、marginRight、marginTop、marginBottom)
        PdfWriter.getInstance(document, new FileOutputStream("E:\\develop\\tmp\\test" + System.currentTimeMillis() + ".pdf"));
        document.open();

        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font keyfont = new Font(bfChinese, 10, Font.BOLD);
        Font textfont = new Font(bfChinese, 10, Font.NORMAL);

        Paragraph wrap = new Paragraph("\r\n", textfont);

        // 段落
        Paragraph paragraph = new Paragraph("知之愈明,则行之愈笃;行之愈笃,则知之益明。------朱熹(宋)", textfont);

        // 直线
        Paragraph line = new Paragraph();
        line.add(new Chunk(new LineSeparator()));

        // 超链接
        Anchor link = new Anchor("点击跳转到百度", textfont);
        link.setReference("www.baidu.com");

        // 添加图片
        Image image = Image.getInstance("E:\\develop\\tmp\\install.png");
        image.setAlignment(Image.ALIGN_CENTER);
        image.scalePercent(40); //依照比例缩放

        // 表格
        PdfPTable table = createTable(new float[]{80, 80, 80, 80, 80, 80});
        table.addCell(createCell("早上9:00", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("中午11:00", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("中午13:00", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("下午15:00", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("下午17:00", keyfont, Element.ALIGN_CENTER));
        table.addCell(createCell("晚上19:00", keyfont, Element.ALIGN_CENTER));
        for (int i = 0; i < 5; i++) {
            table.addCell(createCell("起床", textfont));
            table.addCell(createCell("吃午饭", textfont));
            table.addCell(createCell("午休", textfont));
            table.addCell(createCell("下午茶", textfont));
            table.addCell(createCell("回家", textfont));
            table.addCell(createCell("吃晚饭", textfont));
        }

        //最后一行
        table.addCell(createCell("总计", textfont, Element.ALIGN_CENTER));
        // 处理最后一行边框合并
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorderWidthTop(0);
        cell.setBorderWidthLeft(0);
        cell.setColspan(5);
        cell.setPhrase(new Phrase("5天", textfont));
        table.addCell(cell);

        // 开始从内存写入到磁盘 pdf文件中
        document.add(paragraph);
        document.add(line);
        document.add(link);
        document.add(wrap);
        document.add(table);
        document.add(image);
        document.close();
    }

    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(Linq.of(widths).sumFloat());
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    public static PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    public static PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

}
相关推荐
JL1510 分钟前
Java+Go 混合架构怎么搭?收官 50 道面试题 + 学习路线
java·架构·golang
Tattoo_Welkin21 分钟前
IDEA 中常用操作记载
java·elasticsearch·intellij-idea
音符犹如代码26 分钟前
Arthas classloader + sc 实战:JVM 类加载与手动加载
java·jvm·spring boot
乐启国际旅行社有限公司41 分钟前
Java线程池实战:文旅系统批量任务性能优化(团期生成/数据导出)
java·开发语言·性能优化
Listen·Rain1 小时前
用AI开发出一个AI
java·人工智能·spring boot·tomcat·intellij-idea·mybatis·visual studio
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第203题】【09_Zookeeper篇】第4题:ZooKeeper 的节点类型有哪些?
java·zookeeper·分布式锁·分布式协调·znode
宸津-代码粉碎机1 小时前
告别手动Jar部署!生产级无损热部署方案,彻底解决OOM与更新失效问题
java·大数据·开发语言·人工智能·python
米码收割机1 小时前
【移动】线上购物移动端网站(源码+文档)【独一无二】
java·开发语言·前端·python·django
无敌秋1 小时前
python/c++/java上云
java·c++·python
都叫我大帅哥1 小时前
BCrypt 还是 Argon2?Spring Security 密码加密方案深度解析与实战对比
java·spring