none
Fill a PDF form from Excel RRS feed

  • Question

  • Hello to all.

    I've been googling around for days to find some examples to solve my problem but was unable to find a good one.

    I’m using Excel 2007 and Visual Studio 2010

    Normally I report the data using Excel itself (a workbook). Now the request I have is to fill a PDF file containing a form with a huge number of lines. This means the data i have in the columns and lines of the Excel Worksheet have to be entered in the PDF form fields. I do not even have a way to know the PDF form fields names...

    Can someone please give me a clue of how can i achieve this using VB?

    Thanks in advance for any kind help.

    Octavio

    Wednesday, April 9, 2014 4:39 PM

Answers

  • Hello Octavio,

    VSTO doesn't provide anything for your task. Moreover, there is no trivial way for filling a pdf file programmatically.

    The Excel 2007+ object model provides the ExportAsFixedFormatmethod of the Workbook class which is used to publish a workbook to either the PDF or XPS format. So, instead of filling the ready-made form you can create the form in Excel and then export it as a pdf file.

    Wednesday, April 9, 2014 5:29 PM
  • Hi,

    As Eugene said, Excel object model only provides the Workbook.ExportAsFixedFormat Method to publish a workbook to either the PDF or XPS format.

    But as you said, you want to open an existing PDF file and fill some data into it. To do these, we need to call the exposed APIs of PDF no matter with VB.Net or C#. So I think your requirement is more related to PDF development. Since PDF is a 3rd-party product, about its development, you could consider resorting to PDF official forum for more effective responses.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 10, 2014 9:50 AM
    Moderator

All replies

  • Hello Octavio,

    VSTO doesn't provide anything for your task. Moreover, there is no trivial way for filling a pdf file programmatically.

    The Excel 2007+ object model provides the ExportAsFixedFormatmethod of the Workbook class which is used to publish a workbook to either the PDF or XPS format. So, instead of filling the ready-made form you can create the form in Excel and then export it as a pdf file.

    Wednesday, April 9, 2014 5:29 PM
  • Hi Eugene,

    Thank you for answering.

    My problem is that i have no other choice than to fill in the existing PDF file.

    Since, as you say, VSTO will not help me, do you think i should place my question in another forum? I can use any other tool...

    I'm desperate...

    Thank you.

    Octavio

    Wednesday, April 9, 2014 7:15 PM
  • VSTO doesn't provide anything for this.

    In my opinion your task is how to fill a pdf form from a .net application (where the application is represented by a VSTO add-in). Since VSTO has nothing for this I'd suggest to try asking such questions in the Visual C# forums. May there you will get any valuable suggestions.

    Wednesday, April 9, 2014 7:23 PM
  • Thank you again Eugene.

    I dont know nothing about c#. 

    I can deal with Visual Basic...

    Octavio

    Wednesday, April 9, 2014 8:17 PM
  • See Visual Basic forums.
    Wednesday, April 9, 2014 8:43 PM
  • Hi,

    As Eugene said, Excel object model only provides the Workbook.ExportAsFixedFormat Method to publish a workbook to either the PDF or XPS format.

    But as you said, you want to open an existing PDF file and fill some data into it. To do these, we need to call the exposed APIs of PDF no matter with VB.Net or C#. So I think your requirement is more related to PDF development. Since PDF is a 3rd-party product, about its development, you could consider resorting to PDF official forum for more effective responses.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 10, 2014 9:50 AM
    Moderator
  • Thank you Luna. I will follow your advice.

    Octavio

    Thursday, April 10, 2014 10:55 AM
  • Hi Octavio, I would suggest you to try Aspose.Cells and Aspose.Pdf APIs for your need. You can read the data from Excel files using Aspose.Cells, get and fill form fields in PDF using Aspose.Pdf. Coding with VB.NET using  Aspose APIs is very simple. You can download a free trial version of Aspose.Pdf and Aspose.Cells, add the DLLs in your project and start coding.  You can read the excel file cell by cell or simply import the data from Excel sheet to a data table as per your requirement. Following is a sample which reads an excel sheet to a data table and then fill the PDF form fields based on the that data. As you have not shared much details regarding your data and PDF file, I am assuming that first column in excel sheet contains field names and second column contains the values for this sample. 

    'Instantiating a Workbook object and opening an Excel file
    Dim workbook As New Workbook("book1.xls")
    
    'Accessing the first worksheet in the Excel file
    Dim worksheet As Worksheet = workbook.Worksheets(0)
    
    'Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
    Dim dataTable As System.Data.DataTable = worksheet.Cells.ExportDataTable(0, 0, 7, 2, True)
    
    'open document
    Dim pdfDocument As New Aspose.Pdf.Document("input.pdf")
    
    For Each dr As DataRow In dataTable.Rows
        'get a field
        Dim textBoxField As TextBoxField = TryCast(pdfDocument.Form(dr(0).ToString()), TextBoxField)
    
        'modify field value
        textBoxField.Value = dr(0).ToString()
    Next dr
    
    'Save the resultant file
    pdfDocument.Save("output.pdf")
    If you can share some details about your scenario, I will be more than happy to share the solution according to the requirements.

    P.S. I am working as Social Media Developer for Aspose.

    • Edited by Nausherwan Aslam Friday, April 11, 2014 6:08 AM Links added after account verification
    Thursday, April 10, 2014 7:16 PM