.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

pdf converter in asp.net

Posted By:Gowthammanju       Posted Date: June 08, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article helps you to convert the word ,excel and image files to pdf. When user uploads the document it converts to pdf and saves to our hard disk



These article converts the document,excel,image forms to pdf forms using Microsoft office



1)      First u need to have ms-office 2003 or 2007  version installed in the system

2)      U can perform by

                a)word document: using Microsoft.Office.Interop.word

          b)Image file :using iTextSharp.text

          c)Excel file: using Microsoft.Office.Interop.excel

   3) add those reference in to ur project by right clicking and choose add reference tab in that choose the .net tab and select Microsoft.Office.Interop.word and Microsoft.Office.Interop.excel in it.

 4)for iTextSharp.text u need to choose browse tab in add reference and select the itextsharp.dll in it. these dll is being attached in the source file.

5)For word file code as

    private Microsoft.Office.Interop.Word.ApplicationClass MSWordDoc;
    object UnknownType = Type.Missing;
	public string Word2PdfCOnversion(object InputLocation,object OutputLocation)
        string result;
            if (MSWordDoc == null) MSWordDoc = new Microsoft.Office.Interop.Word.ApplicationClass();
            MSWordDoc.Visible = false;
            MSWordDoc.Documents.Open(ref InputLocation,    //Input File Name Location
                ref UnknownType,    // Conversion Conformation
                ref UnknownType,    // Set ReadOnly Property
                ref UnknownType,    // Add to the Recent Files
                ref UnknownType,    // Document Password Setting
                ref UnknownType,    // Password Templete
                ref UnknownType,    // Revert
                ref UnknownType,    // Write Password to Document
                ref UnknownType,    // Write Password Template
                ref UnknownType,    // File Format
                ref UnknownType,    // Encoding File
                ref UnknownType,    // Visibility
                ref UnknownType,    // To Open or Repair
                ref UnknownType,    // Document Direction
                ref UnknownType,    // Encoding Dialog
                ref UnknownType);   // XML Text Transform
            MSWordDoc.Application.Visible = false;  // To Invisible the Word Document
            MSWordDoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;    // Minimize the Opened Word File.
            object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
            MSWordDoc.ActiveDocument.SaveAs(ref OutputLocation, //Output File Location
            ref SavePDFFormat,    // File Format
            ref UnknownType,    // Comment to PDF File
            ref UnknownType,    // Password
            ref UnknownType,    // Add to Recent File
            ref UnknownType,    // Write Password
            ref UnknownType,    // ReadOnly Propert
            ref UnknownType,    // Original Font Embeding
            ref UnknownType,    // Save Picture
            ref UnknownType,    // Saving Form Datas
            ref UnknownType,    // Save as AOVE Letter
            ref UnknownType,    // Encoding
            ref UnknownType,    // Inserting Line Breakes
            ref UnknownType,    // Allow Substitution
            ref UnknownType,    // Line Ending
            ref UnknownType);   // Add BiDi Marks
            result = "Success";
        catch (Exception )
            result = "Error";
            if (MSWordDoc == null) { }
            else { MSWordDoc.Documents.Close(ref UnknownType, ref UnknownType, ref UnknownType); }
            MSWordDoc.Quit(ref UnknownType, ref UnknownType, ref UnknownType);

} return result; }

6)For image file code as

            using (System.IO.FileStream fs = new FileStream(Server.MapPath("~/imgfile") + "\\" + "letrouters.pdf", FileMode.Create))
                Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
                PdfWriter writer = PdfWriter.GetInstance(document, fs);

                // Open the document to enable you to write to the document

                // Makes it possible to add text to a specific place in the document using 
                // a X & Y placement syntax.
                PdfContentByte cb = writer.DirectContent;

                // Add an image to a fixed position from disk
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Temps/" + from));
                png.SetAbsolutePosition(20, 20);

                // Draw a rectangle
                //cb.Rectangle(350, 550, 150, 150);

                // Close the document

                lblMsg.Text = "Document saved to the pdf folder.";
        catch (Exception rror)
            lblMsg.Text = rror.Message;

7)for excel file code as

    public string ExpExcelToPDF( object src,object desc)
        string res;
        Microsoft.Office.Interop.Excel.Application myExcelApp;
        Microsoft.Office.Interop.Excel.Workbooks myExcelWorkbooks = null;
        Microsoft.Office.Interop.Excel.Workbook myExcelWorkbook = null;
            object misValue = System.Reflection.Missing.Value;
            myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            myExcelApp.Visible = false;
            object varMissing = Type.Missing;
            myExcelWorkbooks = myExcelApp.Workbooks;
            //if file already exist then delete the file
            if (System.IO.File.Exists(desc.ToString()))
            myExcelWorkbook = myExcelWorkbooks.Open(src.ToString(), misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
                desc, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,
                varMissing, false, varMissing, varMissing, false, varMissing);
            res = "Success";
            res = "error";
            myExcelApp = null;
        return res;
For saving in to the hard disk used code as

 if (fu_file.HasFile)
                string FileExtension = Path.GetExtension(fu_file.FileName);
                if (FileExtension == ".doc" || FileExtension == ".docx" || FileExtension == ".rtf" || FileExtension == ".txt")
                    fu_file.SaveAs(Server.MapPath("~/Temps/" + fu_file.FileName));
                    object FromLocation = Server.MapPath("~/Temps/" + fu_file.FileName);
                    string ChangeExtension = fu_file.FileName.Replace(FileExtension, ".pdf");
                    object ToLocation = Server.MapPath("~/PDFFile/" + ChangeExtension);
                    string ConvertResult = word2pdf.Word2PdfCOnversion(FromLocation, ToLocation);
                    if (ConvertResult == "Success")
                    { lb_download.Text = ChangeExtension; lbl_output.Text = "Click Here To Download File : "; }
                    else { lbl_output.Text = ConvertResult; }
                    File.Delete(Server.MapPath("~/Temps/" + fu_file.FileName));
                { lbl_output.Text = "Invalid File"; }
            else { lbl_output.Text = "No File is Selected to Convert"; }
        catch (Exception ex) { lbl_output.Text = ex.Message; }

You will get out put as

on clicking the above file name there by it opens like below


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend