ARTICLE

Save PDF and MS Word File in C#

Posted by Hussain Munaf Articles | C# Language March 21, 2011
In this article I will tell you how to create a PDF file, Microsoft Word file and a text file from C# with a complete sample that uses these tools with C#.
Reader Level:
Download Files:
 


In this article I will tell you how to create a PDF file, Microsoft Word file and a text file from C# with a complete sample that uses these tools with C#.

  1. RichTextBox
  2. Button name is (Save TextFile), (Save Database), (SavePDF File), (Save MsWordFile)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using word= Microsoft.Office.Interop.Word;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using System.Reflection;

namespace ConvertPDFandWord
{
    public partial class ConverPDFandWord : Form
    {
        static string QueryString = @"Data Source=HUSSAIN-PC\HUSSAIN;Initial Catalog=TestDataBase;Integrated Security=True";
        SqlConnection con = new SqlConnection(QueryString);
        SqlCommand cmd = new SqlCommand();
        SaveFileDialog sfd = new SaveFileDialog();

        public ConverPDFandWord()
        {
            InitializeComponent();
        }

        private void btnSaveDB_Click(object sender, EventArgs e)
        {
            string Insertquery = "Insert into savedatabase (data)values('"+rtb.Text+"')";
            SqlCommand cmd = new SqlCommand(Insertquery,con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("SaveDataBase"+ex.Message+ex.StackTrace);
            }
        }

        private void btnSavePDF_Click(object sender, EventArgs e)
        {

            sfd.Title = "Save As PDF";
            sfd.Filter = "(*.pdf)|*.pdf";
            sfd.InitialDirectory = @"C:\";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                iTextSharp.text.Document doc = new iTextSharp.text.Document();
                PdfWriter.GetInstance(doc,new FileStream(sfd.FileName,FileMode.Create));
                doc.Open();
                doc.Add(new iTextSharp.text.Paragraph(rtb.Text));
                doc.Close();           
            }
        }
 
        private void btnSaveWord_Click(object sender, EventArgs e)
        {
            sfd.Title = "Save As MsWord";
            sfd.Filter = "WordDocument|*.docx";
            //sfd.DefaultExt = "docx";
            sfd.InitialDirectory = @"C:\";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                rtb.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
            }
 
        }

        private void btnTextFile_Click(object sender, EventArgs e)|
        {
            sfd.Title = "Save As Textfile";
            sfd.InitialDirectory = @"C:\";
            sfd.Filter = "TextDocuments|*.txt";
           
//sfd.DefaultExt = "txt";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                rtb.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);         
            }
        }
    }
}


I Hope you understand the save file operation in a C#.Net Windows application.

If any kind of problem send your comments.

Login to add your contents and source code to this article
Article Extensions
Contents added by Marrina vincent on Jan 28, 2013
i try the above code and while trying to open the saved file i got an error as "The office open XML file check cannot be opend, because there are some problem with the content
post comment
     

how to save textbox1 text save in .txt file in c Drives in window application with c#.net

Posted by sanjay thakur Apr 18, 2012

yes give me some day i will

Posted by Hussain Munaf Jul 07, 2011

Hussain Munaf , can u send me a project in asp.net in which it converts word file to pdf file through codings

Posted by Gowtham manju May 12, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts