简体   繁体   English

在MVC Web应用程序中要打印的PDF或文档

[英]PDF OR Document To Print in MVC Web application

In my MVC application when it comes to printing of reports i have few options 在我的MVC应用程序中,当涉及到打印报告时,我几乎没有选择

RazorPDF - advanatage of handling design from cshtml itself & can pass values from controller as model RazorPDF-从cshtml本身处理设计的优势,并且可以将控制器的值作为模型传递

iTextSharp - advanatage of handling design from cshtml itself & can pass values from controller as model iTextSharp-从cshtml本身处理设计的优势,并且可以将控制器的值作为模型传递

pdfSharp - No advantage of handling design from cshtml page. pdfSharp-从cshtml页面处理设计没有优势。 Have to do all coding from .cs file & modifications is very difficult. 必须从.cs文件进行所有编码和修改非常困难。 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. 可以从cshtml本身进行PDF设计。
  • Can specify width and height of the PDF page 可以指定PDF页面的宽度和高度

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 或者任何人都可以使用RazorPDF和iTextSharp方法来建议一种提及PDF页面的宽度和高度的方法

Your question is about many different tools, but this is the answer in case you are using iTextSharp. 您的问题与许多不同的工具有关,但这是在使用iTextSharp的情况下的答案。

When you create a PDF from scratch using iTextSharp, you always need a Document and a PdfWriter class. 使用iTextSharp从头开始创建PDF时,始终需要一个Document和一个PdfWriter类。 The Document class is to be used for the high-level functionality; Document类将用于高级功能; the PdfWriter class for the low-level operations. 用于低级操作的PdfWriter类。

The page size is defined at the Document level. 页面大小在Document级别定义。 You can create a new Document object like this: 您可以创建一个新的Document对象,如下所示:

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. 由于我们没有将任何参数传递给构造函数,因此iTextSharp将使用默认页面大小(A4)和半英寸的边距来创建PDF。

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. 如您所见:我将36英寸用作半英寸的值,因为PDF中1英寸= 72个用户单位。

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: 如果要定义另一个页面大小,可以使用PageSize类中可用的其他值之一来进行定义,例如:

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. PageSize.A4PageSize.LETTERRectangle类的实例,因此,如果您需要PageSize未定义的页面大小,则可以创建自己的矩形。 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. 这是定义自定义尺寸页面的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM