PDF OR Document To Print in MVC Web application

1.9k views Asked by At

In my MVC application when it comes to printing of reports i have few options

RazorPDF - advanatage of handling design from cshtml itself & can pass values from controller as model

iTextSharp - advanatage of handling design from cshtml itself & can pass values from controller as model

pdfSharp - No advantage of handling design from cshtml page. Have to do all coding from .cs file & modifications is very difficult. BUt have a great control over the layout of generated report

So Can any one suggest a method with both options

  • Can do the PDF design from cshtml itself.
  • Can specify width and height of the PDF page

Since the report is not always to print on laser printers. Need to giv support for dotmatrix print as well and in that case i have to mention width & height of page .Also there is a possibility toprint on letter heads so i have to mention widtha nd height of empty area again

Or any one can suggest a way to mention to width and height of PDF page with RazorPDF and iTextSharp approach

1

There are 1 answers

2
Bruno Lowagie On

Your question is about many different tools, but this is the answer in case you are using iTextSharp.

When you create a PDF from scratch using iTextSharp, you always need a Document and a PdfWriter class. The Document class is to be used for the high-level functionality; the PdfWriter class for the low-level operations.

The page size is defined at the Document level. You can create a new Document object like this:

Document document = new Document();

As we didn't pass any parameters to the constructor, iTextSharp will create a PDF using the default page size (A4) and margins of half an inch.

This is the equivalent of:

Document document = new Document(PageSize.A4, 36, 36, 36, 36);

As you can see: I use 36 as value for half an inch, because 1 inch = 72 user units in PDF.

If you want to define another page size, you can do so by using one of the other values available in the PageSize class, for instance:

Document document = new Document(PageSize.LETTER);

PageSize.A4 and PageSize.LETTER are instances of the Rectangle class, so if you need a page size that isn't defined in PageSize, then you can create your own rectangle. For instance:

Rectangle envelope = new Rectangle(432, 252);
Document document = new Document(envelope, 0, 0, 0, 0);

Where do these values come from? Let's do the Math:

6 inch x 72 points = 432 points (the width)
3.5 inch x 252 points = 252 points (the height)

This is how you define pages with a custom size.