0% found this document useful (0 votes)
49 views3 pages

C#Code Snippet For File Conversion

This document provides code snippets for converting files between different formats in C#, including: 1) Converting a Word (.docx) file to a text (.txt) file using the Spire.Doc library. 2) Converting a PDF file to text using the PDFBox library. 3) Converting an Excel (.xlsx) file to text using the Spire.Xls library.

Uploaded by

Nipun Dutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views3 pages

C#Code Snippet For File Conversion

This document provides code snippets for converting files between different formats in C#, including: 1) Converting a Word (.docx) file to a text (.txt) file using the Spire.Doc library. 2) Converting a PDF file to text using the PDFBox library. 3) Converting an Excel (.xlsx) file to text using the Spire.Xls library.

Uploaded by

Nipun Dutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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