C# tutorial: PDF page setup


Page setup

In the previous page, you learnt to create a PDF document in five steps by using iTextSharep library. In this tutorial, you learn to define page setup for the PDF document. Like a word processor, you can define the size, orientation, and margins of a document. In iTextSharp, it is easy to accomplish this task.

1. Page Size

To define a size for the document, you can create a Rectangle object with the specified width and height. Then supply the object to the Document constructor when the Document object is created. In the example code below, a page size of 216 user units (=3 inches) width and 720 (=10 inches) user units height is created for the document. 72 units are equal to 1 inch.

//define page size in user units
Rectangle pageSize = new Rectangle(216.0f, 720.0f);
//create a document object
var doc = new Document(pageSize);

Alternatively, you can use the predefined page sizes. iTextSharp has many page sizes that you can choose. These page sizes are rectangle objects in the PageSize class. Below are some page sizes that can be used.

page sizes

 

PageSize.A0

PageSize.A1

PageSize.A2

PageSize.A3

PageSize.A4

PageSize.A5

PageSize.A6

PageSize.B0

PageSize.B1

PageSize.B2

PageSize.B3

PageSize.B4

//create a document object of A4 size
var doc = new Document(PageSize.A4);

2. Page Orientation

In default, the orientation of a PDF page is portrait. There are two ways to change the orientation of a PDF page from portrait to landscape. One way is using the Rotate() method of the Rectangle class. See the example code below.

//create a document object in landscape orientation
var doc = new Document(PageSize.A4.Rotate());

For another way, you can have a landscape PDF page by creating a Rectangle object with a width that is greater than its height.

//define page size in user units
Rectangle pageSize = new Rectangle(720.0f, 216.0f);
//create a document object in landscape orientation
var doc = new Document(pageSize);

3. Page Margins

Another version of Document class constructor, you can initialize the left, right, top, and bottom margins to the PDF document. The measurement of a margin is also specified in user unit.

//create a document object with a specified size and margins
var doc = new Document(PageSize.A4,72.0f,72.0f,72.0f,72.0f);



Comments

test comment

 test

test


2016-04-21
Javeed comment

 Javeed

This is actually the best forum i found on iTextsharp


2016-03-25
CAPTCHA image



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.