supriya khamesra
supriya khamesra

Reputation: 143

How to identify end of page is reached in pdf file using itextsharp

Hi I am using itextsharp to generate a pdf file.I am placing a backgound image on it and want that image on all the pages .But when the first page is completed the text move to next page automatically so the image is not appearing on the new page.

Is there a way to identify the end of page so that we can add a new page and then set the image first so will appear in background and then can add the remaining text.

All is i want a image in background on all the pages of pdf file.

Upvotes: 3

Views: 9248

Answers (2)

Mark Storer
Mark Storer

Reputation: 15868

I suggest you use a page event:

myWriter.setPageEvent(new BackgroundPageEvent(backgroundImage));

class BackgroundPageEvent extends PdfPageEventHelper {
  Image backgroundImage = null;
  public BackgroundPageEvent( Image img ) {
    backgroundImage = img;
  }
  public void onStartPage(PdfWriter writer, Document doc) {
    PdfContentByte underContent = writer.getDirectContentUnder();
    underContent.addImage(backgroundImage);
  }
}

With the above code, backgroundImage will be added to the "under content" as each page is created. No need to worry about when to add it yourself... iText will figure that out for you, and the first thing in the underContent of each page will be your image. You might need to play around with the various overrides of addImage to get the size you want.

I believe you can also query doc for the current page size if it varies in your program. If not, you should be able to create the image you pass in with an absolute position/scale (which may be what you're doing already).


PdfPageEvent has a number of other events you can override. PdfPageEventHelper covers all the bases with "no ops" so you can just override the event[s] you want:

  • OnStartPage
  • OnEndPage
  • OnCloseDocument
  • OnParagraph
  • OnParagraphEnd
  • OnChapter
  • OnChapterEnd
  • OnSection
  • OnSectionEnd
  • OnGenericTag

Generic tag is actually Really Handy. You can give a generic tag (a string) to just about anything within your document, and your OnGenericTag override will be called with the rect that was used to draw whatever it was you tagged. All kinds of spiffy possibilities.

Upvotes: 4

as-cii
as-cii

Reputation: 13019

Just check PdfWriter.PageNumber property like this:

        using (FileStream fs = File.Create("test.pdf"))
        {
            Document document = new Document(PageSize.A4, 72, 72, 72, 72);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);

            document.Open();
            int pageNumber = -1;

            for (int i = 0; i < 20; i++)
            {
                if (pageNumber != writer.PageNumber)
                {
                    // Add image
                    pageNumber = writer.PageNumber;
                }

                // Add something else
            }

            document.Close();
        }

Upvotes: 1

Related Questions