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
close icon

PDF custom handler. Image size equals to Page size

Hi.
I have a "Custom handler" which returns a pdf. In the pdf I import an image. I want the page size is the size of the page.

This is the source code:

public void ProcessRequest(HttpContext context)
        {
            PdfDocument doc = new PdfDocument();
            doc.ViewerPreferences.PageMode = PdfPageMode.FullScreen;

            //read image
            PdfBitmap image = new PdfBitmap("C:\\Users\\ccarranza\\Pictures\\Sin título.png");
            //ImageSize
            System.Drawing.SizeF size = new System.Drawing.SizeF(image.Width, image.Height);
            System.Drawing.PointF location = new System.Drawing.PointF(0, 0);
            System.Drawing.RectangleF rect = new System.Drawing.RectangleF(location, size);

            PdfSection section = doc.Sections.Add();
            section.PageSettings.SetMargins(0);
            //Page Size
            section.PageSettings.Width = image.Width;
            section.PageSettings.Height = image.Height;
            PdfPage page = section.Pages.Add();            
            
As I can insert an image that fills the entire page created?
            
            PdfGraphics g = page.Graphics;
            PdfLayoutFormat format = new PdfLayoutFormat();
            format.Layout = PdfLayoutType.Paginate;
            
            image.Draw(page, rect, format);

            doc.Save("Sample.pdf", context.Response, HttpReadType.Open);

        }

But when you open the PDF page size is larger than the image inserted.

Attached PDF output on screen

Thank you very much for your help







Attachment: test_198a9383.zip

1 Reply

CM Chinnu Muniyappan Syncfusion Team April 14, 2016 09:10 AM UTC

Hi Carlos,

Thank you for contacting Syncfusion support.

We have created a sample for your requirements, could you please refer the below code snippet and sample for more details.
Convert the image width and height to points using PdfUnitConvertor and set it to the page size.

            //Create PDF unit converter

            PdfUnitConvertor converter = new PdfUnitConvertor();


            //Covert pixel to point

            float width = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point);

            float height = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point);


            //Add a section

            PdfSection section = doc.Sections.Add();


            //Set margin

            section.PageSettings.SetMargins(0);


            //Page Size

            section.PageSettings.Width = width;

            section.PageSettings.Height = height;


            //Add new page

            PdfPage page = section.Pages.Add();


Sample link:
http://www.syncfusion.com/downloads/support/forum/123718/ze/WebApplication-598568724

Please let us know if you have any concern.

Regards,
Chinnu

Loader.
Live Chat Icon For mobile
Up arrow icon