Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue May 18, 2021 6:53 am

Hello, I want to know how custom page size pdf file in c# .net.

thank.

jame_piyawat2
 
Posts: 10
Joined: Fri May 07, 2021 4:36 am

Tue May 18, 2021 9:26 am

Thanks for your inquiry.
Please refer to the following codes.

(1) Set page size when adding a new page.

Code: Select all
            PdfDocument pdf = new PdfDocument(input);
            //You can pass in a static variable of common sizes in PdfPageSize
            PdfPageBase pdfPageBase1 = pdf.Pages.Add(PdfPageSize.A5);
            //Or pass in a custom SizeF object.
            PdfPageBase pdfPageBase2 = pdf.Pages.Add(new SizeF(width,height));
            pdf.SaveToFile("output.pdf", FileFormat.PDF);


(2) Reset the page size of existing pages.

Code: Select all
            PdfDocument originalDoc = new PdfDocument();
            originalDoc.LoadFromFile(input);
            //Set the margins
            PdfMargins margins = new PdfMargins(0);
            using (PdfDocument newDoc = new PdfDocument())
            {
                float scale = 0.8f;
                for (int i = 0; i < originalDoc.Pages.Count; i++)
                {
                    PdfPageBase page = originalDoc.Pages[i];
                    //Use same scale to set width and height
                    float width = page.Size.Width * scale;
                    float height = page.Size.Height * scale;
                    //Add new page with expected width and height
                    PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height), margins);
                    newPage.Canvas.ScaleTransform(scale, scale);
                    //Copy content of original page into new page
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
                }
                newDoc.SaveToFile(output);
            }


If you have any other questions, please feel free to contact us.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue May 18, 2021 9:59 am

Andy.Zhou wrote:Thanks for your inquiry.
Please refer to the following codes.

(1) Set page size when adding a new page.

Code: Select all
            PdfDocument pdf = new PdfDocument(input);
            //You can pass in a static variable of common sizes in PdfPageSize
            PdfPageBase pdfPageBase1 = pdf.Pages.Add(PdfPageSize.A5);
            //Or pass in a custom SizeF object.
            PdfPageBase pdfPageBase2 = pdf.Pages.Add(new SizeF(width,height));
            pdf.SaveToFile("output.pdf", FileFormat.PDF);


(2) Reset the page size of existing pages.

Code: Select all
            PdfDocument originalDoc = new PdfDocument();
            originalDoc.LoadFromFile(input);
            //Set the margins
            PdfMargins margins = new PdfMargins(0);
            using (PdfDocument newDoc = new PdfDocument())
            {
                float scale = 0.8f;
                for (int i = 0; i < originalDoc.Pages.Count; i++)
                {
                    PdfPageBase page = originalDoc.Pages[i];
                    //Use same scale to set width and height
                    float width = page.Size.Width * scale;
                    float height = page.Size.Height * scale;
                    //Add new page with expected width and height
                    PdfPageBase newPage = newDoc.Pages.Add(new SizeF(width, height), margins);
                    newPage.Canvas.ScaleTransform(scale, scale);
                    //Copy content of original page into new page
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
                }
                newDoc.SaveToFile(output);
            }


If you have any other questions, please feel free to contact us.



thank you. Now I'm can set pang But I don't know how many millimeters is 1 f ?

jame_piyawat2
 
Posts: 10
Joined: Fri May 07, 2021 4:36 am

Tue May 18, 2021 10:16 am

Hi,

Thanks for your response.

The default page size unit in our product is point. You can refer to the following code to convert the unit from millimeter to point.
Code: Select all
            PdfUnitConvertor convertor = new PdfUnitConvertor();
            float pointValue=convertor.ConvertUnits(millimeterValue, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point);


If you have any other questions, please feel free to contact us.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue May 18, 2021 4:44 pm

Andy.Zhou wrote:Hi,

Thanks for your response.

The default page size unit in our product is point. You can refer to the following code to convert the unit from millimeter to point.
Code: Select all
            PdfUnitConvertor convertor = new PdfUnitConvertor();
            float pointValue=convertor.ConvertUnits(millimeterValue, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point);


If you have any other questions, please feel free to contact us.


Thank you. for Reply :D :D

jame_piyawat2
 
Posts: 10
Joined: Fri May 07, 2021 4:36 am

Wed May 19, 2021 5:33 am

You are welcome!
Have a nice day! :D
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.PDF