【问题标题】:WPF: flowdocument to PDF [closed]WPF:流文档到 PDF [关闭]
【发布时间】:2011-07-17 02:57:14
【问题描述】:

你好,

是否有任何控件可以将流文档内容打印为 PDF?

编辑:不是第 3 方控制

谢谢

【问题讨论】:

    标签: wpf pdf flowdocument xps


    【解决方案1】:

    我能够通过将流文档内容保存到 .DOCX 并使用 Microsoft.Office.Interop.Word

    将其转换为 PDF 来解决我的问题
    using moiw = Microsoft.Office.Interop.Word;
    
    
    public static void WordToPDF(string docFileName)
        {
            // Create a new Microsoft Word application object
            moiw.Application word = new moiw.Application();
    
            // C# doesn't have optional arguments so we'll need a dummy value
            object oMissing = Missing.Value;
    
            // Get a Word file
            FileInfo wordFile = new FileInfo(docFileName);
    
            word.Visible = false;
            word.ScreenUpdating = false;
    
            // Cast as Object for word Open method
            Object filename = (Object)wordFile.FullName;
    
            // Use the dummy value as a placeholder for optional arguments
            moiw.Document doc = word.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(".docx", ".pdf");
            object fileFormat = moiw.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 = moiw.WdSaveOptions.wdDoNotSaveChanges;
            ((moiw._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            doc = null;
    
            // word has to be cast to type _Application so that it will find
            // the correct Quit method.
            ((moiw._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
            word = null;
        }
    

    【讨论】:

    • 您是如何将 FlowDocument 内容转换为 docx 的?
    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2023-03-16
    • 2014-08-15
    • 2021-04-13
    • 1970-01-01
    • 2018-05-30
    • 2010-09-24
    相关资源
    最近更新 更多