将DOC / DOCX转换为PNG

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

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

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

谢谢

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

但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; } } } 

是的,这种复杂的文件类型转换通常在专门的/第三方库(如前面提到的)中实现,或者,例如,在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); } 

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

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

我认为免费且没有办公室客户端的最佳方式需要一个3个步骤:将doc / docx转换为html – 将html转换为PDF – 将PDF转换为PNG。

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

iTextSharp将为您进行免费的PDF转换。 但它不能从PDF到PNG。 最后,

GhostScript.NET将帮助您完成终点线。

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

  • 将docx转换为html的半工作方式: 如何使用带格式的open xml将docx转换为html文件
  • 关于如何使用Ghostscript转换png的例子的非主题问题: 在不使用特定C#库的情况下将PDF转换为JPG /图像
  • 另一个使用Ghostscript的链接: 是否可以使用itextSharp将PDF页面转换为Image?

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

如果您可以选择在系统上安装PNG虚拟打印机,则可以将某些软件视为PDFCreator(也可以打印到PNG)或类似的东西。

考虑使用powertools动态转换docx到html(甚至使用Office VSTO,它会很快),然后使用wkhtmltopdf(直接或使用pechkin或类似)从html渲染png。 我写过为什么wkhtmltopdf比ex更好。 iTextSharp 在这里 。 顺便说一句,我认为使用doc / docx的最好的商业库是TxText – 它真的很棒,你可以做任何你想做的事情。