PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

PdfSharp: adding "page x of total page" to each page of
http://forum.pdfsharp.de/viewtopic.php?f=2&t=1513
Page 1 of 1

Author:  arzdnz1 [ Tue Jan 18, 2011 6:18 pm ]
Post subject:  PdfSharp: adding "page x of total page" to each page of

Hi, Thomas, Stefan,

I'm generating a pdf document using VB.Net and pdfSharp. The document printed has a variable number of pages.

On each page I want to DrawString that says "pg.x of n ", where x represents current page number and n represents total pages. Tracking the current page is trivial.

My problem is discovering the total number of pages before I commence generating the pdf document.

My initial solution was to generate the .pdf twice, 1st to determine the total page count, and then generate the pdf again with the total page count now known. However, pdfSharp seems to throw memory error whenever it is called twice on the same execution. 'Dispose' and 'Close' of my PdfDocument object didn't help. Also, reveiw of Henrylar's thread during August 2010 of this forum didn't help.

Any help would be appreciated. thanks....az

Author:  Thomas Hoevel [ Wed Jan 19, 2011 9:04 am ]
Post subject:  Re: PdfSharp: adding "page x of total page" to each page of

Hi!

Create the pages, keep a reference to each PdfPage - and when the document is completed, add "Page x of y" to each of the pages.
Then save the document.

A single run, a single save.

Author:  rasepretrep [ Wed Jan 19, 2011 9:13 am ]
Post subject:  Re: PdfSharp: adding "page x of total page" to each page of

Hmm I dont understand, why would you need to create it twice anyway?

just add the page counter shortly before saving the PdfDocument.
Also having memory errors on second open makes me suggest you should probably check if you are working with using {} statements or dispose() correctly

Author:  arzdnz1 [ Fri Jan 21, 2011 7:52 am ]
Post subject:  Re: PdfSharp: adding "page x of total page" to each page of

ok, here's the code....

Dim document As PdfDocument = New PdfDocument()
'Reset HasMorePages to True so loop will start over again.
HasMorePages = True
While (HasMorePages)
Call PrintDocument(document)
End While

What I originally wanted to do was:
TotalPageCounter = document.PageCount
document.Dispose()

and then recreate document with the "pg.x of n" on each page, but a memory error throws. Thomas' method is much more efficient, but how do you reference back to each page in document, and then basically edit each page with the correct "pg. x of n"?

Author:  Thomas Hoevel [ Mon Jan 24, 2011 9:32 am ]
Post subject:  Re: PdfSharp: adding "page x of total page" to each page of

arzdnz1 wrote:
but how do you reference back to each page in document

In C# you can use "List<PdfPage>" to store all references (I don't know the VB syntax).

You can still add content to page 1, even after adding more pages.

Author:  arzdnz1 [ Tue Jan 25, 2011 5:00 am ]
Post subject:  Re: PdfSharp: adding "page x of total page" to each page of

thanks...any code examples on referencing would be greatly appreciated. (prefereably VB, but C# would also be appreciated)

Also...after referencing each pdf page, a new gfx object can simply be added to that page, using x1,y1 coordinates on the Drawstring method?

thanks .....az

Author:  arzdnz1 [ Tue Jan 25, 2011 6:15 pm ]
Post subject:  Re: PdfSharp: adding "page x of total page" to each page of

ok...got it thanks...

All you have to do is:

Dim modPage As PdfPage

where document was pdfDocument previously created. "Add pg x of n" to each page with new gfx object looping

I = 0 to document.PageCount

modPage=document.Pages(I)


then use gfx to draw on page "add pg.x of n"


Thomas, if you ever come to Hawaii look me up and I'll buy you a beer.

thanks...az

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/