Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DOC / DOCX to PNG [closed]

I am trying to create a web service that will convert a doc/docx to png format.

The problem I seem to have is I can't find any library or something close to it that will do what I need, considering I am looking for something free and not Office dependent (the server where the app will run does not have Office installed).

Is there anything that can help me in obtaining this? Or must I choose between using something office dependant (like Interop - which btw I read is really bad to be used on server) or something that isn't free?

Thanks

like image 548
relysis Avatar asked Oct 19 '15 14:10

relysis


People also ask

Can you convert DOCX to PNG?

CloudConvert is an online document converter. Amongst many others, we support PDF, DOCX, PPTX, XLSX. Thanks to our advanced conversion technology the quality of the output will be as good as if the file was saved through the latest Microsoft Office 2019 suite.

Can you save a Word DOC as a PNG file?

Microsoft Word itself has a function that will allow you to do so. Versions 2007 and better allow you to convert documents into images using "paste special," and you will have the option to convert into jpeg, jpg, png, and other formats of your choice.


2 Answers

I know this is most likely not what you want, since it is not free.

But Aspose can do what you need.

Spire.doc too. Again, not free.

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;
        }
    }
}
like image 161
Gertsen Avatar answered Sep 22 '22 16:09

Gertsen


Yes, such complex file types conversions are usually well implemented in the specialized / 3-rd party libraries (like in the aforementioned one), or, for example, in the DevExpress Document Automation:

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);
}
like image 45
Mikhail Avatar answered Sep 20 '22 16:09

Mikhail