简体   繁体   English

itextsharp:如何使用itextsharp在PDF中生成带有动态标题的报告?

[英]itextsharp: How to generate a report with dynamic header in PDF using itextsharp?

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. 我正在使用itextsharp生成PDF报告,该报告的标题包含客户的信息,该报告的主体中包含客户交易的列表。

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?" 您的问题可能会遭到拒绝投票或至少在“您尝试了什么?”的意义上发表评论。

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. 但是,我为您编写了一个Java小示例,您可以轻松地适应C#,因为iText和iTextSharp或多或少共享相同的语法。

The example is called VariableHeader and these are the most interesting snippets: 该示例称为VariableHeader ,这些是最有趣的代码段:

First I create a custom implementation of the PdfPageEvent interface (using PdfPageEventHelper ). 首先,我创建了PdfPageEvent接口的自定义实现(使用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. 重要的是要了解您不能使用onStartPage()方法(出于我的书中所述的原因),而应使用onEndPage()方法。

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);
    }
}

As you can see, the text of the header is stored in a variable that can be changed using the setHeader() method we created. 如您所见,标头的文本存储在一个变量中,可以使用我们创建的setHeader()方法对其进行更改。

The event is declared to the PdfWriter like this: 像这样向PdfWriter声明事件:

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

I change the header Phrase before invoking the newPage() method: 我在调用newPage()方法之前更改标题Phrase

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

In my simple example, I generate a document that lists the factors of all the numbers from 2 to 300: variable_header.pdf . 在我的简单示例中,我生成了一个文档,其中列出了2到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. 每页的标题显示“ X的因素”,其中X是该页面上显示的因素数。

You can easily adapt this to show different customer names instead of numbers. 您可以轻松地对此进行调整,以显示不同的客户名称而不是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM