Pdf file with pages of different size

Hi,

ist it possible to create a Pdf with pages of different size? I need it to add images in the original size on separate pages.

I can change the sie of all pages in a Pdf document like this:

                    var document = new PdfDocument();
                    //make the page bigger - needed for images
                    document.PageSettings.Width = ImageQuality.PdfWidth;
                    document.PageSettings.Height = ImageQuality.PdfHigh;

Unfortunately it is not possible to set the size on a page like this:

                            PdfPage page = document.Pages.Add();
                            page.Size = new Syncfusion.Drawing.SizeF(1000, 2000);

Generally it is possible to create Pdf file with pages of different size. 

Thanks
Marian





11 Replies

KC Karthikeyan Chandrasekar Syncfusion Team September 6, 2018 12:19 PM UTC

Hi Marian, 
Thank you for your interest in Syncfusion products. We can create different sized pages in PDF document by using the below code snippet. 
//Create a new PDF document. 
PdfDocument document = new PdfDocument(); 
 
// Page with A5 size 
PdfSection section1 = document.Sections.Add(); 
section1.PageSettings.Size = PdfPageSize.A5; 
PdfPage page1 = section1.Pages.Add(); 
 
// Page with A3 size 
PdfSection section2 = document.Sections.Add(); 
section2.PageSettings.Size = PdfPageSize.A3; 
PdfPage page2 = section2.Pages.Add(); 
             
//Save the document. 
document.Save(memoryStream); 
document.Close(true); 
 

Regards, 
Karthikeyan 



MG Marian Grzesik September 6, 2018 01:22 PM UTC

Hi,

thank a lot for your help. It works fine for Android and iOS. I've found only one small issue, at the end of a document with many sections. The last section has the right width, but the high is sometimes too big. Sometimes it works without the bug. Strange.

Here is my code:

                var outData = new List<BillSendItem>();
                //Bills with many pages add to one Pdf document

                //Create a new PDF document.
                PdfDocument document = null;
                int docCnt = 1;
                var billGroups = Images.GroupBy(x => x.GroupNumber);
                foreach (var group in billGroups)
                {
                    document = new PdfDocument();
                    document.PageSettings.Margins.All = 0;
                    foreach (var item in group)
                    {
                        if (item.ImageType == FileTypeDef.Pdf)
                        {
                            // Merges PDFDocument
                            PdfDocument.Merge(document, item.ImageData);
                        }
                        else if (item.SelectorType == BillImageTypeDef.Image) // for all images
                        {
                            //Load the image from the image data.
                            PdfBitmap image = new PdfBitmap(new MemoryStream(item.ImageData));

                            var section = document.Sections.Add();
                            section.PageSettings.Size = new Syncfusion.Drawing.SizeF(image.Width, image.Height);

                            //Add a page to the document.
                            PdfPage page = section.Pages.Add();
                            //Create PDF graphics for the page.
                            PdfGraphics graphics = page.Graphics;
                            //Draw the image
                            graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                        }
                    }

                    MemoryStream stream = new MemoryStream();
                    // Saves the document as a stream
                    document.Save(stream);
                    document.Close(true);
                    outData.Add(new BillSendItem { Name = GetFileName("", true, docCnt++), Data = stream.ToArray() });
                    stream.Dispose();
                }

Thanks
Marian


KC Karthikeyan Chandrasekar Syncfusion Team September 7, 2018 12:36 PM UTC

Hi Marian, 
I suspect this issue occurs for a specific Image, can you share the image which reproduces the issue. So that I will look into it further and provide you a solution. 

Regards, 
Karthikeyan  



MG Marian Grzesik September 13, 2018 12:44 PM UTC

Hi,

the second image has some white area, what is wrong.

Thanks Marian

Attachment: Dokumente_13092018_bc60490.zip


KC Karthikeyan Chandrasekar Syncfusion Team September 14, 2018 07:26 AM UTC

Hi Marian, 
This issue occurs as the page orientation is not set to Landscape if the page width is greater than page height. Please use the below code snippet and check if your issue resolves. 
section.PageSettings.Size = new Syncfusion.Drawing.SizeF(image.Width, image.Height); 
 
if (section.PageSettings.Size.Height < section.PageSettings.Size.Width) 
{ 
    section.PageSettings.Orientation = PdfPageOrientation.Landscape; 
} 
 
Regards, 
Karthikeyan 



MG Marian Grzesik September 14, 2018 11:32 AM UTC

Hi,

unfortunatelly the change of the orientation doesn't change anything. The bug is still present. I think, the API shouldn't differentiate between the orientation and the height / width of the image. If I set the page size and set an image of the same size, the page should just fill the content with the image and not change the size of the page. 

Thanks
Marian



KC Karthikeyan Chandrasekar Syncfusion Team September 17, 2018 10:42 AM UTC

Hi Marian, 
I have modified the code snippet to achieve your requirement, please use the below code snippet and let me know if you need any further assistance. 
PdfDocument document = new PdfDocument(); 
 
PdfBitmap img = new PdfBitmap(new MemoryStream(item.ImageData)); 
document.PageSettings.Margins.All = 0; 
 
if (img.Width > img.Height) 
{ 
    document.PageSettings.Orientation = PdfPageOrientation.Landscape; 
} 
else 
{ 
    document.PageSettings.Orientation = PdfPageOrientation.Portrait; 
} 
 
PdfSection section = document.Sections.Add(); 
section.PageSettings.Size = new System.Drawing.SizeF(img.Width, img.Height); 
 
PdfPage page = section.Pages.Add(); 
page.Graphics.DrawImage(img, new System.Drawing.PointF(), new System.Drawing.SizeF(img.Width, img.Height)); 
 
MemoryStream stream = new MemoryStream(); 
document.Save(stream); 
document.Close(true); 

Regards, 
Karthikeyan 



MG Marian Grzesik September 17, 2018 12:48 PM UTC

Hi,

I think unfortunatelly that it doesn't change anything. The problem is, that you assume that all images have with > height or vice versa. It is not so. Every image can have quite different size.

Thanks
Marian


KC Karthikeyan Chandrasekar Syncfusion Team September 18, 2018 10:56 AM UTC

Hi Marian, 
Thanks for your update, for converting Image to PDF we have 3 possible options in the dimension of the input images. 
  1. Height > Width
  2. Width > Height
  3. Height = Width
We have included code for handling all the cases, based on the input dimension the PDF page size will be automatically change. A simple working sample is attached below for your reference. 
Regards, 
Karthikeyan 



MG Marian Grzesik September 20, 2018 06:12 PM UTC

Hi,

it seems to work now! 

Thanks!
Marian



KC Karthikeyan Chandrasekar Syncfusion Team September 21, 2018 11:24 AM UTC

Hi Marian, 
We are happy to know that your requirement is fulfilled, revert us back if you need any further assistance in future. 

Regards, 
Karthikeyan 



Loader.
Up arrow icon