使用itextsharp将任何类型的文档转换为pdf

问题描述:

我需要转换任何具有任何类型扩展名的文档文件,如.doc,.docx,.xml,.xsl,.txt ,. rft等,使用itextsharp dll到pdf文件。任何人都可以帮我分享代码以达到这个要求。

I need to convert any document file which is having any type of extension like .doc, .docx, .xml, .xsl, .txt, .rft etc., to pdf file using itextsharp dll. Can anyone help me to share the code to achieve this requirement.

谢谢&问候,

Kishore。

Thanks & regards,
Kishore.

Kishore

这是一个代码示例:>

this is an example of code:>

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
 
namespace Word2Pdf
{
    class Program
    {
        [STAThreadAttribute]
        static void Main(string[] args)
        {
 
            using (var client = new WebClient())
            {
                Console.WriteLine("Please choose a Word document to convert to PDF.");
 
                var openFileDialog = new OpenFileDialog {Filter = 
"Word document(*.doc;*.docx)|*.doc;*.docx"};
                if (openFileDialog.ShowDialog()!=DialogResult.OK) return;                               
                var fileToConvert = openFileDialog.FileName;
 
                Console.WriteLine(string.Format("Converting the file {0} Please wait.", fileToConvert));
 
                var data = new NameValueCollection();               
                data.Add("OutputFileName", "MyFile.pdf"); 
 
                try
                {                    
                    client.QueryString.Add(data);
                    var response = 
client.UploadFile("http://do.convertapi.com/word2pdf", fileToConvert);
                    var responseHeaders = client.ResponseHeaders;
                    var web2PdfOutputFileName = responseHeaders["OutputFileName"];
                    var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), web2PdfOutputFileName);
                    File.WriteAllBytes(path, response);
                    Console.WriteLine("The conversion was successful! The word file {0} converted to PDF and saved at {1}", fileToConvert, path);
                }
                catch (WebException e)
                {
                    Console.WriteLine("Exception Message :" + e.Message);
                    if (e.Status == WebExceptionStatus.ProtocolError)
                    {
                        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                    }
 
                }
 

            }
 
            Console.ReadLine();
        }

和 

http://stackoverflow.com/questions/1537063/itextsharp-convert-word-doc-docx-to-pdf

http://www.c-sharpcorner。 com / Blogs / 10025 / convert-text-document-to-pdf-file.aspx