将DOC/DOCX转换为PNG

rel*_*sis 16 c# asp.net png web-services ms-word

我正在尝试创建一个将doc/docx转换为png格式的Web服务.

我似乎遇到的问题是我找不到任何可以满足我需要的库或其他东西,考虑到我正在寻找免费的东西而不依赖于Office(应用程序将运行的服务器没有安装Office) ).

有什么能帮助我获得这个吗?或者我必须选择使用依赖于办公室的东西(比如Interop - 我读的哪个在服务器上使用真的很糟糕)或者不是免费的东西?

谢谢

Ger*_*sen 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);
}
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*ail 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);
}
Run Code Online (Sandbox Code Playgroud)


Bal*_*lah 5

我认为免费且无需 Office 客户端的最佳方法需要 3 个步骤:将 doc/docx 转换为 html - 将 html 转换为 PDF - 将 PDF 转换为 PNG。

Open XML将帮助您完成第一篇文章。这不需要安装任何 Office 客户端,并且有一个非常好的资源可以帮助您组合代码来解决第一步 ( http://openxmldeveloper.org/ )。但我不认为它可以解决 PDF/PNG 问题。因此,

iTextSharp将为您免费进行 PDF 转换。但它无法从 PDF 转换为 PNG。所以最后,

GhostScript.NET将带您冲过终点线。

这些是我整理的似乎最有用的链接:


我感觉从来没有人使用免费工具做过这样的事情。如果你成功了,请在 Github 上分享你的代码:)


LaP*_*ino 5

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

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