【问题标题】:Convert docx in bytes[] format to pdf in bytes[] format with openXML or similar使用openXML或类似方法将字节[]格式的docx转换为字节[]格式的pdf
【发布时间】:2014-09-13 17:29:17
【问题描述】:

我现在有一个函数可以使用 Microsoft.Office.Interop.Word 将 docx(以字节 [] 格式)转换为 pdf(以字节 [] 格式)

而且效果很好。除了它不能在线工作,因为它需要在服务器上安装 WinOffice,而我对此无能为力。

所以我需要去做别的事情,我正在考虑 openXML(除非你知道更好的方法)。

但是我该如何解决这个问题呢? 我只想获取这个 docx 文件,将其转换为字节 [] 格式的 pdf 并返回。

我之前在 Microsoft.Office 中的代码是这样的

public static byte[] ConvertDocx2PDF(byte[] DocxFile, string FileName)
{
    try
    {
        string path = Path.Combine(HttpRuntime.AppDomainAppPath, "MailFiles/DOCX2PDF");

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        Guid id = Guid.NewGuid();

        FileName = id.ToString() + FileName;

        path += "/" + FileName;



        if (File.Exists(path))
            File.Delete(path);

        File.WriteAllBytes(path, DocxFile);

        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        object oMissing = System.Reflection.Missing.Value;

        word.Visible = false;
        word.ScreenUpdating = false;

        // Cast as Object for word Open method
        Object filename = (Object)path;
        // Use the dummy value as a placeholder for optional arguments
        Microsoft.Office.Interop.Word.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 = (object)path.ToLower().Replace(".docx", ".pdf");
        object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

        if (File.Exists(outputFileName.ToString()))
            File.Delete(outputFileName.ToString());

        // 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);

        object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
        ((Microsoft.Office.Interop.Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
        doc = null;

        ((Microsoft.Office.Interop.Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;

        try
        {
            File.Delete(path);
        }
        catch { }

        return File.ReadAllBytes(path.ToLower().Replace(".docx", ".pdf"));
    }
    catch (Exception e)
    {

    }
    byte[] erroByte = new byte[0];
    return erroByte;
}

如前所述。它工作得很好,但在我的服务器上不起作用。

知道如何在 openXML 或其他任何方法中执行此操作吗?

感谢您的宝贵时间

【问题讨论】:

标签: c# .net pdf openxml docx


【解决方案1】:

您可以使用 OpenXmlSdk 和 OpenXML 强大的工具将 docx 转换为 html,然后您可以将您的 html 转换为 pdf。 这里不需要互操作。 最后,您可以使用WkHtmlToPDF 作为 dll 从 Html 创建 pdf。 Web 浏览器中的 pdf 呈现。这对我有用。

链接:

OpenXml Docx to Html

Docx to Html using XSLT

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    docx 是一种文档描述格式,而您可以将 pdf 视为矢量图形格式。尽管它非常努力地伪装成文档格式,但它本质上是一种图形格式。

    这是什么意思?这意味着需要进行适当的转换才能呈现文档。基本上,您必须重新实现 MS Word 的核心部分才能使其可靠。

    我想有一些库存在,但它比获得一台服务器要多得多,你可以在其中安装 Word 的副本。

    但毕竟,OpenOffice可以渲染word文档,所以也许有人可以尝试将它嵌入到一个(庞大的)库中......

    编辑:实际上,我找到了this answer,这可能会有所帮助,但它说它需要安装 OpenOffice。也许它可以与 xcopied OOo 一起使用,您可以尝试一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2022-07-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多