对用java语言生成pdf文件的一些补充

前篇写到:用iText-2.0.8.jar以及亚洲语言包iTextAsian.jar配合生成固定格式的pdf文件,基本能满足生成要求,但今天遇到一个新需求,发现已经集成的生成方法不能满足该条件,场景如下:

在生成pdf表格时出现的问题,需要合并单元格,在上述jar包中,支持表格的setColspan(2)方法,可以合并列,但是需求需要合并行,不支持setRowspan(2)方法。


首先想到能不能用嵌套的方式,在表格中嵌套表格,以期达到目的。在xml文件中配置如下:(因为在代码中已经集成递归功能,cell向下可以包含任何单元。)

<cell>

<table colWidths="10,10" borderWidth="1" padding="0">

<cell><p>********</p></cell>

<cell><p>********</p></cell>

</table>

</cell>

效果如图所示:


未找到消除边框的方法,嵌套表格的方法暂时被搁置。


经查阅资料发现,在后续的itext 5以上的版本中已经支持setRowspan(2)方法,下载到相关jar包(itextpdf-5.4.5.jar、itext-asian.jar),进行替换,发现除了对页眉与页尾有影响之外,表格已能符合条件的生成,支持setRowspan(2),效果如下:


完美符合要求。但是该jar包又不再支持HeaderFooter类,对页脚的设置又被影响。

从网上收集相关资料,重写页脚生成类,代码如下:

import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class FooterTest extends PdfPageEventHelper {
	  
    /**
     * 页眉
     */
    public String header = "";
  
    /**
     * 文档字体大小,页脚页眉最好和文本大小一致
     */
    public int presentFontSize = 12;
  
    /**
     * 文档页面大小,最好前面传入,否则默认为A4纸张
     */
    public Rectangle pageSize = PageSize.A4;
  
    // 模板
    public PdfTemplate total;
  
    // 基础字体对象
    public BaseFont bf = null;
  
    // 利用基础字体生成的字体对象,一般用于生成中文文字
    public Font fontDetail = null;
  
    /**
     * Creates a new instance of PdfReportM1HeaderFooter 无参构造方法.
     *
     */
    public FooterTest() {
  
    }
  
    /**
     *
     * Creates a new instance of PdfReportM1HeaderFooter 构造方法.
     *
     * @param yeMei
     *            页眉字符串
     * @param presentFontSize
     *            数据体字体大小
     * @param pageSize
     *            页面文档大小,A4,A5,A6横转翻转等Rectangle对象
     */
    public FooterTest(String yeMei, int presentFontSize, Rectangle pageSize) {
        this.header = yeMei;
        this.presentFontSize = presentFontSize;
        this.pageSize = pageSize;
    }
  
    public void setHeader(String header) {
        this.header = header;
    }
  
    public void setPresentFontSize(int presentFontSize) {
        this.presentFontSize = presentFontSize;
    }
  
    /**
     *
     * TODO 文档打开时创建模板
     *
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
     */
    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(50, 50);// 共 页 的矩形的长宽高
    }
  
    /**
     *
     * TODO 关闭每页的时候,写入页眉,写入'第几页共'这几个字。
     *
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
     */
    public void onEndPage(PdfWriter writer, Document document) {
  
        try {
            if (bf == null) {
                bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
            }
            if (fontDetail == null) {
                fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 数据体字体
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  
        // 1.写入页眉
        ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(header, fontDetail), document.left(), document.top() + 20, 0);
         
        // 2.写入前半部分的 第 X页/共
        int pageS = writer.getPageNumber();
        String foot1 = "第 " + pageS + " 页 /共";
        Phrase footer = new Phrase(foot1, fontDetail);
  
        // 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = len
        float len = bf.getWidthPoint(foot1, presentFontSize);
  
        // 4.拿到当前的PdfContentByte
        PdfContentByte cb = writer.getDirectContent();
  
        // 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了 ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer, (document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F + 20F, document.bottom() - 20, 0);
  
        // 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F + len , y 轴和之前的保持一致,底边界-20
        cb.addTemplate(total, (document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20F, document.bottom() - 20); // 调节模版显示的位置
  
    }
  
    /**
     * TODO 关闭文档时,替换模板,完成整个页眉页脚组件
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
     */
    public void onCloseDocument(PdfWriter writer, Document document) {
        // 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。
        total.beginText();
        total.setFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色
        String foot2 = " " + (writer.getPageNumber() - 1) + " 页";
        total.showText(foot2);// 模版显示的内容
        total.endText();
        total.closePath();
    }
}
--摘自 http://www.tuicool.com/articles/6rYvyu

重写一个 继承PdfPageEventHelper类的方法,定义事件,在主类中将该对象事件set进PdfWriter  , PdfWriter.setPageEvent(new FooterTest)。

实现效果如下:

明天再研究下如何通过xml设置页脚的相关参数吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值