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

Sebastian
  • 4,625
  • 17
  • 76
  • 145
  • 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… – stakx - no longer contributing Jul 06 '15 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 – Sebastian Jul 06 '15 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? – Bruno Lowagie Jul 06 '15 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. – Paulo Soares Jul 06 '15 at 12:14
  • @BrunoLowagie Yes i am trying for a solution where i can define page size – Sebastian Jul 06 '15 at 15:02
  • OK, I can explain that. Just a moment, I think I have answered that question before, but I need to look it up. – Bruno Lowagie Jul 06 '15 at 15:03
  • possible duplicate of [How Do I Set Page Size As Envelope , Landscape In Itextsharp](http://stackoverflow.com/questions/25886909/how-do-i-set-page-size-as-envelope-landscape-in-itextsharp) – Bruno Lowagie Jul 06 '15 at 15:04
  • @BrunoLowagie Cant really say it as a duplicate. Link is really guides me ..My question is askinng for the possibility of doing it from Razor view engine itself. LIke in the samples here http://pastebin.com/eeBDAQt4 or http://pastebin.com/2B31fzpg – Sebastian Jul 06 '15 at 15:13
  • Although the people who developed the Razor view engine uses my code (and as I now see, also [my examples](http://pastebin.com/eeBDAQt4); my name is mentioned there and those are my favorite books and movies), I don't know how you are using those templates. I only know that Razor is using code that dates from more than 10 years ago (June 26, 2003). Maybe you should consider a more modern approach. – Bruno Lowagie Jul 06 '15 at 15:18
  • I am planning to use a similar approach . Like get all values in a model to the Razor view and try to develop a pdf structure from cshtml itself instead of doing all things in code behind . So you are suggesting to do the stuffs from code directly instead of following this approach – Sebastian Jul 06 '15 at 15:23
  • I know you're looking for a better answer but I can tell you, without a doubt, iTextSharp cannot parse ASP.Net, MVC, Razor or cshtml. iTextSharp does, however, ship with a helper library called XmlWorker that _can_ parse HTML and CSS. For more on this, [see this answer](http://stackoverflow.com/a/25164258/231316) specifically the fourth paragraph. Also, although RazorPDF does use iTextSharp under the hood, as Bruno said it requires a 12 year obsolete version and is in no way endorsed or affiliated with the real iTextSharp in any way. – Chris Haas Jul 06 '15 at 19:49

1 Answers1

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • IS any possibility to do it from Razor view engine itself .See comment to get pastebin sample – Sebastian Jul 06 '15 at 15:14
  • 1
    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. – Bruno Lowagie Jul 06 '15 at 15:20