Domanda

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#

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.

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. 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?

È stato utile?

Soluzione

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.

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).

This is demonstrated in examples such as MovieCountries1 (see http://tinyurl.com/itextsharpIIA2C04 for the C# counterpart).

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top