WPF:流程文档转 PDF

发布于 2024-10-22 13:06:44 字数 1539 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-10-29 13:06:44

我能够通过将流程文档内容保存到 .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;
    }

I was able to solve my problem by saving the flowdocument content to .DOCX and converting it to PDF by using Microsoft.Office.Interop.Word

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文