You must Sign In to post a response.
  • Category: ASP.NET

    How to programmatically convert word,image files to pdf in c# & save by its filename?


    Are you looking for a way to convrt word to pdf programtically ? then read this thread to know more about it



    Hi,
    I want to convert and save any file like .doc,.docx,.jpeg,.bmp files in pdf by its own file name.

    First I want to convert files to pdf only through c# programming not using any .dll files.
    I tried using Interop.Word to convert to pdf, but it does not work in .Net 2003.

    When saving pdf converted files, I want to save file by its old file name.

    How to do this?
  • #735249
    If you are using word 2007 and above then microsoft provide you a facility/addin to convert word to pdf.
    using 'Microsoft.Office.Interop.Word' namespace
    see below code snippet

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    ...

    // Create a new Microsoft Word application object
    Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();

    // C# doesn't have optional arguments so we'll need a dummy value
    object objMissing = System.Reflection.Missing.Value;

    // Get list of Word files in specified directory
    DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
    FileInfo[] arrFiles = dirInfo.GetFiles("*.doc");

    objWordApp.Visible = false;
    objWordApp.ScreenUpdating = false;

    foreach (FileInfo objFile in arrFiles)
    {
    // Cast as Object for word Open method
    Object filename = (Object)objFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    Document objDoc = objWordApp.Documents.Open(ref filename, ref objMissing,
    ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
    ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
    ref objMissing, ref objMissing, ref objMissing, ref objMissing);
    objDoc.Activate();

    object szResultFile = objFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // Save document into PDF Format
    objDoc.SaveAs(ref szResultFile,
    ref fileFormat, ref objMissing, ref objMissing,
    ref objMissing, ref objMissing, ref objMissing, ref objMissing,
    ref objMissing, ref objMissing, ref objMissing, ref objMissing,
    ref objMissing, ref objMissing, ref objMissing, ref objMissing);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)objDoc).Close(ref saveChanges, ref objMissing, ref objMissing);
    objDoc = null;
    }

    // word has to be cast to type _Application so that it will find
    // the correct Quit method.
    ((_Application)word).Quit(ref objMissing, ref objMissing, ref objMissing);
    objWordApp = null;

    before running above code you need to install an addin provided by microsoft. see below link
    http://www.microsoft.com/en-in/download/details.aspx?id=9943

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #735644
    Hi,

    In order to convert docx file to PDF you can use below code snippet.
        public partial class WebForm1 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Microsoft.Office.Interop.Word.Application MyWordFile = new Microsoft.Office.Interop.Word.Application();
    wordDocument = MyWordFile.Documents.Open(@"D:\TestFile.docx");
    wordDocument.ExportAsFixedFormat(@"D:\TestPDF.pdf", WdExportFormat.wdExportFormatPDF);
    }
    public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }

    }

    For converting image to PDF use below code snippet
            void ConvertImageToPDF(Image imaInput, string Filename)
    {
    PdfDocument docTest = new PdfDocument();
    PdfPage page = docTest.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);
    XImage ximage = XImage.FromGdiPlusImage(imaInput);
    gfx.DrawImage(ximage, 0, 0);
    docTest.Save(Filename);
    }


    Best Regards
    Bashar

  • #737346
    Converting different formats, especially images, to PDF can have 2 meanings: either produce an image-based PDF where the pages only contain bitmaps, or produce text-based PDF where the original images are recognized for text and the text is saved in the resulting PDF.
    I worked on an application that used the first approach where we took many different types of input files, converted them all to images, and saved them as image-based PDF files for archiving. We used a commercial library named Leadtools because it handled the different formats such as Doc, Docx, JPEG, etc all the same way. The same library had an option for producing text PDF using OCR, but we did not need it so we did not buy that option.

    I imagine that converting JPEG, PNG and BMP files won't be that much of a hassle for you because there lots of articles and free libraries that you can use to do this but doing so for DOC and DOCX files will need some work.

    Also you mentioned .NET 2003. If you mean Visual Studio 2003, I don't know if you'll find many options to work with, since it's way old and new libraries are not likely to support it. My own project was done using VS2008, and that was 3 years ago, so good luck.

  • #743400
    You can convert any MS office file or image file to pdf by using Aspose's .NET Component. Try it and its code samples to know more about conversion.

    http://www.aspose.com/.net/word-component.aspx

  • #762477
    You could also try Spire to make the file conversion between Doc, PDF and Image. Sample code

    //Load Document
    Document document = new Document();

    document.LoadFromFile(@"E:\work\documents\Sample.docx");

    //Convert Word to PDF
    document.SaveToFile("toPDF.PDF", FileFormat.PDF);


  • Sign In to post your comments