0

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

11
  • 1
    I don't quite get it. Page sizes in a PDF document are independent from any printer used to print out the PDF. Wouldn't you always want to (in fact, have to) specify what size pages your PDF contains? You cannot have a page of unknown size, after all… Commented Jul 6, 2015 at 10:23
  • Page size means width and height of the PDF page . Or without mentioning page size how the report will fit for pages with less dimensions than A4 Commented Jul 6, 2015 at 10:28
  • 1
    The page size in a PDF is defined in the /MediaBox entry. This is a required element. A PDF without a /MediaBox value is not a PDF. I don't understand your question. Can you clarify? When you create a Document instance with iTextSharp, you can pass a page size. If no page size is provided, PageSize.A4 is taken as default. Is that your question? Are you looking for the Document constructor that accepts a page size? Commented Jul 6, 2015 at 10:35
  • Careful when printing PDFs in dot-matrix printers, the printer driver will convert the PDF into an image and send it to the printer. It will take forever to print and the quality will be lousy. Commented Jul 6, 2015 at 12:14
  • @BrunoLowagie Yes i am trying for a solution where i can define page size Commented Jul 6, 2015 at 15:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

IS any possibility to do it from Razor view engine itself .See comment to get pastebin sample
I don't know Razor. I only know that it uses an obsolete version of iText. Take a look at your pastebin sample. I am the Lowagie from lowagie,com. You are showing me an example I wrote on June 26, 2003. That is 12 years ago. The iText version used by Razor is no longer supported. I don't recommend using it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.