PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 05, 2024 1:25 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Tue Jan 18, 2011 6:18 pm 
Offline

Joined: Tue Jun 15, 2010 2:58 am
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 19, 2011 9:04 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 19, 2011 9:13 am 
Offline

Joined: Tue Feb 23, 2010 12:13 pm
Posts: 7
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


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 21, 2011 7:52 am 
Offline

Joined: Tue Jun 15, 2010 2:58 am
Posts: 6
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"?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 24, 2011 9:32 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 25, 2011 5:00 am 
Offline

Joined: Tue Jun 15, 2010 2:58 am
Posts: 6
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 25, 2011 6:15 pm 
Offline

Joined: Tue Jun 15, 2010 2:58 am
Posts: 6
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 68 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group