views:

278

answers:

5

Hi.

I need to convert below mentioned file formats to pdf using C#/VB.Net. User will upload the file using FileUpload control and system will returns the pdf file after converting the document.

doc/docx to pdf xls/xlsx to pdf ppt/pps to pdf

Does ITextSharp provide such facility ? Please Only mentioned open source or free libraries.

Thanks

+1  A: 

I've never seen any free libraries to convert office docs to pdf. However, there are free PDF printer drivers, like PDFCreator http://sourceforge.net/projects/pdfcreator/files/, so possibly you could install one of those and then just have your app automate the printing of the documents to one of those pdf printers.

Might be a possible solution if it's just a small intranet site or similar.

ho1
A: 

I am not aware of any free libraries as this is an extremely complicated field. Some commercial ones are available, but they generally lack fidelity when converting files.

I have worked on a Web Services based PDF Converter, which works well but it is not free.

Muhimbi
A: 

There are a number of open source libraries that handle PDF. However I'm not certain any will do the conversion for you between the formats.

You will probably need two libraries. One to read DOC/DOCX and one to write out to PDF. Reading DOC/DOCX might actually be the more difficult part, at least without installing Microsoft Word. If you have Word then you have access to the COM interfaces to manipulate Word documents, but obviously you have to pay for Word.

Wikipedia list a number of libraries, open source and commercial, including iTextSharp you mentioned.

http://en.wikipedia.org/wiki/List_of_PDF_software

Given OpenOffice is open source, it may be worth looking into how they do it as they can read DOC (and DOCX?) and write out to PDF.

Alternatively, you could look into PDF Printer solutions. Again, I'm not certain of open source / free solutions, but if there is one, you'd basically just print to a special PDF Printer from C# and it would convert to a PDF file. Some products will also just let you dump a file into a folder and it converts it.

I have used Adobe Distiller (part of Acrobat) and ActivePDF, but these are commercial solutions. ActivePDF do provide a library though.

There is also CutePDF which claims to be free. Have not tried it and not sure what the limitations are over their professional version.

tjmoore
+1  A: 

I did the same thing only for Word format. i use Microsoft.Office.Interop.Word

below the code.

public static void Word2PDF(string fileName)
{
    ApplicationClass wordApplication = new ApplicationClass();
    Document wordDocument = null;
    object paramSourceDocPath = fileName + @".doc";
    object paramMissing = Type.Missing;
    string paramExportFilePath = fileName + @".pdf";
    WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
    bool paramOpenAfterExport = false;

    WdExportOptimizeFor paramExportOptimizeFor =WdExportOptimizeFor.wdExportOptimizeForPrint;

    WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
    int paramStartPage = 0;

    int paramEndPage = 0;
    WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

    bool paramIncludeDocProps = true;
    bool paramKeepIRM = true;

    WdExportCreateBookmarks paramCreateBookmarks =
    WdExportCreateBookmarks.wdExportCreateWordBookmarks;

    bool paramDocStructureTags = true;
    bool paramBitmapMissingFonts = true;

    bool paramUseISO19005_1 = false;

    try
    {
        // Open the source document.
        wordDocument = wordApplication.Documents.Open(
        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
        ref paramMissing, ref paramMissing, ref paramMissing,
        ref paramMissing, ref paramMissing, ref paramMissing,
        ref paramMissing, ref paramMissing, ref paramMissing,
        ref paramMissing, ref paramMissing, ref paramMissing,
        ref paramMissing);

        // Export it in the specified format.
        if (wordDocument != null)
            wordDocument.ExportAsFixedFormat(paramExportFilePath,
            paramExportFormat, paramOpenAfterExport,
            paramExportOptimizeFor, paramExportRange, paramStartPage,
            paramEndPage, paramExportItem, paramIncludeDocProps,
            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
            paramBitmapMissingFonts, paramUseISO19005_1,
            ref paramMissing);
    }
    catch (Exception ex)
    {

        // Respond to the error
    }
    finally
    {
        // Close and release the Document object.
        if (wordDocument != null)
        {
            wordDocument.Close(ref paramMissing, ref paramMissing,
            ref paramMissing);
            wordDocument = null;
        }
        // Quit Word and release the ApplicationClass object.
        if (wordApplication != null)
        {
            wordApplication.Quit(ref paramMissing, ref paramMissing,
            ref paramMissing);
            wordApplication = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

Hope this helps.

Note: i am using version 12, means office 2007.

Sakhawat Ali
A: 

I used pdftron.PDF.ToPDF (or ToXPS()) to convert Word/MS Office to PDF in my web app. based on Convert sample (http://www.pdftron.com/pdfnet/samplecode.html#Convert)

Ivan