Table of Contents
- Getting Started
- EO.Pdf
- Overview
- Installation and Deployment
- Using HTML to PDF
- Using HTML to PDF
- Quick Start
- Setting Page Size and Margins
- Resizing Output
- Output Paging
- Web Page Authentication
- HTTP Post and Headers
- JavaScript in HTML
- Conversion Trigger
- Links in HTML
- Page Header and Footer
- Partial Page Conversion
- Repeating Table Header and Footer
- Merging Multiple HTML pages
- Generating Page Images
- Using with PDF Creator
- Creating Interactive Form Elements
- Working with Secure Pages
- Using HtmlToPdfSession object
- Troubleshooting HTML to PDF
- Debugging HTML to PDF
- Using PDF Creator
- Working with Existing PDF Files
- Using in Web Application
- Advanced Topics
- EO.Web
- EO.WebBrowser
- EO.Wpf
- Common Topics
- Reference
Setting Page Size and Margin |
Setting Page Size
To configure output page size, set HtmlToPdfOptions's PageSize:
//Set output page size to 8.5 by 11 inches EO.Pdf.HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f); //Convert c:\test.html to c:\result.pdf EO.Pdf.HtmlToPdf.ConvertUrl("c:\\test.html", "c:\\result.pdf");
You can also use one of the pre-defined page sizes:
//Set output page size to A5
EO.Pdf.HtmlToPdf.Options.PageSize = EO.Pdf.PdfPageSizes.A5;
The default page size is letter size.
Setting Page Margins
Page margins are indirectly set through HtmlToPdfOptions's OutputArea. This property defines the "output window" on a page, which excludes page margins. The default OutputArea has the following values on a letter size paper:
Left 1 inch Top 1 inch Width 6.5 inches Height 9 inches
This creates a margin of 1 inch on all sides because letter size is 8.5 by 11 inches. The following code would change the margins to 0.5 inch on all sides:
//Set margins to 0.5 inch on all sides EO.Pdf.HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f);
Setting Page Orientation
EO.Pdf does not have a direct option for page orientation. The same paper with a different orientation is just a different size with width/height value swapped. For example, the following code uses A4 paper in landspace instead of in portrait orientation:
//Using A4 in landscape, note that the width/height values are swapped EO.Pdf.HtmlToPdf.Options.PageSize = new SizeF(EO.Pdf.PdfPageSizes.A4.Height, EO.Pdf.PdfPageSizes.A4.Width);
Using @page CSS at-rule
EO.Pdf supports @page CSS at-rule. You can either automatically or manually apply @page at-rule. @page at-rule is automatically applied when both of the following conditions are met:
- HtmlToPdfOptions.UsePrintMedia is set to True;
- HtmlToPdfOptions.PageSize is not explicitly set;
@page { size: A4; margin: 1cm; }
The following code demonstrates how to automatically apply the above style:
//@page rule is only automatically applied when UsePrintMedia //is set to true HtmlToPdf.Options.UsePrintMedia = true; //Convert a page that contains the above style block will //automatically set page size and margin based on the above style HtmlToPdf.ConvertUrl(url, pdf_file_name);
The @page at-rule will not be automatically applied when either of the above condition is not met. However in that case it can still be applied manually. The following code demonstrates how to apply @page at-rule manually:
using (HtmlToPdfSession session = HtmlToPdfSession.Create()) { //Load the Url to be convertered session.LoadUrl(url); //Get the @page at-rule information. GetPageStyle //returns null if no @page at-rule exists PageStyle style = session.GetPageStyle(); //Apply the rule if it exists if (style != null) style.Apply(session.Options); //Render the result session.RenderAsPDF(pdf_file); }