Generate, Edit, and Read PDFs within MVC Applications; # Convert MVC View to PDF in C# and VB ASP.NET; # Works with HTML, CSS, JavaScript, & Images. Creating iTextSharp’s Document & Writer When you Create a Pdf file, the first step is to create a Document and a PdfWriter. Then pen the Document using.Open() method. Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15); PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); Here I created the Document and set the page size as A4, and provided margins to it. Then I created an instance of PdfWriter class and attached it to the Document. On the 3rd line of code I opened the Document. Creating the PDF file When you have to Create a Pdf file, then use the following code: pdfWriter.CloseStream = false; pdfDoc.Close(); Response.Buffer = true; Response.ContentType = 'application/pdf'; Response.AddHeader('content-disposition', 'attachment;filename=Credit-Card-Report.pdf'); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); I closed the PdfWriter stream and the document and then set the ContentType to “application/pdf”. By using the Response.AddHeader() I gave the Pdf file name and with Response.Write() method the Pdf file is created. Combining the Code I combine the above two codes and inside them add PDF element like paragraphs, lines, tables and so on. In this way the whole PDF file is created. So look at the structure of the full combined code: /*Creating iTextSharp’s Document & Writer*/ Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15); PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); /*End*/ //Here you create PDF elements like paragraph, table, lines and so on. /*Creating the PDF file*/ pdfWriter.CloseStream = false; pdfDoc.Close(); Response.Buffer = true; Response.ContentType = 'application/pdf'; Response.AddHeader('content-disposition', 'attachment;filename=Credit-Card-Report.pdf'); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); /*End*/ Creating PDF Elements on iTextSharp The iTextSharp provides lot of PDF elements which you can add to the PDF document. These are Paragraph, Image, Table, Chunk and so on. Let us discuss each of them: Chunk Chunk is a building block of element through which you can add a text with a certain font. Fifa 2014 world cup wavin flag songs dowload. To create a text, with Arial font, font-size as 20, font-weight as bold & italic and color of magenta, use chunk like this: Chunk chunk = new Chunk('Your Text', FontFactory.GetFont('Arial', 20, Font.BOLDITALIC, BaseColor.MAGENTA)); Paragraph A paragraph is similar a chunk and is used to create a certain text. Paragraph para = new Paragraph('Your Text'). A paragraph with Chunk can be used to create a Horizontal line like this: Paragraph line = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1))); Image Image represents a Graphical Element. You can use it to add any image to your PDF file. Image image = Image.GetInstance(Server.MapPath('~/myPic.jpg')); PdfTable & PdfPCell PdfTable is a table element containing rows and columns. It contains PdfPCell which contain other elements like Image, Chunk, Paragraph, etc. Check the below code: PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(); Image image = Image.GetInstance(Server.MapPath('~/myPic.jpg')); cell.AddElement(image); table.AddCell(cell); Paragraph para = new Paragraph('Your Text'); cell = new PdfPCell(); cell.AddElement(para); table.AddCell(cell); In the above code I created a PdfPTable that has 2 columns. Then I created a PdfPCell and an Image element, and then added the image to the PdfPCell. D day hack version apk download. Finally I added that PdfPCell to the PdfPTable. In the same way I added a Paragraph element to another cell and added that cell to the table. Ek thi daayan film. JQuery makes AJAX procedure very easily. I used it to create feature withing just a few minutes time. Example: Create a Pdf I now give you a complete example that explains How to Create a Pdf file with iTextSharp. This PDF file will look as shown below: To download this PDF file. This Pdf file has the following sections: • 1. A top heading with a text. Few horizontal line.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |