Hi all,
can u pls tell me how to convert
.doc,rtf,.xls files to pdf in C# .Net
thx
can u pls tell me how to convert
.doc,rtf,.xls files to pdf in C# .Net
thx
==============================
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