views:

152

answers:

1

i am generating a report in vb.net using itextsharp. sometimes the entire report does not fit on one page and i need to know when itextsharp is adding a page to the document. is there a way to detect this?

+3  A: 

As long as you're implementing the PdfPageEvent interface, you just need to override the public void onEndPage(PdfWriter writer, Document document) method, which is called right before a new page is started.

Edit: here's some code explaining the procedure, without knowing what you want to do if a new page is created by iTextSharp, this is the most I can give you:

Public Class YourReport
  Implements PdfPageEvent

  'Your report code

  Public Overrides Sub onEndPage(ByVal writer as PdfWriter, 
                                       ByVal doc as Document)
    'if you get here, a new page was created by iTextSharp 
    'so do what you need to do.
  End Sub

End Class
jball
jball how do i do that?
I__
Overrides Sub onEndPage(ByVal writer as PdfWriter, ByVal doc as Document)
jball
so i just put that line into the sub and it will not generate a new page?
I__
No, it will still generate a new page, but you will detect it in the `onEndPage` and you can then react accordingly.
jball
Just to clarify, you are implementing the `PdfPageEvent` interface at the top of your class with something like `Public Class YourReport Implements PdfPageEvent`, correct?
jball
im sorry im a beginner, can you plainly tell me how to check if i am adding a new page, could u paste some code please in your answer
I__
thank you very much!!!!
I__
hey jball, how do i detect if onendpage sub was triggered?
I__
by the way, the string onendpage was not found in the entire solution
I__
You need to add the `onEndPage` to your class. It's part of the iTextSharp library's `PdfPageEvent` interface, so you won't find it by a string search unless you've added it.
jball
You should put your code in the `onEndPage` override that you create. It will get hit when iTextSharp finishes writing a page.
jball
thank you very much. i am learning a lot. the thing is i dont have a class it's just a sub, and i dont think i am able to put a sub within a sub
I__
Error 2 Statement cannot appear within a method body. End of method assumed.
I__
Error 3 sub 'onEndPage' cannot be declared 'Overrides' because it does not override a sub in a base class.
I__
You should create a class just to handle your report.
jball
i copied and pasted exactly what you told me and now im getting these errors: Error 2 Type 'PdfPageEvent' is not defined.
I__
Error 3 sub 'onEndPage' cannot be declared 'Overrides' because it does not override a sub in a base class.
I__
You've included the iTextSharp dll in your project references?
jball
yes i sure have and am referncing it no problem
I__
Then you should be able to create a class and include the code I wrote in the answer without getting an error. `PdfPageEvent` is an interface provided by the iTextSharp library, so if you're referencing the dll no problem, you'll be able to implement that interface.
jball
by the way this doesnt work either Implements iTextSharp.text.pdf.IPdfPageEvent
I__
please see this screenshot http://img704.imageshack.us/img704/7882/fullscreencapture122120.png
I__
please help!!!!!!
I__
Ensure that you have the latest version of iTextSharp http://sourceforge.net/projects/itextsharp/files/ and read the overview of page events http://itextsharp.sourceforge.net/tutorial/ch12.html - beyond that, you may have some problem in your environment that's causing the iTextSharp dll to not be included properly.
jball
it is included properly because i am using it all over the place
I__
jball any help please
I__
I've given you all the advice that I have, so you should post another question about not being able to implement an interface provided by iTextSharp in VB.net. Perhaps someone else will be able to solve that for you. Once you have the interface implemented, then my answer above should work for you.
jball