将.docx,.ppt文件转换为pdf [英] Convert .docx,.ppt file into pdf

查看:151
本文介绍了将.docx,.ppt文件转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用任何付费dll和MS Office的情况下将文件转换为pdf.我的代码在这里,但我不想使用Microsoft.Office.Interop.Word.

I want to convert file into pdf without using any paid dll and MS Office.My code is here but i do not want to use Microsoft.Office.Interop.Word.

public void ConvertWordToPDF()
        {
            #region"Word document to pdf converter "
            words.Application objword = new words.Application();
            // Create a new Microsoft Word application object
            //Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

            // C# doesn't have optional arguments so we'll need a dummy value
            object oMissing = System.Reflection.Missing.Value;

            // Get list of Word files in specified directory
            DirectoryInfo dirInfo = new DirectoryInfo(@"D:\\My Document\");
            FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
            
            objword.Visible = false;
            objword.ScreenUpdating = false;
            //int a = 1;
            foreach (FileInfo wordFile in wordFiles)
            {
                Object filename = (Object)wordFile.FullName;
               
                //// Cast as Object for word Open method
                // Use the dummy value as a placeholder for optional arguments
                words.Document doc = objword.Documents.Open(ref filename, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                     ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                doc.Activate();

                object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
                object fileFormat = words.WdSaveFormat.wdFormatPDF;

               // Save document into PDF Format

                doc.SaveAs(ref outputFileName,
                    ref fileFormat, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                // Close the Word document, but leave the Word application open.
                // doc has to be cast to type _Document so that it will find the
                // correct Close method.                
                object saveChanges = words.WdSaveOptions.wdDoNotSaveChanges;
                ((words._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;
}

添加了代码块格式[/编辑]

Code block formatting added[/Edit]

推荐答案

尝试使用iTextSharp库.免费.


如果可以满足您的查询,请标记为答案.

问候,
Dheeraj
try to use iTextSharp library. its free.


Please Mark as answer if it suffice your query.

Regards,
Dheeraj


这篇关于将.docx,.ppt文件转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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