創建document時,設置pageSize,和距離四周的距離,設置頁面。
如果沒有傳入參數,默認是A4大小
Rectangle pageSize = new Rectangle(216f,720f); Document doc = new Document(pageSize,30,30,50,50);//設置頁面的大小
完整代碼如下:
/// <summary> /// 自定義頁面大小 /// </summary> public static void CreatePDF() { Stream str = System.IO.File.OpenWrite(@"d:\helloworld.pdf");//創建一個PDF文件 Rectangle pageSize = new Rectangle(216f,720f); Document doc = new Document(pageSize,30,30,50,50);//設置頁面的大小 PdfWriter.GetInstance(doc, str);//將pdf文檔寫入文件 doc.Open(); doc.Add(new Paragraph("Hello World"));//在文檔上寫“Hello World” doc.Close(); }
執行效果:
ITEXT已經為我們定義了一套標准的頁面大小:
Document doc = new Document(PageSize.A5,30,30,50,50);//使用ITEXT提供的類庫設置頁面的大小
我們在定義的頁面后面加上rotate方法,可以使頁面翻轉90度
在創建document以后,可以通過setPageSize,setMargins,setMarginMirroring,setMarginMirroringTopBottom設置頁面大小,頁邊距等:
doc.SetPageSize(PageSize.A5); doc.SetMarginMirroringTopBottom(true);
注意,頁面長寬的單位是pt




