Create a PDF file after extracting information

I need to create a pdf file from data retrieved from a database. I need to get data from a database and generate this data in pdf format.

I would like to have pointers (useful information).

Thanks.

+4
source share
5 answers

you can use itextsharp for this

protected void btnExportPdf_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=BusinessUnit.pdf"); Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView grd = new GridView(); grd.DataSource = yourdatatable.DefaultView//get data from DB in Datatable grd.DataBind(); grd.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A2, 8f, 8f, 8f, 8f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } 
+6
source

Here is a list of Open Source PDF Librarie that you can use:

PDFjet example

 PDF pdf = new PDF(); Font f1 = new Font(pdf, "Helvetica"); Image image1 = new Image(pdf, "images/eu-map.png"); Image image2 = new Image(pdf, "images/fruit.jpg"); Image image3 = new Image(pdf, "images/mt-map.gif"); // Please note: // All font and image objects must be created // before the first page object. Page page = new Page(pdf, A4.PORTRAIT); text.SetText( "The map on the right is an embedded GIF image"); text.SetPosition(90.0, 800); text.DrawOn(page); image3.SetPosition(390, 630); image3.ScaleBy(0.5); image3.DrawOn(page); pdf.wrap(); pdf.save("Example_03.pdf"); 

SharpPDF Example

 pdfDocument myDoc = new pdfDocument("TUTORIAL","ME"); pdfPage myPage = myDoc.addPage(); myPage.addText("Hello World!",200,450,predefinedFont.csHelvetica,20); myDoc.createPDF(@"c:\test.pdf"); myPage = null; myDoc = null; 

Report.NET Example

 Report report = new Report(new PdfFormatter()); FontDef fd = new FontDef(report, "Helvetica"); FontProp fp = new FontPropMM(fd, 25); Page page = new Page(report); page.AddCenteredMM(80, new RepString(fp, "Hello World!")); RT.ViewPDF(report, "HelloWorld.pdf"); 

ITextSharp Example

 Document document=new Document(); PdfWriter.getInstance(document,new FileOutputStream("hello.pdf")); document.open(); document.add(new Paragraph("Hello Pdf")); document.close(); 

Return on the fly created PDF files

You can return binary data using Response.Write see MSDN on "How to write binary data"

Here is an example of how you use Response.WriteFile to provide a PDF to the user:

 //Set the appropriate ContentType. Response.ContentType = "Application/pdf"; //Get the physical path to the file. string FilePath = MapPath("acrobat.pdf"); //Write the file directly to the HTTP content output stream. Response.WriteFile(FilePath); Response.End(); 
+9
source

Try using one of the installed library to create pdf. Here is a post with lots of information and many resources on this

https://stackoverflow.com/questions/177/how-do-i-programmatically-create-a-pdf-in-my-net-application/2384209#2384209

+2
source

Philippe's earlier answer is fantastic. This link may help in itextsharp:

http://forums.asp.net/t/1419119.aspx

+1
source

Source: https://habr.com/ru/post/1342973/


All Articles