Convert Microsoft Office Document (Word) to PDF using C# and VB.Net in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

I need to convert a ppt to pdf using command line argument. I have got office 2010 installed in my system. Is there a way to do it without using Office interop or any third party sources. I can use the printer.

Posted 3 months ago Modified on 3 months ago

Hi pawanjois,

No its not possible without using Office Interop or any third party library.

Using Office Interop refer the below sample code.

C#

private void Word2PDFWithInterop(object source, object destination)
{
    object missing = Type.Missing;
    Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
    try
    {
        application.Visible = true;
        application.Activate();
        // Open the source document
        application.Documents.Open(ref source, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        application.Application.Visible = false;
        application.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
        object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
        // Export it in the specified format
        application.ActiveDocument.SaveAs(ref destination, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    }
    catch (Exception e)
    {
        // Respond to the error
        Response.Write(e.Message);
    }
    finally
    {
        // Close and release the Document object
        if (application != null)
        {
            application.Documents.Close(ref missing, ref missing, ref missing);
        }
        application.Quit(ref missing, ref missing, ref missing);
    }
}

VB.Net

Private Sub Word2PDFWithInterop(source As Object, destination As Object)
    Dim missing As Object = Type.Missing
    Dim application As New Microsoft.Office.Interop.Word.ApplicationClass()
    Try
        application.Visible = True
        application.Activate()
        ' Open the source document
        application.Documents.Open(source, missing, missing, missing, missing, missing, missing, missing, _
                                   missing, missing, missing, missing, missing, missing, missing, missing)
        application.Application.Visible = False
        application.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize
        Dim format As Object = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF
        ' Export it in the specified format
        application.ActiveDocument.SaveAs(destination, format, missing, missing, missing, missing, missing, missing, _
                                          missing, missing, missing, missing, missing, missing, missing, missing)
    Catch e As Exception
        ' Respond to the error
        Response.Write(e.Message)
    Finally
        ' Close and release the Document object
        If application IsNot Nothing Then
            application.Documents.Close(missing, missing, missing)
        End If
        application.Quit(missing, missing, missing)
    End Try
End Sub

Posted 3 months ago Modified on 3 months ago

I have modified my previous reply.

Please refer the below link.

http://www.aspforums.net/Threads/202049/Convert-any-ms-office-doc-to-pdf/Replies/1#Replies

If you want to execute cmd from code behind then refer the below code.

Refer the below code.

C#

string cmdText = "/c /pt";
System.Diagnostics.Process.Start("CMD.exe", cmdText);

VB.Net

Dim cmdText As String = "/c /pt"
System.Diagnostics.Process.Start("CMD.exe", cmdText)

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html