Sunday, July 22, 2012

Excel to PDF in C# Win Form


These link have important information about this topic:

http://stackoverflow.com/questions/769246/xls-to-pdf-conversion-inside-net

http://stackoverflow.com/questions/5499562/excel-to-pdf-c-sharp-library

http://tinyway.wordpress.com/2011/03/30/how-to-convert-office-documents-to-pdf-using-open-office-in-c/


Using Open Office:-

1)Install the Open Office and Open Office SDK
2)Add references in the project i.e, Add following dll's from OpenOffice SDK file:
cli_basetypes.dll
cli_cppuhelper.dll
cli_oootypes.dll
cli_ure.dll
cli_uretypes.dll 


Copy and Paste the below code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using uno.util;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.util;
using System.Diagnostics;
namespace PDFGenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string excelFile = "file:///C:/244.xls";
            string pdfFile = "file:///C:/244.pdf";

            int NewOpenOfficeID = 0;
            int PreviousTotalOpenOfficeID = 0;
            int NextTotalOpenOfficeID = 0;
            foreach (Process p in Process.GetProcessesByName("soffice.bin"))
                PreviousTotalOpenOfficeID = PreviousTotalOpenOfficeID + p.Id;
            if (PreviousTotalOpenOfficeID > 0)
            {
                MessageBox.Show("Please Close the Open Office before going to further","Mesaage");
                return;
            }
            else
            {
                // Start OpenOffce or get a reference to an existing session
                XComponentContext localContext = Bootstrap.bootstrap();
                XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory)localContext.getServiceManager();
                XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");

                foreach (Process p in Process.GetProcessesByName("soffice.bin"))
                    NextTotalOpenOfficeID = NextTotalOpenOfficeID + p.Id;

                NewOpenOfficeID = NextTotalOpenOfficeID - PreviousTotalOpenOfficeID;

                // Open file hidden in read-only mode
                PropertyValue[] loadProps = new PropertyValue[2];
                loadProps[0] = new PropertyValue();
                loadProps[0].Name = "ReadOnly";
                loadProps[0].Value = new uno.Any(true);
                loadProps[1] = new PropertyValue();
                loadProps[1].Name = "Hidden";
                loadProps[1].Value = new uno.Any(true);

                // Open the file
                XComponent sourceDoc = componentLoader.loadComponentFromURL(excelFile, "_blank", 0, loadProps);

                // Conversion parameters - overwrite existing file, use PDF exporter
                PropertyValue[] conversionProperties = new PropertyValue[3];
                conversionProperties[0] = new PropertyValue();
                conversionProperties[0].Name = "Overwrite";
                conversionProperties[0].Value = new uno.Any(true);
                conversionProperties[1] = new PropertyValue();
                conversionProperties[1].Name = "FilterName";
                conversionProperties[1].Value = new uno.Any("calc_pdf_Export");

                // Set PDF export parameters
                PropertyValue[] filterData = new PropertyValue[3];

                // JPEG compression quality 70
                filterData[0] = new PropertyValue();
                filterData[0].Name = "Quality";
                filterData[0].Value = new uno.Any(70);
                filterData[0].State = PropertyState.DIRECT_VALUE;

                // Max image resolution 300dpi
                filterData[1] = new PropertyValue();
                filterData[1].Name = "ReduceImageResolution";
                filterData[1].Value = new uno.Any(true);
                filterData[1].State = PropertyState.DIRECT_VALUE;
                filterData[2] = new PropertyValue();
                filterData[2].Name = "MaxImageResolution";
                filterData[2].Value = new uno.Any(300);
                filterData[2].State = PropertyState.DIRECT_VALUE;

                conversionProperties[2] = new PropertyValue();
                conversionProperties[2].Name = "FilterData";
                conversionProperties[2].Value = new uno.Any(filterData.GetType(), filterData);

                // Save as PDF
                XStorable xstorable = (XStorable)sourceDoc;
                xstorable.storeToURL(pdfFile, conversionProperties);

                // Close document
                ((XCloseable)sourceDoc).close(false);
                ((XCloseable)xstorable).close(false);

                Process pro = Process.GetProcessById(NewOpenOfficeID);
                pro.Kill();
            }

        }
    }
}