FROM#. Convert documents to PDF

I need to convert the following file formats to pdf using C # / VB.Net. The user will upload the file using the FileUpload control and the system will return the PDF file after converting the document.

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

Does ITextSharp provide such a facility? Please list only open source or free libraries.

thanks

+2


a source to share


4 answers


I did the same for Word format only. I am using Microsoft.Office.Interop.Word

below 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.

+1


a source


I have never seen any free libraries for converting office documents to pdf. However, there are free PDF printer drivers like PDFCreator

http://sourceforge.net/projects/pdfcreator/files/ , so maybe you can install one of these and then just the app will automate printing documents to one of those PDFs. printers.



Perhaps this is a possible solution if it's just a small intranet site or similar.

+1


a source


I don't know of any free libraries as this is an extremely complex field. Some commercial ones are available, but usually they are not fidelity when converting files.

I have been working on a web service based PDF converter that works well but is not free.

0


a source


There are several open source libraries that handle PDF. However, I'm not sure if you will make the conversion for you between formats.

You will probably need two libraries. One for reading DOC / DOCX and one for writing to PDF. Reading DOC / DOCX can be the trickier part, at least without Microsoft Word installed. If you have Word, then you have access to COM interfaces for manipulating Word documents, but obviously you have to pay for Word.

Wikipedia lists several libraries, open source and commercial, including iTextSharp that you mentioned.

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

Given that OpenOffice is open source, it might be worth looking at how they do it as they can read DOC (and DOCX?) And write to PDF.

Alternatively you can look in PDF Printer solutions. Again, I'm not sure about open-source / free solutions, but if there is one, you basically just print to a dedicated PDF printer from C # and convert it to a PDF file. Some products will also allow you to upload a file to a folder and convert it.

I've used Adobe Distiller (part of Acrobat) and ActivePDF, but these are commercial solutions. However, ActivePDF provides a library.

There is also CutePDF , which claims to be free. Haven't tried and didn't know what the limitations are on their professional version.

0


a source







All Articles