iTextSharp的:如何生成使用iTextSharp的PDF格式的动态标题的报告? [英] itextsharp: How to generate a report with dynamic header in PDF using itextsharp?

查看:852
本文介绍了iTextSharp的:如何生成使用iTextSharp的PDF格式的动态标题的报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成与iTextSharp的一个PDF格式的报告,该报告的标题中有客户的信息,
在报告正文中包含客户交易清单。

I'm generating a PDF report with itextsharp, the header of the report has the information of the Customer, In the body of the report contains a list of customer transactions.

我的疑问是:如何动态为每个客户产生一个头?

My doubt is: How to generate a header dynamically for each client?

我需要一个例子来动态地生成头报告。
每一个新的页面标题需要有数据客户端时,改变的客户端头应包含新的客户端上的信息。

I need an example to generate a header dynamically to the report. Every new page Header need to have the data that client when changing client header should contain information on the new client.

推荐答案

您的问题的风险下投票或在意义上至少评论你尝试过什么?

Your question risks down-voting or at least comments in the sense of "What have you tried?"

不过,我写你的Java一个小例子您可以轻松地适应C#作为的iText和iTextSharp的份额或多或少相同的语法。

However, I've written you a small example in Java which you can easily adapt to C# as iText and iTextSharp share more or less the same syntax.

这个例子是所谓的 VariableHeader ,这些都是最有趣的片段:

The example is called VariableHeader and these are the most interesting snippets:

首先,我创建 PdfPageEvent 接口的自定义实现(使用 PdfPageEventHelper )。要明白,你不能使用的OnStartPage()方法(在我的书说明的原因),使用的OnEndPage()方法,而不是

First I create a custom implementation of the PdfPageEvent interface (using PdfPageEventHelper). It's important to understand that you can't use the onStartPage() method (for reasons described in my book), use the onEndPage() method instead.

public class Header extends PdfPageEventHelper {

    protected Phrase header;

    public void setHeader(Phrase header) {
        this.header = header;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContentUnder();
        ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, header, 559, 806, 0);
    }
}



正如你所看到的,标题的文本是存储在可使用我们创建的的setHeader()方法来改变一个变量。

As you can see, the text of the header is stored in a variable that can be changed using the setHeader() method we created.

该事件被宣布在 PdfWriter 是这样的:

The event is declared to the PdfWriter like this:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
Header event = new Header();
writer.setPageEvent(event);



我改变标题短语调用之前 NEWPAGE()方法:

event.setHeader(new Phrase(String.format("THE FACTORS OF %s", i)));
document.newPage();

在我简单的例子,我产生了一份文件,列出了所有的数字的因素,从2到300 : variable_header.pdf 。每个页面的标题说,其中的 X 的是,其中显示该网页上的因素的数量。

In my simple example, I generate a document that lists the factors of all the numbers from 2 to 300: variable_header.pdf. The header of each page says "THE FACTORS OF X" where X is the number of which the factors are shown on that page.

您可以轻松适应它来显示不同​​的客户名称而不是数字。

You can easily adapt this to show different customer names instead of numbers.

这篇关于iTextSharp的:如何生成使用iTextSharp的PDF格式的动态标题的报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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