Save PDF and MS Word File in C#

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.


Similar Articles