如何在所有页面上重复标题 - iText pdf [英] How to give repeating title on all pages - iText pdf

查看:843
本文介绍了如何在所有页面上重复标题 - iText pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标题表和一个内容表。我的内容表太长,不适合单个页面。所以我想在每页后重复我的标题表。我怎样才能做到这一点?请帮忙。



这是我的代码

 文件tempFile =文件.createTempFile(progress,tmp); 
凭证凭证=新凭证(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(tempFile));
document.setMargins(6,6,36,36);
document.setMarginMirroring(true);
document.open();
PdfPTable titleTable = new PdfPTable(new float [] {18f});
// Image img = Image.getInstance(uploadFolder +/ logo.jpg);
字体titleFont = new Font(getBaseFont(),12f,Font.BOLD);
字体tHeadFont = new Font(getBaseFont(),10f,Font.BOLD);
Font contentFont = new Font(getBaseFont(),10f);
// img.scalePercent(50);
titleTable.setWidthPercentage(100);
titleTable.getDefaultCell()。setBorder(PdfPCell.NO_BORDER);
titleTable.getDefaultCell()。setHorizo​​ntalAlignment(PdfPCell.ALIGN_CENTER);
// titleTable.addCell(new Paragraph(new Chunk(img,5,-5)));
document.add(gutter());
titleTable.addCell(new Paragraph(Title,titleFont));
document.add(titleTable);
document.add(gutter());
document.add(gutter());

PdfPTable comTable = new PdfPTable(new float [] {6f,4f,2f,3f,6f,3f});
PdfPCell cell1 = new PdfPCell(new Paragraph(Module,tHeadFont));
cell1.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setPaddingBottom(4);
comTable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph(Name,tHeadFont));
cell2.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setPaddingBottom(4);
comTable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph(Serial,tHeadFont));
cell3.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setPaddingBottom(4);
comTable.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Paragraph(Lesson,tHeadFont));
cell4.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setPaddingBottom(4);
comTable.addCell(cell4);
PdfPCell cell5 = new PdfPCell(new Paragraph(Topic,tHeadFont));
cell5.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setPaddingBottom(4);
comTable.addCell(cell5);
PdfPCell cell6 = new PdfPCell(new Paragraph(No.of Lessons,tHeadFont));
cell6.setHorizo​​ntalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setPaddingBottom(4);
comTable.addCell(cell6);

下面有一些迭代用于显示动态的内容,所以我不知道会有多少页面在那里。
我想在所有页面上重复titleTable。

解决方案

你有不同的选择。



方法#1:定义表格的标题行



您可以使用两个表格,而不是使用单个表并定义一个或多个标题行。在以下问题的答案中对此进行了解释:





您还可以从以下示例中获得灵感:





方法#2:定义页面标题



正如您在StackOverflow上浏览其他问题时所发现的,您可以使用页面事件来创建标题。不到一天前,我在这里解释了基本原则: ITextSharp中的页眉和页脚



有关更多示例,请参阅:





但你真正想要的是问题的答案:





简而言之,您需要这样的页面事件:

 公共类FooterTable扩展了PdfPageEventHelper {
protected PdfPTable footer;
public FooterTable(PdfPTable footer){
this.footer = footer;
}
public void onEndPage(PdfWriter writer,Document document){
footer.writeSelectedRows(0,-1,36,64,writer.getDirectContent());
}
}

但是,而不是 FooterTable ,你应该调用这个类 HeaderTable ,而不是使用硬编码坐标,你应该使用这样的东西:

  float x = document.left(36); 
float y = document.top(10);
footer.writeSelectedRows(0,-1,x,y,writer.getDirectContent());

定义 x 和<$时c $ c> y ,我使用文档对象作为只读对象来获取边距为36的左边坐标和顶部以10为边距进行协调。重要的是要认识到10的边距可能是表空间不足:标题表可能会进入内容表。如果是这种情况,您需要计算标题表的高度并更改文档的上边距,以使标题表和内容表不重叠。



<我有一个问题要问:我们重新设计了iText网站,我们在感恩节发布了它。我们现在注意到,我们没有像以前那样多次访问。鉴于您需要的所有信息都可以在在线文档中找到,并且鉴于您仍然需要提出问题,我们想知道该网站有什么问题。我们可以做些什么来改善内容吗?为了找到问题的答案,我使用了搜索框和标签,例如标题。可能是什么原因导致人们远离我们的网站?我们现在有太多内容吗?


I have a table for title and a table for content. My content table is too long and doesn't fit in a single page. So I want to repeat my title table after each page. How can I achieve this? Please help.

Here is my code

File tempFile = File.createTempFile("progress", "tmp");
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempFile));            
document.setMargins(6, 6, 36, 36);
document.setMarginMirroring(true);
document.open();
PdfPTable titleTable = new PdfPTable(new float[] { 18f });
// Image img = Image.getInstance(uploadFolder + "/logo.jpg");
Font titleFont = new Font(getBaseFont(), 12f, Font.BOLD);
Font tHeadFont = new Font(getBaseFont(), 10f, Font.BOLD);
Font contentFont = new Font(getBaseFont(), 10f);
// img.scalePercent(50);
titleTable.setWidthPercentage(100);
titleTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
titleTable.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// titleTable.addCell(new Paragraph(new Chunk(img, 5, -5)));
document.add(gutter());
titleTable.addCell(new Paragraph("Title", titleFont));         
document.add(titleTable);
document.add(gutter());
document.add(gutter());

PdfPTable comTable = new PdfPTable(new float[] { 6f, 4f, 2f, 3f, 6f, 3f });
PdfPCell cell1 = new PdfPCell(new Paragraph("Module", tHeadFont));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setPaddingBottom(4);
comTable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("Name", tHeadFont));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setPaddingBottom(4);
comTable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("Serial", tHeadFont));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setPaddingBottom(4);
comTable.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Paragraph("Lesson", tHeadFont));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setPaddingBottom(4);
comTable.addCell(cell4);
PdfPCell cell5 = new PdfPCell(new Paragraph("Topic", tHeadFont));
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setPaddingBottom(4);
comTable.addCell(cell5);
PdfPCell cell6 = new PdfPCell(new Paragraph("No.of Lessons", tHeadFont));
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setPaddingBottom(4);
comTable.addCell(cell6);

There are some iterations below for displaying content which is dynamic so I can't tell how many pages will be there. I want titleTable to be repeated on all pages.

解决方案

You have different options.

Approach #1: define a header row for a table

Instead of using two tables, you could use a single table and define one or more header rows. This is explained in the answer to the following questions:

You can also get some inspiration from the following examples:

Approach #2: define a header for the page

As you may have discovered when browsing other questions on StackOverflow, you can use page events to create a header. Less than a day ago, I explained the basic principle here: Header and Footer in ITextSharp

For more examples, see:

But what you really want is the answer to the questions:

In short, you need a page event like this:

public class FooterTable extends PdfPageEventHelper {
    protected PdfPTable footer;
    public FooterTable(PdfPTable footer) {
        this.footer = footer;
    }
    public void onEndPage(PdfWriter writer, Document document) {
        footer.writeSelectedRows(0, -1, 36, 64, writer.getDirectContent());
    }
}

However, instead of FooterTable, you should call this class HeaderTable, and instead of using hard coded coordinates, you should use something like this:

float x = document.left(36);
float y = document.top(10);
footer.writeSelectedRows(0, -1, x, y, writer.getDirectContent());

When defining the x and the y, I used the document object as a read-only object to get the left coordinate with a margin of 36 and the top coordinate with a margin of 50. It is important to realize that a margin of 10 may be insufficient space for the table: the header table might run into the content table. If that is the case, you need to calculate the height of the header table and change the top margin of the document so that the header table and content table don't overlap.

I have one question for you: we redesigned the iText web site and we released it on Thanksgiving. We now notice that we don't get as many visits as we used to. Given the fact that all the information you needed can be found on the online documentation and given the fact that you still needed to ask the question, we'd like to know what is wrong with the web site. Is there something we can do to improve the content? To find the answers to your questions, I used the search box and tags such as header. What could be the reason that drives people away from our web site? Do we have too much content now?

这篇关于如何在所有页面上重复标题 - iText pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆