使用openXML或类似方法将docx以bytes []格式转换为pdf,格式为[]格式

时间:2014-07-22 18:32:47

标签: c# .net pdf openxml docx

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

它很棒。除了它不能在线工作的事实,因为它需要在服务器上安装WinOffice,我无法做任何事情。

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

但我究竟会怎么回事呢? 我只想获取这个docx文件,转换并将其作为pdf以bytes []格式返回。

我之前在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或其他任何地方执行此操作吗?

感谢您的时间

2 个答案:

答案 0 :(得分:1)

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

链接:

OpenXml Docx to Html

Docx to Html using XSLT

希望这有帮助!

答案 1 :(得分:0)

docx是一种文档描述格式,而您可以将pdf视为矢量图形格式。尽管它很难伪装成文档格式,但它本身就是一种图形格式。

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

我想有一些库存在,但它比你获得一个只能安装Word副本的服务器要花费更多。

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

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