Hello, I want to know how custom page size pdf file in c# .net.
thank.
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); 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);
}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.
PdfUnitConvertor convertor = new PdfUnitConvertor();
float pointValue=convertor.ConvertUnits(millimeterValue, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point);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.