使用itextsharp将任何类型的文档转换为pdf [英] Convert any type of document to pdf using itextsharp

查看:95
本文介绍了使用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


这篇关于使用itextsharp将任何类型的文档转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆