Convert Word Document to PDF document using iTextSharp in ASP.Net

Last Reply on May 09, 2012 07:31 AM By Mudassar

Posted on May 09, 2012 04:14 AM

I want when user upload  a doc file it convert into pdf. How can i do this. I read that itextsharp convert it. Please help on this.

Posted on May 09, 2012 04:16 AM

iTextSharp converts HTML to PDF hence you cannot use it

Refer

http://cathalscorner.blogspot.in/2009/10/converting-docx-into-doc-pdf-html.html


Posted on May 09, 2012 07:11 AM

It can work but when i use fileupload and when it convert it It shows error.
This file could not be found. (C:\Windows\system32\Input.docx)
I can change the coding and my codeing is

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Convert Input.docx into Output.doc 
        Convert(FileUpload1.FileName, "~/uploads/output.doc", WdSaveFormat.wdFormatDocument)

        ' 
        '             * Convert Input.docx into Output.pdf 
        '             * Please note: You must have the Microsoft Office 2007 Add-in: Microsoft Save as PDF or XPS installed 
        '             * http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en 
        '             

        Convert(FileUpload1.FileName, "~/uploads/output.pdf", WdSaveFormat.wdFormatPDF)

        ' Convert Input.docx into Output.html 
        Convert(FileUpload1.FileName, "~/uploads/output.html", WdSaveFormat.wdFormatHTML)
    End Sub

    ' Convert a Word 2008 .docx to Word 2003 .doc 
    Public Shared Sub Convert(ByVal input As String, ByVal output As String, ByVal format As WdSaveFormat)
        ' Create an instance of Word.exe 
        Dim oWord As word._Application = New word.Application()

        ' Make this instance of word invisible (Can still see it in the taskmgr). 
        oWord.Visible = False

        ' Interop requires objects. 
        Dim oMissing As Object = System.Reflection.Missing.Value
        Dim isVisible As Object = True
        Dim [readOnly] As Object = False
        Dim oInput As Object = input
        Dim oOutput As Object = output
        Dim oFormat As Object = format

        ' Load a document into our instance of word.exe 
        Dim oDoc As word._Document = oWord.Documents.Open(oInput, oMissing, [readOnly], oMissing, oMissing, oMissing, _
         oMissing, oMissing, oMissing, oMissing, oMissing, isVisible, _
         oMissing, oMissing, oMissing, oMissing)

        ' Make this document the active document. 
        oDoc.Activate()

        ' Save this document in Word 2003 format. 
        oDoc.SaveAs(oOutput, oFormat, oMissing, oMissing, oMissing, oMissing, _
         oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, _
         oMissing, oMissing, oMissing, oMissing)

        ' Always close Word.exe. 
        oWord.Quit(oMissing, oMissing, oMissing)
    End Sub

 


Posted on May 09, 2012 07:31 AM

This simply means you are not specifying the full path of the file.

Instead of name of the file pass the full path.

Example: c:\uploads\output.pdf