C# : Web : - How to Convert .doc,rtf,.xls files to pdf

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tim007
    New Member
    • Feb 2008
    • 29

    C# : Web : - How to Convert .doc,rtf,.xls files to pdf

    Hi all,

    can u pls tell me how to convert
    .doc,rtf,.xls files to pdf in C# .Net


    thx
  • vrpraba
    New Member
    • Feb 2008
    • 2

    #2
    Friend please to try this codeing

    Code:
    ==============================
    
    mports BCL.easyPDF.Interop.EasyPDFPrinter
    Imports System.Windows.Forms
    
    Public Class Form1
        Private oPrinter As Printer = Nothing
        Private oPrintJob As PrintJob = Nothing
        Private oPDFSetting As PDFSetting = Nothing
        Private oWatermarkText As String = Nothing
        Private oWatermarkBool As Boolean
    
        ' #################################################################################
        ' Sub CheckBox1_CheckedChanged
        ' Description: Check if users enables or disables the watermark option from the UI.
        ' #################################################################################
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            If (CheckBox1.Enabled = True) Then
                oWatermarkBool = True
            Else
                oWatermarkBool = False
            End If
        End Sub
    
        ' #################################################################################
        ' Function ConvertToPDF, return String
        ' Description: Return the watermark text.           
        ' #################################################################################
        Public Function GetWatermarkText() As String
            Return oWatermarkText
        End Function
    
    
        ' #################################################################################
        ' Sub ConvertToPDF
        ' Description: Initialize the printer driver name and check the watermark condition
        '              and print to pdf file.              
        ' #################################################################################
        Private Sub ConvertToPDF(ByVal inFile As String, ByVal outFile As String)
            Try
                ' This is how our printer driver gets initialized
                oPrinter = CreateObject("easyPDF.Printer.5")
                oPrintJob = oPrinter.PrintJob
    
                ' Use PDFSetting because we want to use Watermark feature
                oPDFSetting = oPrintJob.PDFSetting
    
                ' Check the watermark properties if the users has entered the text or not
                ' oWatermarkBool is a global variable for enable/disable watermark
                ' GetWatermarkText will return a value of oWatermarkText
                If (oWatermarkBool And (oWatermarkText <> Nothing)) Then
                    oPDFSetting.Watermark(0) = oWatermarkBool
                    oPDFSetting.WatermarkText(0) = GetWatermarkText()
                End If
    
                ' Printing to pdf from whatever inFile format is
                oPrintJob.PrintOut(inFile, outFile)
    
                MessageBox.Show("Success!!!")
    
                ' All the conversion errors will get caught here and the message box
                ' will pop up notifying users for the errors.
            Catch ex As System.Runtime.InteropServices.COMException
                MessageBox.Show(ex.Message)
                If (ex.ErrorCode = prnResult.PRN_R_CONVERSION_FAILED And Not oPrintJob Is Nothing) Then
                    MessageBox.Show(oPrintJob.ConversionResultMessage)
                End If
            End Try
    
            ' Clean up the object from the memory
            oPrinter = Nothing
    
        End Sub
    
    
        ' #################################################################################
        ' Sub Button1_Click 
        ' Description: open the "Open File Dialog" and "Save File Dialog"
        '              asking users to choose the input file and the target file name, 
        '              this case is pdf.
        ' #################################################################################
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim openFile As OpenFileDialog
            Dim savePDFFile As SaveFileDialog
            Dim inFileName As String
            Dim outFileName As String
            Dim pos As Integer
    
            ' "Open File Dialog" instantiation
            openFile = New System.Windows.Forms.OpenFileDialog
            openFile.Filter = "All Files (*.*)|*.*"
            openFile.Title = "Select a File"
            If (openFile.ShowDialog() <> System.Windows.Forms.DialogResult.OK) Then
                Return
            End If
            inFileName = openFile.FileName
    
            outFileName = inFileName
            pos = inFileName.LastIndexOf(".")
            If (pos > 0) Then
                outFileName = outFileName.Substring(0, pos)
            End If
            outFileName += ".pdf"
    
            ' "Save File Dialog" instantiation
            savePDFFile = New System.Windows.Forms.SaveFileDialog
            savePDFFile.FileName = outFileName
            savePDFFile.Filter = "pdf files (*.pdf)|*.pdf"
            savePDFFile.Title = "Save PDF File"
            If (savePDFFile.ShowDialog() <> System.Windows.Forms.DialogResult.OK) Then
                Return
            End If
            outFileName = savePDFFile.FileName
    
            ' Once we know the input file and the output file, we pass these information to
            ' the ConvertToPDF function for printing to pdf file.
            ConvertToPDF(inFileName, outFileName)
        End Sub
    
        ' #################################################################################
        ' Sub TextBox1_TextChanged
        ' Description: Assign the string text of the watermark to oWatermarkText variable.
        ' #################################################################################
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            oWatermarkText = TextBox1.Text
        End Sub
    
    End Class
    =======================================

    Comment

    • tim007
      New Member
      • Feb 2008
      • 29

      #3
      Thanks Prabakaran...

      let me try this in C# as i need to impliment this in C#

      this line of code
      imports BCL.easyPDF.Int erop.EasyPDFPri nter

      i would need this dll right ?

      thanks again vl try and get back to u

      Comment

      • tim007
        New Member
        • Feb 2008
        • 29

        #4
        No it didnt work ..couldnt get the dll

        Comment

        • vel
          New Member
          • Oct 2007
          • 12

          #5
          hi did you get the solution for this convertion problem.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Originally posted by tim007
            No it didnt work ..couldnt get the dll
            Looks like you need a 3rd party DLL to use his solution.

            Actually...ther e is no native PDF functionality, so you will need a third-party dll regardless. I suggest either searching for the one he used (start here, i found this from a quick google search) and if you can't find it, move on to Sourceforge. They have plenty of .NET PDF solutions.

            Comment

            • JulsSmile
              New Member
              • May 2012
              • 25

              #7
              Seems to be a great converter?

              Comment

              Working...