注册 登录
编程论坛 ASP.NET技术论坛

excel、word转换成pdf

hhy420459674 发布于 2011-01-21 16:29, 3330 次点击
如何将excel、word转换成pdf呢
分不够,有了多给点哈
谢谢了!我急用啊~~~
6 回复
#2
baoyuwang1982011-01-22 19:50
可以从网上下载ICSharpCode.SharpZipLib.dll、itextsharp.dll这两个组件
然后 编程实现转换
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using

namespace CreatePDFDocument
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //该变量保存PDF的文档名
        public static string filePath = "";

        //创建PDF文档
        private void button1_Click(object sender, EventArgs e)
        {
            //给出文件保存信息,确定保存位置
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "PDF文件(*.PDF)|*.PDF";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = saveFileDialog.FileName;
                //开始创建PDF文档
                Document document = new Document();
                PdfWriter.getInstance(document, new FileStream(filePath, FileMode.Create));
                document.Open();
                BaseFont baseFont = BaseFont.createFont(@"c:\windows\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 20);
                document.Add(new Paragraph(richTextBox1.Text, font));
                document.Close();
                MessageBox.Show("祝贺你,文档创建成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.Close();
        }
    }
}
#3
baoyuwang1982011-01-22 19:51
顺便T一下 网上也有 转换工具,可以去百度下
#4
hhy4204596742011-01-23 12:38
回复 2楼 baoyuwang198
ICSharpCode.SharpZipLib.dll、itextsharp.dll
请问这两个组建在哪里下载啊?我在网上没有找到,下载下来的都是有问题的,感觉的是文件损坏的那种!
帮个忙 谢谢!
#5
isolated2011-02-14 22:03
很好的知识,学习下
#6
jyhfr67ihfr2014-02-24 14:57


这几款pdf转换软件好用

免费下载pdf转换成word转换器 http://www. 

pdf转换成word转换器 http://dl.

pdf转换成word http://www.

另外这款word转换成pdf格式转换器也很好用万能word转pdf转换器 http://www.

 
#7
wangnannan2014-02-24 15:56
我是用office2007组件 然后另存为pdf 这样比较稳妥 几乎不出什么错误吧 主要代码你可以参考一下
程序代码:
    //Word转换成pdf
        /// <summary>
        
/// 把Word文件转换成为PDF格式文件
        
/// </summary>
        
/// <param name="sourcePath">源文件路径</param>
        
/// <param name="targetPath">目标文件路径</param>
        
/// <returns>true=转换成功</returns>
        public static bool DOCConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            object paramMissing = Type.Missing;
            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;
                Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                wordDocument = wordApplication.Documents.Open(
                ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing);
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                    paramExportFormat, paramOpenAfterExport,
                    paramExportOptimizeFor, paramExportRange, paramStartPage,
                    paramEndPage, paramExportItem, paramIncludeDocProps,
                    paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                    paramBitmapMissingFonts, paramUseISO19005_1,
                    ref paramMissing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        /// <summary>
        
/// 转换方法 ppt to pdf
        
/// </summary>
        
/// <param name="sourcePath"></param>
        
/// <param name="targetPath"></param>
        
/// <param name="targetFileType"></param>
        
/// <returns></returns>
        private static bool Convert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType)
        {
            bool result;
            object missing = Type.Missing;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
            Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
            try
            {

                application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }


 
1