Itextpdf添加页眉页脚页码

项目需要样式比较复杂,刚开始因为时间比较紧所以采用了Itextpdf插件代码生成pdf ,实话说过程十分繁琐,因为pdf文件样式比较多,表格也比较多,各种的表格,还有就是页眉页脚页码都要自己找页面位置坐标但是又不能像html那样方便更改查看样式,只能改一点导出来看看合适不,然后再改再导出来看。
私下调研了很多,现在罗列一下我所用到的一些样式处理,希望可以帮助需要做这个功能的朋友少踩坑,少尝试,直接出完美pdf报告。

  1. 封面
    在这里插入图片描述
  2. 首页在这里插入图片描述
    封面右上角那个图片可以换成logo,我做的大概是这样的一个样子,上代码
//定义 页面大小,以及页边距左右上下
package com.example.demo.controller;

import com.example.demo.config.PDFConfig;
import com.example.demo.utils.PDFBuilder;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author hongli.zhang
 * @create 2020/5/26 13:02
 **/

@RestController
@RequestMapping("/businessApplication/")
public class PdfContrller {

    @Autowired
    PDFConfig config;//这个是配置类,里面设置了需要用到的字体和logo图片的绝对路径
    public static void setImg(PdfWriter writer, String path, float fitWidth, float fitHeight, float absoluteX, float absoluteY) {
        try {
            Image image = Image.getInstance(path);
            image.setAlignment(Image.MIDDLE);
            image.scaleToFit(fitWidth, fitHeight);
            image.setAbsolutePosition(absoluteX, absoluteY);
            writer.getDirectContent().addImage(image);
        }catch(Exception e){
        }
    }
    @GetMapping("/test")
    public void test(HttpServletRequest requ, HttpServletResponse resp) throws DocumentException {

//设置页面大小为A4纸大小,以及页边距左右上下
        Document document = new Document(PageSize.A4, 100F, 100F, 80F, 120F);
        PdfWriter writer = null;
        try {
            FileOutputStream out = new FileOutputStream("E:\\new.pdf");
            writer = PdfWriter.getInstance(document, out);
        } catch ( IOException | DocumentException e) {
            e.printStackTrace();
        }
        // 定义页眉和页脚页码事件,PDFBuilder代码在后面
        PDFBuilder builder = new PDFBuilder();
        //设置页面监听事件
        writer.setPageEvent(builder);

        document.open();
        //换行
        Paragraph newLine = new Paragraph("\n");
        float tableWidthPercentage = 100f; //表格的整体宽度

        //表格背景色
        BaseColor green = new BaseColor(175, 215, 136);
        BaseColor blue = new BaseColor(148, 170, 214);

        //图标
        setImg(writer, config.getLogo(), 130, 100, document.getPageSize().getRight(230), 700);
        //所需字体
        String fontPath = config.getCalibri();
        String fontBlodPath = config.getCalibrib();
        BaseFont bf = null;
        BaseFont blodbf = null;
//        BaseFont chineseFont = null;
//        BaseFont chineseBlodFont = null;
        try {
            bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            blodbf = BaseFont.createFont(fontBlodPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//            chineseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
        Font coverFont = new Font(blodbf, 30, Font
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Java中使用iText库生成PDF并在页眉中插入图片的方法如下: 首先,你可以创建一个页眉页脚类,根据实际情况使用,如果不需要页眉页脚可以忽略这一步。通过参考中的链接可以了解更多关于页眉页脚带图片的详细信息。 接下来,你需要使用iText库生成PDF。你可以通过参考中的链接找到一个示例代码,并在生成PDF的过程中插入图片。具体来说,在创建表格的过程中,你可以在表格的某一行中插入图片,例如在createHardwarePDF方法的第九行。 最后,你可以参考中的链接来了解更多关于JavaiText生成PDF并插入图片的信息和效果演示。 这样,你就可以在Java中使用iText库生成PDF并在页眉中插入图片了。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [itextpdf 使用之 html 转 pdf 页眉页脚带图片](https://download.csdn.net/download/z1353095373/85382971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [JavaItext生成Pdf,并给PdfCell添加图片](https://blog.csdn.net/qq_17847881/article/details/130180328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Itextpdf添加页眉页脚页码页眉中需要添加logo图片](https://blog.csdn.net/qb170217/article/details/118718685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值