• itextpdf5操作文本


    itextpdf使用document操作文本可以使用3个对象来做:Chunk、Phrase、Paragraph。

    itextpdf5的包对它们的介绍是这样的:

    chunk:

    这是可以添加到文档中最小的重要部分。
    大多数元素可以划分为一个或多个块。chunkis是一个带有特定字体的字符串。所有其他布局参数都应该在这个textis块添加到的对象中定义。

    Phrase:

    短语是一系列的块。
    一个短语有一个主字体,但是短语中的一些块可以有不同于主字体的字体。一个短语中的所有块都有相同的开头。

    Paragraph:

    A Paragraph is a series of Chunks and/or Phrases.
    A Paragraph has the same qualities of a Phrase, but alsosome additional layout-parameters:
    •the indentation
    •the alignment of the text

    它们有一些自己的特点:

    在继承结构上,paragraph继承了phrase。phrase和paragraph的文本会自动换行,而chunk是不会自动换行的,超出页面的部分不会被显示。

    所以,chunk可以设置文本本身的一些属性,如文字背景,下划线,行高。

    而paragraph可以操作文字的排版,段落的间距,行间距,等等。phrase功能跟单一,能设置行间距,也被paragraph继承了。

    1、Paragraph的一些方法的功能:

    代码:

            Paragraph paragraph = new Paragraph(text,firstCoverFont);
            /** 设置行间距,俩个参数:float fixedLeading, float multipliedLeading,
             * 总的行间距可以用getTotalLeading()来查看, 可以理解是两行文字,顶部到顶部的距离,如果是0,两行文字是重叠的。
             * 计算方式:fixedLeading+multipliedLeading*fontSize。在pdfDocument中是当前字体;在ColumnText,是指最大字体。
             * 其中fixedLeading是固定参数,默认值是:1.5倍的fontsize
             * multipliedLeading是可变参数,默认值是0.
             */
            paragraph.setLeading(10,2);//设置行间距
            paragraph.setAlignment(Element.ALIGN_CENTER);//设置对齐方式:居中、左对齐、右对齐
            paragraph.setFirstLineIndent(20f);//设置首行缩进
            
            paragraph.setExtraParagraphSpace(200f);//设置段落之间的空白,测试无效,可以没找到正确的书写格式
            paragraph.setIndentationLeft(10f);//设置段落整体的左缩进
            paragraph.setIndentationRight(20f);//设置段落整体的右缩进
            paragraph.setPaddingTop(10f);//设置距离上一元素的顶部padding
            paragraph.setSpacingAfter(10f);//设置段落下方的空白距离
            paragraph.setSpacingBefore(10f);//设置段落上方的留白,,如果的本页的第一个段落,该设置无效
            doc.add(paragraph);

     2、chunk的方法

            Chunk chunk = new Chunk("测试chunk",firstCoverFont);
            chunk.setBackground(BaseColor.GREEN);//文字背景色
            chunk.setLineHeight(10);//行高
            chunk.setUnderline(2, 3);//下划线,或者文字任意文字的线条
            doc.add(chunk);

     3、将文本放置的任意大小,页面任意位置

    官方文档:

            String path = "E:/demo/pdfCreat/"+System.currentTimeMillis()+".pdf";//输出pdf的路径
            Document doc = new Document();
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path));
            doc.open();
            String text = readfile("src/main/resources/file/pdf/test.text");
            Rectangle rect = new Rectangle(300, 300, 400, 400);//文本框位置
            rect.setBorder(Rectangle.LEFT);//显示边框,默认不显示,常量值:LEFT, RIGHT, TOP, BOTTOM,BOX,
            rect.setBorderWidth(1f);//边框线条粗细
            rect.setBorderColor(BaseColor.GREEN);//边框颜色
            PdfContentByte cb = writer.getDirectContent();
            cb.rectangle(rect);
            
            ColumnText ct = new ColumnText(cb);
            ct.addText(new Phrase("test",firstCoverFont));
            ct.setSimpleColumn(rect);
            ct.setUseAscender(false);
            ct.go();

    简单方式:

            Rectangle rect = new Rectangle(300, 300, 400, 400);//文本框位置
            rect.setBorder(Rectangle.LEFT);//显示边框,默认不显示,常量值:LEFT, RIGHT, TOP, BOTTOM,BOX,
            rect.setBorderWidth(1f);//边框线条粗细
            rect.setBorderColor(BaseColor.GREEN);//边框颜色
            ColumnText cts = new ColumnText(writer.getDirectContent());
            cts.addText(new Phrase("test",firstCoverFont));
            cts.setSimpleColumn(rect);
            cts.go();
    就算这个世道烂成一堆粪坑,那也不是你吃屎的理由
  • 相关阅读:
    QT解决方案中新建动态链接库工程,且继承于QObject,解决无法生成moc_XXX.cpp文件的问题,解决工程之间的引用问题
    MATLAB Simulink中解算器的定步长和各模块采样时间之间的关系
    simulink使用MATLAB function的端口多出入多输出,输入输出向量设置
    simulink error:Error in default port dimensions function of S-function ‘XXXXXXXXXXX’. This function does not fully set the dimensions of output port 2.
    Git 问题:SSL certificate problem: self signed certificate
    git push失败:ssh:connect to host github.com port 22:Connection timed out
    戴尔笔记本插耳机听歌暂停后继续声音突然变大
    PDF阅读器和编辑器选择
    21天战拖记——Day15:填坑:行动任务项目的区别(2014-05-18)
    21天战拖记——Day14:如何养成一个习惯?(2014-05-17)
  • 原文地址:https://www.cnblogs.com/whalesea/p/11766476.html
Copyright © 2020-2023  润新知