Asp.net To Pdf Pdfsharp

2020. 3. 3. 06:03카테고리 없음

Creating iTextSharp’s Document & WriterWhen 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 fileWhen 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 CodeI combine the above two codes and inside them add PDF element like paragraphs, lines, tables and so on. 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.ALIGNLEFT, 1)));ImageImage 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 & PdfPCellPdfTable 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.

Pdfsharp

Then I created a PdfPCell and an Image element, and then added the image to the PdfPCell. 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. JQuery makes AJAX procedure very easily.

I used it to create feature withing just a few minutes time. Example: Create a PdfI 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. 2. Few horizontal line.

3. A table containing 2 columns – a picture of a person on the first columns and person’s details on the second column. 4. A table containing 5 columns and looks like a Grid. 5. A paragraph containing a hello message. 6.

Table with 2 ColumnsCreate a table and set its width to 100% of the size of the Document. Align it to left size of the document (.HorizontalAlignment=0) and provide some spacing to it.Add the 2 columns (I set the border=0 as I don’t want any border on these 2 cells). In the first cell put image and on the second cell put text.Add the cells to the table and then add this table to document.The code is given below: //TablePdfPTable table = new PdfPTable(2);table.WidthPercentage = 100;table.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Righttable.SpacingBefore = 20f;table.SpacingAfter = 30f;//Cell no 1PdfPCell cell = new PdfPCell;cell.Border = 0;Image image = Image.GetInstance(Server.MapPath('/Content/Upload/salma.jpg'));image.ScaleAbsolute(200, 150);cell.AddElement(image);table.AddCell(cell);//Cell no 2chunk = new Chunk('Name: Mrs. Salma Mukherji,nAddress: Latham Village, Latham, New York, US, nOccupation: Nurse, nAge: 35 years', FontFactory.GetFont('Arial', 15, Font.NORMAL, BaseColor.PINK));cell = new PdfPCell;cell.Border = 0;cell.AddElement(chunk);table.AddCell(cell);//Add table to documentpdfDoc.Add(table).

Asp.net To Pdf Pdfsharp Word

Table with 5 columns that looks like a GridCreate the table with 5 columns by using table = PdfTable(5). Also make the first cell full width of the table by using Colspan=5.So the next cells will automatically come down from the 2nd row.

AbstractThis is a tip for creating PDF using ItextSharp and downloading the PDF file using MVC. IntroductionAs we know whenever we are working on a project there is a need of reports that a user wants to view for a respective business date - it can be any day to day transactional reports, inventory reports of stores etc.

Irrespective of the project in the tip of code snippet I will be generating a PDF report of a sample records which I will fetch from database as shown below step by step.1.1 Create New ProjectFigure 1: New Project in VS 20151.2 Select MVC Template for creating WEB Application as shown below:Figure 2: Selecting MVC TemplateHere I will using Entity Framework to retrieve records from the table. If you are new to Entity Framework my suggestion is to go and read my basic article on Entity Framework. First what records I am going to show into the pdf file?. I will be showing Employee details to the User as shown below in the snapshot.Figure 3: Information to be shown in pdf1.3 Creating your ModelsNow we will create Model class named Employee as shown below:Figure 4: Creating Model ClassFigure 5: Naming your Model class.