none
create pdf file from C#.net

    Question

  • Hi,

    I need to do the following task in C#.Net. Can anyone tell me which is the best method to follow? Is there any in build features available in .net? Or do we need to use some external libraries.

    * Import grid view data to pdf and save it in local disc (create pdf file in runtime)

    * Read content from pdf file and convert it into grid view

     

    Thanks in advance!

    Regards,

    Sakthivel. S

     

    Thursday, June 24, 2010 8:29 AM

Answers

All replies

    • Marked as answer by SamAgain Friday, July 02, 2010 3:07 AM
    Thursday, June 24, 2010 9:07 AM
    • Marked as answer by SamAgain Friday, July 02, 2010 3:07 AM
    Thursday, June 24, 2010 6:44 PM
  • This external library enables you to create PDF from C# very easily. Just create create document model table populated with data from grid view and export it to PDF.

    Here is a C# code:

    // Create a new empty document.
    DocumentModel document = new DocumentModel();
    
    var table = new Table(document);
    
    // Call method that creates rows and cells with data from GridView and adds them to Table.
    PopulateTable(gridView, table);
    
    // Add document content.
    document.Sections.Add(
    	new Section(document,
    		table));
     
    // Save the document to a PDF file.
    document.Save("Document.pdf");

    Thursday, July 19, 2012 7:54 AM
  • You can use pdfsharp library for generating pdf file from c# applications check this one ..  Create PDF file from C# is explain the simple step by step methods to generate pdf file from c#.

    Liam


    • Edited by liam_web Friday, October 18, 2013 6:04 AM spell
    Friday, October 18, 2013 6:03 AM
  • Creating PDF file in runtime can be done with this tool http://www.componentpro.com/pdf.net/. I paste code snippet to create a PDF document (the code is copied from: http://www.componentpro.com/doc/pdf/Creating-tables.htm)

    using System;
    using System.Drawing;
    using ComponentPro.Pdf;
    using ComponentPro.Pdf.Graphics;
    using ComponentPro.Pdf.Tables;
    
    ...
    
    static string[][] datastring = new string[2][];
    
    [STAThread]
    static void Main()
    {
        // Create a new PDF document. This object represents the PDF document.
        // This document has one page, by default. Additional pages have to be added.
        PdfDocument doc = new PdfDocument();
    
        // Add a page to the document 
        PdfPage page = doc.Pages.Add();
    
        // Specify values for the table
        datastring[0] = new string[] {"111", "Maxim", "100"};
        datastring[1] = new string[] {"222", "Calvin", "95"};
        // Creates a new document
        PdfSimpleTable table = new PdfSimpleTable();
        table.DataSource = datastring;
    
        table.BeforeRowLayout += new BeforeRowLayoutEventHandler(table_BeforeRowLayout);
        table.BeforeCellLayout += new BeforeCellLayoutEventHandler(table_BeforeCellLayout);
        
        // Draw the table 
        table.Draw(page, new PointF(0, 0));
    
        // Save the PDF document to disk.
        doc.Save(@"c:\temp\customize table.pdf");
    
        // Use the following code to stream the document to the browser. 
        // pdfDoc.Save("customize table.pdf", Response, HttpResponseType.OpenInsideBrowser); // Response is an HttpResponse object.
    
        // Close the document.
        doc.Close();
    }
    
    static void table_BeforeCellLayout(object sender, BeforeCellLayoutEventArgs args)
    {
        if (args.CellIndex == 0 && (args.RowIndex % 2) == 1)
        {
            // Draw ellipses inside cells
            args.Graphics.DrawEllipse(PdfBrushes.Green, args.Bounds);
        }
    }
    
    static void table_BeforeRowLayout(object sender, BeforeRowLayoutEventArgs args)
    {
        if ((args.RowIndex % 2) == 0)
        {
            args.IgnoreColumnFormat = true;
        }
    }


    Saturday, May 03, 2014 5:07 PM
  • Most solutions create PDF documents from scratch (eg with using iTextSharp or something similar). You may also want to use a solution that starts out with a predefined template and simply merges your data with it into a new PDF document.

    There are PDF generators that use HTML as templates or Word-documents (eg: www.docati.com). I prefer those, since they provide a nice and clean way of separating the document/report markup from your code.

    Friday, February 10, 2017 12:39 PM
  • I agree with Sam R. Smith as far as the concept is concerned, using a template to generate a final document and convert it to pdf. If you are inserting larger chunks of text directly into pdf if has to flow over several lines, pushing the text underneath further down, which cannot be done in PDF.

    Here is the code snippet using Docentric Toolkit to achieve pdf output from a MS Word template and using data from the database:

    IEnumerable<Customer> customers = DataAccess.GetCustomerById(15);
    DocumentGenerator dg = new DocumentGenerator(customers);
    DocumentGenerationResult result = dg.GenerateDocument("MyTemplate.docx", "MyReport.pdf");
    This is conceptually the same as Docati, which Sam R. Smith suggests, except that Docentric Toolkit is much more powerful.

    Wednesday, May 03, 2017 7:27 PM