We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add headers to existing pages?

I use PdfTextElement and PdfLayoutResult to create the content of my pages, however I'd like to add headers at the top of the pages, is there a way to catch an event and move the content of the page just a lit lower (and moving content that doesn't fit anymore to the next page) so a header could be added?

10 Replies

MH Mateusz Haex March 2, 2016 10:29 AM UTC

I found the solution! :)

I simply add an event to the PdfTextElement (BeginPageLayout) and in the method that's called by the event I simply request the page's content to start at 0,30 (X,Y).

PdfTextElement element = PdfTextElement("Some text", someFont);
element.BeginPageLayout += new BeginPageLayoutEventHandler(BeginPageLayoutMethod);
PdfLayoutResult result = element.Draw(page, new RectangleF(x, y, width, page.GetClientSize().Height), layoutFormat);

private void BeginPageLayoutMethod(object sender, BeginPageLayoutEventArgs e)
{
  RectangleF bounds = e.bounds;
  if (!e.Page.Equals(_page))
  {
    bounds.Y = 30f;
  }
  e.Bounds = bounds;
}


CM Chinnu Muniyappan Syncfusion Team March 3, 2016 06:39 AM UTC

Hi Mateusz,
We are glad to know that your issue has been resolved. Please let us know if you need further assistance.
Regards,
Chinnu


MH Mateusz Haex March 11, 2016 12:34 PM UTC

I noticed that when text doesn't fit on the page anymore it goes to the next page as is expected, however the same does not apply to PdfImage. How can I fix this? How can I move the entire image (which is only 100x100px) to the next page?


MH Mateusz Haex March 11, 2016 12:50 PM UTC

What I mean is there a similar object like PdfTextElement for images? Because with PdfTextElement I can use a PdfLayoutFormat and set that to PdfLayoutType.Paginate and PdfLayoutBreakType.FitPage. Is there a way to use a layoutformat for images as well?


MH Mateusz Haex March 11, 2016 01:12 PM UTC

Nevermind, got it:

PdfImage image = new PdfBitmap(stream);
image.Draw(page, new RectangleF(x, y, 100, 100), layoutFormat);


MH Mateusz Haex March 11, 2016 02:51 PM UTC

Turns out that the following line:

image.Draw(page, new RectangleF(x, y, 100, 100), layoutFormat);
Only draws a part of the image that will fit into the rectangle with a width and height of 100.
Can I somehow let the image scale so it fits within the rectangle?




CM Chinnu Muniyappan Syncfusion Team March 14, 2016 11:56 AM UTC

Hi Mateusz,
We are suggesting to you to use Page.Graphics.DrawImage method to draw the image within the given bounds, use  Image.Draw to paginate the large image from one page to another page, we have created a simple sample for your reference, please refer the below code snippet and sample.
Code Snippet:
PdfBitmap img = new PdfBitmap(pngImage);

page.Graphics.DrawImage(img, new RectangleF(10, 10, 100, 100)); 

Sample link:
http://www.syncfusion.com/downloads/support/forum/123265/ze/Sample11972287588

Regards,
Chinnu


MH Mateusz Haex March 15, 2016 08:29 AM UTC

That's what I used before, but "page.graphics.DrawImage" (1) does not support a layoutformat, so I replaced it with "PdfBitmap.Draw" (2) which does paginate the image, but I also need to scale it like the first one does. So I rather use the second one because that one paginates, but I also need the image to be smaller. Isn't there a way to do both?


MH Mateusz Haex March 15, 2016 01:05 PM UTC

So I've done what you suggested and used page.Graphics.DrawImage instead and additionally wrote some code to create a new page and new y-location myself instead of the automatic pagination by image.Draw to compensate for the lack of that feature in page.Graphics.DrawImage.


CM Chinnu Muniyappan Syncfusion Team March 16, 2016 12:11 PM UTC

Hi Mateusz,
We are glad to know that your issue has been resolved. Please let us know if you need further assistance.
Regards,
Chinnu

Loader.
Up arrow icon