将Word文件打印为PDF [英] Print Word File to PDF

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

问题描述

大家好,有没有办法在不使用任何第三方组件的情况下将word文件打印为pdf?

提前谢谢.
Alder

Hi all, is there a way to print a word file to pdf without using any third party component?

Thanks in advance.
Alder

推荐答案

public static bool CreatePDF(string WordFilePath)
        {
            bool returnValue = true;
            try
            {
                ApplicationClass wordApplication = new ApplicationClass();
                Document wordDocument = null;

                object paramSourceDocPath = WordFilePath;
                object paramMissing = Type.Missing;

                string paramExportFilePath = Path.ChangeExtension(WordFilePath, "pdf");
                Helper.CheckExistingFile(paramExportFilePath);
                WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
                bool paramOpenAfterExport = false;
                WdExportOptimizeFor paramExportOptimizeFor =
                    Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                WdExportCreateBookmarks paramCreateBookmarks =
                    WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                try
                {
                    // Open the source document.
                    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);

                    // Export it in the specified format.
                    if (wordDocument != null)
                        wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref paramMissing);
                }
                catch (Exception ex)
                {
                    
                    returnValue = false;
                }
                finally
                {
                    // Close and release the Document object.
                    if (wordDocument != null)
                    {
                        wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                        wordDocument = null;
                    }

                    // Quit Word and release the ApplicationClass object.
                    if (wordApplication != null)
                    {
                        wordApplication.Quit(ref paramMissing, ref paramMissing,
                            ref paramMissing);
                        wordApplication = null;
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            catch(Exception ex)
            {
                returnValue = false;
                
            }
            return returnValue;
        }


谢谢,我已经采用了2°链接中报告的解决方案.
我的SaveAs步骤有问题.
如果我将wdFormatPDF指定为输出格式,则会给我一个错误(超大值").
如果我指定任何其他输出格式(例如wdFormatHTML),则可以使用.
我已经安装了Office2003.
有什么建议吗?

Thank you, I''ve adopted the solution reported in the 2° link.
I have a problem with the step SaveAs.
If I specify wdFormatPDF as output format it gives me an error ("outsize value").
If I specify any other output format (ex. wdFormatHTML) it works.
I have installed Office 2003.
Any suggestions?

public Boolean Print_Interop(int FileTyp, string Doc_Name, string PDF_Name)
        {
            Word.ApplicationClass MSWordDoc;
            object UnknownType = Type.Missing;
            Boolean TOk = false;

            object InputLocation = Doc_Name;
            object OutputLocation = PDF_Name;

            MSWordDoc = new Word.ApplicationClass();

            try
            {
                //To Open the Word Document
                MSWordDoc.Documents.Open(ref InputLocation,    //Input File Name Location
                    ref UnknownType,    // Conversion Conformation
                    ref UnknownType,    // Set ReadOnly Property
                    ref UnknownType,    // Add to the Recent Files
                    ref UnknownType,    // Document Password Setting
                    ref UnknownType,    // Password Templete
                    ref UnknownType,    // Revert
                    ref UnknownType,    // Write Password to Document
                    ref UnknownType,    // Write Password Template
                    ref UnknownType,    // File Format
                    ref UnknownType,    // Encoding File
                    ref UnknownType,    // Visibility
                    ref UnknownType,    // To Open or Repair
                    ref UnknownType,    // Document Direction
                    ref UnknownType,    // Encoding Dialog
                    ref UnknownType);   // XML Text Transform

                //To Get Document in PDF Format
                object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

                //To SaveAs the Document
                MSWordDoc.ActiveDocument.SaveAs(ref OutputLocation, //Output File Location
                ref SavePDFFormat,    // File Format
                ref UnknownType,    // Comment to PDF File
                ref UnknownType,    // Password
                ref UnknownType,    // Add to Recent File
                ref UnknownType,    // Write Password
                ref UnknownType,    // ReadOnly Propert
                ref UnknownType,    // Original Font Embeding
                ref UnknownType,    // Save Picture
                ref UnknownType,    // Saving Form Datas
                ref UnknownType,    // Save as AOVE Letter
                ref UnknownType,    // Encoding
                ref UnknownType,    // Inserting Line Breakes
                ref UnknownType,    // Allow Substitution
                ref UnknownType,    // Line Ending
                ref UnknownType);   // Add BiDi Marks

                //To Close the Document File
                MSWordDoc.Documents.Close(ref UnknownType, ref UnknownType, ref UnknownType);

                //To Exit the Word Application
                MSWordDoc.Quit(ref UnknownType, ref UnknownType, ref UnknownType);

                TOk = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);

                TOk = false;
            }

            return TOk;
        }


http://www.c-sharpcorner.com/uploadfile/sasi12prabhu/word-to -pdf-converter/ [ ^ ]
http://c-sharp- paradise.blogspot.com/2012/03/best-solution-to-convert-word-to-pdf-in.html [
http://www.gemboxsoftware.com/document/articles/c-sharp-vb-net-convert-word-to-pdf[^]
http://www.c-sharpcorner.com/uploadfile/sasi12prabhu/word-to-pdf-converter/[^]
http://c-sharp-paradise.blogspot.com/2012/03/best-solution-to-convert-word-to-pdf-in.html[^]


这篇关于将Word文件打印为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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