Itext的com.itextpdf.text.Paragraph 类是段落的处理。在一个段落中,你可以设置段落的对齐方式,缩进和间距。
例子一代码如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class DocumentExample {
public static void main(String[] args) {
//创建文本
Document document = new Document();
try {
//写入文本到文件中
PdfWriter.getInstance(document, new FileOutputStream("Paragraph.pdf"));
//打开文本
document.open();
//定义段落
Paragraph paragraph = new Paragraph();
//插入十条文本块到段落中
int i=0;
for(i=0; i<10; i++){
Chunk chunk = new Chunk("This is a sentence which is long " + i + ". ");
paragraph.add(chunk);
}
//添加段落
document.add(paragraph);
//关闭文本
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
运行结果如下:
[img]http://dl.iteye.com/upload/attachment/0062/5997/ec1466ab-a544-32c9-a6c6-f070b6df5422.png[/img]
暂时看来和短句的运行效果差不多,每句都在自己的行。
所添加的文本超过的文档的右边缘,段对象知道如何添加行距。行距是用单位来衡量。每英寸有72个单位。默认间距是字体高度的1.5倍。您可以更改行距间距作为参数传递给段落构造方法。
复制代码
1
Paragraph paragraph = new Paragraph(50);
您可以设置一个段落前后的间距。
复制代码
1
paragraph.setSpacingAfter(50);
paragraph.setSpacingBefore(50);
您可以设置使用setAlignment()方法的段落的对齐方式。
复制代码
1
paragraph.setAlignment(Element.ALIGN_LEFT);
paragraph.setAlignment(Element.ALIGN_CENTER);
paragraph.setAlignment(Element.ALIGN_RIGHT);
您可以设置该段左,右缩进。
复制代码
1
paragraph.setIndentationLeft(50);
paragraph.setIndentationRight(50);
例子二设置完属性的段落
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class DocumentExample {
public static void main(String[] args) {
//创建文本
Document document = new Document();
try {
//写入文本到文件中
PdfWriter.getInstance(document, new FileOutputStream("Paragraph.pdf"));
//打开文本
document.open();
//定义段落
Paragraph paragraph = new Paragraph();
//设置段落前后间距
paragraph.setSpacingAfter(25);
paragraph.setSpacingBefore(25);
//设置段段落居中
paragraph.setAlignment(Element.ALIGN_CENTER);
//设置缩进
paragraph.setIndentationLeft(50);
paragraph.setIndentationRight(50);
//插入十条文本块到段落中
int i=0;
for(i=0; i<10; i++){
Chunk chunk = new Chunk("This is a sentence which is long " + i + ". ");
paragraph.add(chunk);
}
//添加段落
document.add(paragraph);
//关闭文本
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
中心对齐,并使用左,右缩进50个用户单位。
效果如下
[img]http://dl.iteye.com/upload/attachment/0062/6008/c075d497-9d77-31b7-90bf-6be0989b01a0.png[/img]
小宝制造。
最后
以上就是潇洒酸奶最近收集整理的关于Itext 学习笔记(四) Paragraph (段)的用法的全部内容,更多相关Itext内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复