如何在ColumnText中添加章节标题? [英] How can I add titles of chapters in ColumnText?
问题描述
请问,我如何在ColumnText中添加章节的标题?
我需要像这样制作PDF:
| ColumnText column1 | ColumnText column2 |
|具有内容的PdfPTable |具有内容的PdfPTable |
| |第2章标题|
|第1章标题| |
然后将TOC添加到此文档中。
我使用ColumnText和表格制作文档。但是不能在表格中添加章节。
我只能将章节添加到文档正文中,但在本例中,章节的标题不在ColumnText中。
示例,了解它是如何完成的。
就像你一样,我用标题和表格创建一个 ColumnText
对象:
ColumnText ct = new ColumnText(writer.getDirectContent());
int start;
int end;
for(int i = 0; i< = 20;){
start =(i * 10)+ 1;
i ++;
end = i * 10;
String title = String.format(%s到%s的数字,开始,结束);
Chunk c = new Chunk(title);
c.setGenericTag(title);
ct.addElement(c);
ct.addElement(createTable(start,end));
}
int column = 0;
do {
if(column == 3){
document.newPage();
column = 0;
}
ct.setSimpleColumn(COLUMNS [column ++]);
} while(ColumnText.hasMoreText(ct.go()));
结果如下:
尽管有规则要在StackOverflow上发布问题,您没有发布代码示例,但您的代码和我的代码之间至少有一个区别:
c.setGenericTag(标题);
在这一行中,我们声明了一个通用标签。此标记由 TOCEntry
类使用,如下所示:
公共类TOCCreation扩展PdfPageEventHelper {
protected PdfOutline root;
protected List< TOCEntry> toc = new ArrayList< TOCEntry>();
public TOCCreation(){
}
public void setRoot(PdfOutline root){
this.root = root;
}
公开列表< TOCEntry> getToc(){
return toc;
}
@Override
public void onGenericTag(PdfWriter writer,Document document,Rectangle rect,String text){
PdfDestination dest = new PdfDestination(PdfDestination.XYZ, rect.getLeft(),rect.getTop(),0);
new PdfOutline(root,dest,text);
TOCEntry entry = new TOCEntry();
entry.action = PdfAction.gotoLocalPage(writer.getPageNumber(),dest,writer);
entry.title = text;
toc.add(entry);
}
}
如您所见,我们创建 PdfDestination
根据标题的位置:
PdfDestination dest = new PdfDestination(PdfDestination .XYZ,rect.getLeft(),rect.getTop(),0);
如果你想要书签,你可以创建 PdfOutline
像这样:
new PdfOutline(root,dest,text);
如果你想要TOC,你可以存储一个 String
和一个 PdfAction
在列表中
:
TOCEntry entry = new TOCEntry();
entry.action = PdfAction.gotoLocalPage(writer.getPageNumber(),dest,writer);
entry.title = text;
toc.add(entry);
现在我们了解了 TOCCreation
类,我们来看看如何使用它:
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(dest));
TOCCreation事件=新TOCCreation();
writer.setPageEvent(event);
document.open();
event.setRoot(writer.getRootOutline())
我们创建 event
object,将其传递给 writer
,在我们打开文档后,我们将大纲树的根传递给事件。书签将自动创建,TOC不会。如果你想添加TOC,你需要这样的东西:
document.newPage();
for(TOCEntry entry:event.getToc()){
Chunk c = new Chunk(entry.title);
c.setAction(entry.action);
document.add(新的(c)段);
}
您现在有一个标题列表,您可以点击它们跳转到相应的表。
Please, how i can add titles of the Chapters in ColumnText? I need make PDF like this:
| ColumnText column1 | ColumnText column2 |
| PdfPTable with content | PdfPTable with content |
| | Chapter 2 title |
| Chapter 1 title | |
And then add TOC to this document. I make document with ColumnText and table in it. But can't add Chapter in table. I can add Chapter only to the document body, but in this case title of Chapter not in ColumnText.
Image of one page of the result document here
Your question isn't clear in the sense that you don't tell us if you want a TOC like this:
If this is the case, you are using the wrong terminology, as what you see in the Bookmarks panel can be referred to as Outlines or bookmarks.
If you say you want a TOC, you want something like this:
I mention both, because you talk about the Chapter
(a class you should no longer use) and that class creates bookmarks/outlines, not a TOC.
I have create a PDF file that has both, bookmarks and a TOC: columns_with_toc.pdf. Please take a look at the CreateTOCinColumn example to find out how it's done.
Just like you, I create a ColumnText
object with titles and tables:
ColumnText ct = new ColumnText(writer.getDirectContent());
int start;
int end;
for (int i = 0; i <= 20; ) {
start = (i * 10) + 1;
i++;
end = i * 10;
String title = String.format("Numbers from %s to %s", start, end);
Chunk c = new Chunk(title);
c.setGenericTag(title);
ct.addElement(c);
ct.addElement(createTable(start, end));
}
int column = 0;
do {
if (column == 3) {
document.newPage();
column = 0;
}
ct.setSimpleColumn(COLUMNS[column++]);
} while (ColumnText.hasMoreText(ct.go()));
The result looks like this:
In spite of the rules for posting a question on StackOverflow, you didn't post a code sample, but there is at least one difference between your code and mine:
c.setGenericTag(title);
In this line, we declare a generic tag. This tag is used by the TOCEntry
class that looks like this:
public class TOCCreation extends PdfPageEventHelper {
protected PdfOutline root;
protected List<TOCEntry> toc = new ArrayList<TOCEntry>();
public TOCCreation() {
}
public void setRoot(PdfOutline root) {
this.root = root;
}
public List<TOCEntry> getToc() {
return toc;
}
@Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
PdfDestination dest = new PdfDestination(PdfDestination.XYZ, rect.getLeft(), rect.getTop(), 0);
new PdfOutline(root, dest, text);
TOCEntry entry = new TOCEntry();
entry.action = PdfAction.gotoLocalPage(writer.getPageNumber(), dest, writer);
entry.title = text;
toc.add(entry);
}
}
As you can see, we create a PdfDestination
based on the position of the title:
PdfDestination dest = new PdfDestination(PdfDestination.XYZ, rect.getLeft(), rect.getTop(), 0);
If you want bookmarks, you can create a PdfOutline
like this:
new PdfOutline(root, dest, text);
If you want a TOC, you can store a String
and a PdfAction
in a List
:
TOCEntry entry = new TOCEntry();
entry.action = PdfAction.gotoLocalPage(writer.getPageNumber(), dest, writer);
entry.title = text;
toc.add(entry);
Now that we understand the TOCCreation
class, we take a look at how to use it:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
TOCCreation event = new TOCCreation();
writer.setPageEvent(event);
document.open();
event.setRoot(writer.getRootOutline())
We create an event
object, pass it to the writer
and after we've opened the document, we pass the root of the outline tree to the event. The bookmarks will be created automatically, the TOC won't. If you want to add the TOC, you need something like this:
document.newPage();
for (TOCEntry entry : event.getToc()) {
Chunk c = new Chunk(entry.title);
c.setAction(entry.action);
document.add(new Paragraph(c));
}
You now have a list of titles which you can click to jump to the corresponding table.
这篇关于如何在ColumnText中添加章节标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!