简体   繁体   English

PDF iTextSharp,将PDF行写入其他页面

[英]PDF iTextSharp, write PDF line to different page

I'm working on a previous employee's buggy code (How I love working on other people's code), ad he's creating a pdf using iTextSharp ASP framework 4.0, c# 我正在处理前雇员的错误代码(我喜欢在其他人的代码上工作),并且他正在使用iTextSharp ASP框架4.0,c#创建pdf

He's using: 他正在使用:

if (yindex <= 100)
{
Cb.PdfDocument.NewPage();

Cb.SetCMYKColorStroke(100, 60, 0, 18);
yindex = 770;
}
Cb.BeginText();
Cb.SetFontAndSize(Font, FontSize);
Cb.ShowTextAligned(Alignment, text, xPos, yindex, rotation);
Cb.EndText();

Basically what is happening is hes printing a list of times, keeping a running total of their amounts, once the section is completed, he trying to set the yindex back to the top of the page and write a header whit a total. 基本上发生的事情是,他打印了一个时间列表,并保持它们总量的连续运行,一旦完成该部分,他便试图将yindex设置回页面顶部并写一个总计的标题。

This works fine if there is one page, however when a section spans multiple pages, setting the yindex to the top of the page sets the text to the top of the current page. 如果只有一页,则效果很好,但是当一个部分跨越多页时,将yindex设置为页面顶部会将文本设置为当前页面顶部。 How would I keep track of what page the header should be on, and then set the yindex to correct height on the correct page? 如何跟踪标题应该在哪个页面上,然后将yindex设置为正确页面上的正确高度?

I would throw away the code that writes the header along with the rest of the data, and introduce a page event that adds the header in the OnEndPage() event. 我将丢弃将标头与其余数据一起写入的代码,并引入一个页面事件,该事件将标头添加到OnEndPage()事件中。

To deal with the fact that you only know the total when page X is reached whereas you may need that total on pages X - 1, X -2,... I would use a PdfTemplate (as mkl suggests). 要处理这样一个事实,即只有到达页面X时您才知道总数,而您可能需要在页面X-1,X -2,...上总计,我将使用PdfTemplate (如mkl所建议)。

This is demonstrated in examples such as MovieCountries1 (see http://tinyurl.com/itextsharpIIA2C04 for the C# counterpart). 在例如MovieCountries1示例中MovieCountries1进行了MovieCountries1 (有关C#的示例,请参见http://tinyurl.com/itextsharpIIA2C04 )。

Using a page event has the benefit that you don't need to keep track of the pages, you can keep the code that draws the header separate from the rest of the business logic. 使用页面事件的好处是您不必跟踪页面,可以将绘制标头的代码与其余业务逻辑分开。 See http://itextpdf.com/sandbox/events/ for some more examples. 有关更多示例,请参见http://itextpdf.com/sandbox/events/

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

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