Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

C#.

NET CODE SNIPPETS FOR FILE CONVERSION IN DIFFERENET FORMAT

CONVERT WORD FILE(.DOCX FILE) TO TEXT FILE(.TXT FILE) IN C#


Here I will discuss how we can convert a word file to a text file in C#
step by step:

1) Go to add reference tab and add spire.doc dll, in your project.

2) write the namespace at the top like-


     using Spire.Doc;
     using Spire.Doc.Documents;

3) now write the code to convert a .docx file to .txt file -


                      Document doc = new Document();
                     doc.LoadFromFile("filename.docx");
                    //doc.LoadFromFile(textBox2.Text, FileFormat.Docx);

                    StringBuilder sb = new StringBuilder();

                    foreach (Section section in doc.Sections)


          {
                        foreach (Paragraph paragraph in section.Paragraphs)
            {
                            sb.AppendLine(paragraph.Text);
            }
          }

                    System.IO.File.WriteAllText("filename.txt", sb.ToString());

CONVERT PDF FILE TO TEXT FILE IN C#


Here I will discuss how we can convert a pdf file to text file:

1) Go to add reference tab and add following dll files-


     pdf.dll
     pdfbob-1.7.0.dll
     fontbox-1.7.0.dll
     IKVM.openJDK.Core.dll
     IKVM.openJDK.Text.dll
     IKVM.openJDK.Util.dll
    You can dowload these dll files.

2) Use namespace like -


     using org.apache.pdfbox.pdmodel;
     using org.apache.pdfbox.util;

3) Now write the code to convert a pdf file to text file -


                PDDocument doc = PDDocument.load("filename.pdf");
                PDFTextStripper stripper = new PDFTextStripper();
                string text = stripper.getText(doc);

                if (System.IO.File.Exists("filename.txt"))
        {
                    //  MessageBox.Show("filename.txt is already exist");
                        
        }
                else
        {
                    var file = System.IO.File.Create("filename.txt");

                    var writer = new System.IO.StreamWriter(file);


                    writer.WriteLine(text);

CONVERT MICROSOFT EXCEL FILE(.XLSX) TO TEXT FILE(.TXT FILE) IN C#


Here I will discuss how we can convert a excel file to text file:

1) Go to add reference tab and add following dll files-


    Spire.XLS

2) Use namespace like -


     using Spire.Xls;

3) Now write the code to convert a excel file to text file -


             
                    Workbook wb = new Workbook();
                    wb.LoadFromFile("filename.xlsx", ExcelVersion.Version2007);
                    Worksheet ws = wb.Worksheets[0];
                    ws.SaveToFile("filename.txt", ",");
                     System.Diagnostics.Process.Start("filename.txt");
                     MessageBox.Show(".Xlsx file has been converted to .txt file");
        }

You might also like