将DOC / DOCX转换为PNG

19
我正在尝试创建一个网络服务,将 doc/docx 转换为 png 格式。
问题在于我找不到任何库或类似的工具能够做到我所需的操作,并且我需要的是免费的而且不依赖于 Office(应用运行的服务器没有安装 Office)。
是否有任何可以帮助我获得这个的东西?或者我必须在使用某些依赖于 Office 的工具(如 Interop - 顺便说一下,我读过它在服务器上使用真的很糟糕)和非免费的工具之间做出选择?
谢谢。

3
你想做的事情存在问题,因为PNG是一张图片;而Word文档则是(a)一串二进制字符或者(b)一个XML文件的压缩包。在这两种情况下,需要使用Word应用程序来排版页面,以便将文档作为文档显示出来,并具有所有“花里胡哨”的元素(如格式设置、行和分页符、页眉、页脚等)。我所知道的唯一制作Word文档的“图片”方法是在显示器上显示文档,然后对每一页进行截屏。也许将其转换为PDF格式会更好? - Cindy Meister
1
该死,这个问题应该关闭 - 我们不在这里做产品推荐。 - TomTom
@TomTom:我不是在寻找产品!谷歌上有很多产品可以找到!! - Cyrus Raoufi
@CyC0der 你不是吗?好吧,问题是。你有没有读它?“有什么可以帮助我获得这个吗?还是我必须选择使用某些依赖于 Office 的东西(比如 Interop - 顺便说一下,我读到它在服务器上使用真的很糟糕)或者一些不免费的东西?”- 这是在寻求产品推荐。 - TomTom
@TomTom 哦!该死,抱歉那是我的错误... - Cyrus Raoufi
重点在于找到一种在服务器上无需使用Office的方法,如果可能的话。正如您在我的回答中所看到的,这似乎是可能的,而且不需要专有工具。对于文件格式来说,很少能找到任意转换的库,因为通常缺乏良好的中间表示,尽管像Pandoc这样的工具表明这并非完全不可能。我在这里使用PDF作为中间表示,您可以从那里开始。 - LaPingvino
6个回答

6

在服务器上安装LibreOffice。最新版本的LibreOffice拥有命令行界面,可用于将文档保存为PDF。(libreoffice --headless --convert-to pdf filename.doc[x])

然后使用例如imagemagick或LibreOffice Draw转换选项将PDF转换为图像。


对于浏览评论的人,这个是从顶部到底部免费的。 - LaPingvino

6

我知道这可能不是你想要的,因为它不是免费的。

但是Aspose可以做到你需要的。

Spire.doc也可以。同样不免费。

Aspose:

string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar;
string dataDir = new Uri(new Uri(exeDir), @"../../Data/").LocalPath;

// Open the document.
Document doc = new Document(dataDir + "SaveAsPNG.doc");

//Create an ImageSaveOptions object to pass to the Save method
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
options.Resolution = 160;

// Save each page of the document as Png.
for (int i = 0; i < doc.PageCount; i++)
{
    options.PageIndex = i;
    doc.Save(string.Format(dataDir+i+"SaveAsPNG out.Png", i), options);
}

Spire.doc (WPF):

using Spire.Doc;
using Spire.Doc.Documents;

namespace Word2Image
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document("sample.docx", FileFormat.Docx2010);
            BitmapSource[] bss = doc.SaveToImages(ImageType.Bitmap);
            for (int i = 0; i < bss.Length; i++)
            {
                SourceToBitmap(bss[i]).Save(string.Format("img-{0}.png", i));
            }
        }

        private Bitmap SourceToBitmap(BitmapSource source)
        {        

            Bitmap bmp;
            using (MemoryStream ms = new MemoryStream())
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);
                bmp = new Bitmap(ms);
            }
            return bmp;
        }
    }
}

谢谢您的回复,但我正在寻找免费的方法。在我的地区,这些库非常昂贵。 - Cyrus Raoufi
很不幸,它们在任何地方都很昂贵 :-( 我想做和你一样的事情,但到目前为止,我还没有找到任何免费的解决方案。- 我开始怀疑是否存在任何免费的解决方案,最接近免费解决方案的可能是使用Office Interop,但这对于一个服务来说并不好,因为它需要大量资源且速度相当慢。 - Gertsen
能够使用Interop吗?虽然不是最好的选择,但总比没有强。 - Cyrus Raoufi
1
这并不是官方支持的,所以可能会有所不同,但我认为这是可能的,就像在这个例子中一样:http://stackoverflow.com/questions/24830027/issue-with-converting-doc-to-png - Gertsen
我正在尝试转换一个包含阿拉伯文本的docx文件,但Spire.doc把它搞得一团糟 :( 可能它只适用于LTR方向的语言 :( - sohaiby

6

是的,这样复杂的文件类型转换通常在专门的/第三方库中得到很好的实现(如上述库中),或者例如在DevExpress文档自动化中实现:

using System;
using System.Drawing.Imaging;
using System.IO;
using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit;

using(MemoryStream streamWithWordFileContent = new MemoryStream()) {
    //Populate the streamWithWordFileContent object with your DOC / DOCX file content

    RichEditDocumentServer richContentConverter = new RichEditDocumentServer();
    richContentConverter.LoadDocument(streamWithWordFileContent, DocumentFormat.Doc);

    //Save
    PrintableComponentLink pcl = new PrintableComponentLink(new PrintingSystem());
    pcl.Component = richContentConverter;
    pcl.CreateDocument();

    ImageExportOptions options = new ImageExportOptions(ImageFormat.Png);

    //Paging
    //options.ExportMode = ImageExportMode.SingleFilePageByPage;
    //options.PageRange = "1";

    pcl.ExportToImage(MapPath(@"~/DocumentAsImageOnDisk.png"), options);
}

新版本的DevExpress如下所示:``public static void docxToImage(string inpath ) { var sourceServer = new RichEditDocumentServer(); sourceServer.LoadDocument(inpath); var pl= new PrintableComponentLink(); pl.PrintingSystemBase =new PrintingSystemBase(); pl.Component = sourceServer; pl.CreateDocument(true); var options = new ImageExportOptions(ImageFormat.Png); pl.ExportToImage("image.png", options); }`` - bh_earth0

5
我认为免费且无需办公室客户端的最佳方法需要三个步骤:将doc/docx转换为html-将html转换为PDF-将PDF转换为PNG。 Open XML可以帮助您完成第一步。这不需要安装任何Office客户端,有一个非常好的资源可以帮助您编写代码来解决这个第一步(http://openxmldeveloper.org/)。但是我认为它不能解决PDF/PNG问题。因此, iTextSharp将为您进行免费的PDF转换。但它无法从PDF转换为PNG。所以最后, GhostScript.NET将帮助您完成最后的转换。
这些是我整理的似乎最有用的链接:
我感觉没有人使用免费工具完成过这个任务。如果你成功了,请在Github上分享你的代码 :)

2
考虑使用powertools动态将docx转换为html(甚至可以使用office VSTO,速度会更快),然后使用wkhtmltopdf(直接或通过pechkin或类似工具)从html中渲染png。 我已经写过了为什么wkhtmltopdf比iTextSharp等其他工具更好here。 顺便说一句,我认为用于处理doc/docx的最佳商业库是TxText - 它真的很棒,你可以做任何想做的事情。

2

如果您的系统允许安装PNG虚拟打印机,您可以考虑使用PDFCreator(也可以打印为PNG格式)或类似的软件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接