Converting a microsoft document imaging mdi file to pdf or jpg in asp net

Introduction

a Microsoft (MDI) file to PDF or JPG in ASP.NET can be a challenging task. However, with the right approach and the use of appropriate libraries, it is possible to this conversion seamlessly. In this article, we will explore methods and examples to help you solve this problem.

Method 1: Microsoft Office Interop

One way to an MDI file to PDF or JPG is by utilizing the Microsoft Office Interop library. This library allows you to interact with Microsoft Office programmatically. Here's an example of how you can use it:


using Microsoft.Office.Interop.MSProject;

public void ConvertMDIToPDF(string mdiFilePath, string outputFilePath)
{
    Application application = new Application();
    Document document = application.Documents.Open(mdiFilePath);
    document.ExportAsFixedFormat(outputFilePath, WdExportFormat.wdExportFormatPDF);
    document.Close();
    application.Quit();
}

In the above example, we create an instance of the Application class from the Microsoft.Office.Interop.MSProject namespace. We then open the MDI file using the Documents.Open method and export it as a PDF using the ExportAsFixedFormat method. Finally, we close the document and quit the application.

Method 2: Using third-party libraries

If you prefer to avoid using the Microsoft Office Interop library, you can rely on third-party libraries specifically designed for document conversion. One popular library is the iTextSharp library, which provides extensive functionality for working with PDF files. Here's an example of how you can use it:


using iTextSharp.text;
using iTextSharp.text.pdf;

public void ConvertMDIToPDF(string mdiFilePath, string outputFilePath)
{
    using (var mdiStream = new FileStream(mdiFilePath, FileMode.Open))
    {
        var document = new Document();
        var writer = PdfWriter.GetInstance(document, new FileStream(outputFilePath, FileMode.Create));
        document.Open();
        var mdiImage = Image.GetInstance(mdiStream);
        document.Add(mdiImage);
        document.Close();
    }
}

In this example, we use the iTextSharp library to create a new PDF document. We open the MDI file as a stream and create an instance of the Document class. We then create a PdfWriter instance to write the PDF to the output file. Next, we open the document, add the MDI image to it, and finally, close the document.

Conclusion

Converting a Microsoft Document Imaging (MDI) file to PDF or JPG in ASP.NET can be achieved using different methods. In this article, we explored two approaches: using the Microsoft Office Interop library and using the iTextSharp library. Both methods provide effective ways to convert MDI files to desired formats. Choose the method that best suits your and integrate it into your ASP.NET application to solve this problem.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents