C#/VB.NET: Convert PDF to Word Doc or Docx

PDF and Word are two of the most popular file formats used in our daily work. In some cases, you may need to convert PDF documents to Word. For example, when you want to make the text in a PDF document editable and searchable. This article will illustrate how to convert PDF to Word Doc or Docx in C# and VB.NET.

Installation

Spire.PDF for .NET is used here to achieve the conversion from PDF to Word. It’s a commercial library for creating, editing, converting and printing PDF documents. A free version of Spire.PDF for .NET is also available, but it should be pointed out that it’s limited to 10 pages while loading PDF files.

You can install either of them via NuGet:

Commercial version: Install-Package Spire.PDF

Free version: Install-Package FreeSpire.PDF

Convert PDF to Word Doc in C# and VB.NET

Spire.PDF offers a PdfDocument.SaveToFile(filePath, FileFormat.DOC) method for converting PDF documents to Word Doc format. The following are the detailed steps for your reference:

  • Initialize an instance of PdfDocument instance.
  • Load a PDF document using PdfDocument.LoadFromFile(filePath) method.
  • Convert the PDF document to Word Doc format and save it to a specified path using PdfDocument.SaveToFile(filePath, FileFormat.DOC) method.

C#

using Spire.Pdf;

namespace ConvertPdfToDoc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();

            //Load the PDF document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Convert PDF to Doc and save it to a specified path
            doc.SaveToFile("ToDoc.doc", FileFormat.DOC);
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace ConvertPdfToDoc
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()

            'Load the PDF document
            doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

            'Convert PDF to Doc and save it to a specified path
            doc.SaveToFile("ToDoc.doc", FileFormat.DOC)
        End Sub
    End Class
End Namespace

The input PDF document:

The input PDF document

The converted Doc document:

Convert PDF to Word Doc using C# or VB.NET

Convert PDF to Word Docx in C# and VB.NET

You can convert PDF documents to Word Docx format using PdfDocument.SaveToFile(filePath, FileFormat.DOCX) method. The following are the detailed steps to implement this feature:

  • Initialize an instance of PdfDocument instance.
  • Load a PDF document using PdfDocument.LoadFromFile(filePath) method.
  • Convert the PDF document to Word Doc format and save it to a specified path using PdfDocument.SaveToFile(filePath, FileFormat.DOCX) method.

C#

using Spire.Pdf;

namespace ConvertPdfToDocx
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument doc = new PdfDocument();

            //Load the PDF document
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Convert PDF to Docx and save it to a specified path
            doc.SaveToFile("ToDocx.docx", FileFormat.DOCX);
        }
    }
}

VB.NET

Imports Spire.Pdf

Namespace ConvertPdfToDocx
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim doc As PdfDocument = New PdfDocument()

            'Load the PDF document
            doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

            'Convert PDF to Docx and save it to a specified path
            doc.SaveToFile("ToDocx.docx", FileFormat.DOCX)
        End Sub
    End Class
End Namespace

The converted Docx document:

Convert PDF to Word Docx using C# or VB.NET

 Conclusion

This article demonstrates how to convert PDF documents to Word Doc or Docx format in C# and VB.NET using Spire.PDF for .NET. The library also supports many other functions, such as adding digital signatures, inserting form fields, and protecting PDF documents. If you’re interested, you can try it by yourself.

C# and VB.NET – Remove Macros from Word Documents

A macro is a series of commands that can be used to automate repetitive tasks. You can create and run macros in Word documents to make your document-processing tasks more efficient. However, in some other cases, you may need to remove macros from Word documents. For example, when you need to send a Word document with macros to someone who requires a macro-free file. In this article, I will demonstrate how to remove macros from Word documents in C# and VB.NET.

Installation

To remove macros from Word documents, this article uses Free Spire.Doc for .NET. You can install it via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console, and then executing the following command:

PM> Install-Package FreeSpire.Doc

Alternatively, you can also download the API from this website and install it, then add the DLL files under the Bin folder to your project as references.

Remove Macros from a Word Document in C# and VB.NET

Free Spire.Doc provides a Document.IsContainMacro property to detect if a Word document contains macros, if the result is true, you can use the Document.ClearMacros() method to remove them. Here are the detailed steps:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Detect if the document contains macros through Document.IsContainMacro property.
  • If the result is true, remove the macros from the Word document using Document.ClearMacros() method.
  • Save the result document using Document.SaveToFile() method.

C#

using Spire.Doc;

namespace RemoveMacrosFromWord
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a Document object
            Document document = new Document();
            //Load the Word document
            document.LoadFromFile("Input.docm");

            bool hasMacros = false;
            //Detect if the document contains macros
            hasMacros = document.IsContainMacro;

            //If the result is true, remove the macros from the document
            if (hasMacros)
            {
                document.ClearMacros();
            }
            
            //Save the document
            document.SaveToFile("Output.docm", FileFormat.Docm);            
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace RemoveMacrosFromWord
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize a Document object
            Dim document As Document = New Document()
            'Load the Word document
            document.LoadFromFile("Input.docm")

            Dim hasMacros = False
            'Detect if the document contains macros
            hasMacros = document.IsContainMacro

            'If the result is true, remove the macros from the document
            If hasMacros Then
                document.ClearMacros()
            End If

            'Save the document
            document.SaveToFile("Output.docm", FileFormat.Docm)
        End Sub
    End Class
End Namespace

C#/VB.NET: Encrypt or Decrypt PDF Documents

PDF files are widely used in business because they can preserve document formatting, style, and other information when viewed on different devices. Additionally, PDF files are also ideal for sharing sensitive information because they cannot be edited easily and can also be encrypted with passwords to prevent the document information from unauthorized access. In this article, I will demonstrate how to encrypt or decrypt PDF documents in C# and VB.NET.

Installation

Spire.PDF for .NET is used here to achieve the PDF encryption and decryption. It’s a commercial library for creating, editing, converting and printing PDF documents. A free version of Spire.PDF for .NET is also available, but it should be pointed out that it’s limited to 10 pages while loading PDF files.

You can install either of them via NuGet:

Commercial version: Install-Package Spire.PDF

Free version: Install-Package FreeSpire.PDF

Encrypt PDF Documents in C# and VB.NET

A PDF document can be encrypted with two types of passwords: document open password and permissions password.

Document open password

A document open password, also known as a user password, is required to open the encrypted PDF.

Permissions password

A permissions password, also known as a master or owner password, is used to change the permission settings of the PDF. This password allows you to restrict certain features that can be performed on the PDF, such as printing, editing, and copying content.

If a PDF document is encrypted with both types of passwords, recipients can open the PDF with either password. However, if they need to change the restrictions you have set, they will need to enter the permissions password.

Spire.PDF enables developers to encrypt a PDF document with open password and permissions password by using the PdfDocument.Security.Encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize) method. The following are the detail steps to implement this feature:

  • Initialize an instance of PdfDocument class.
  • Load the PDF document using PdfDocument.LoadFromFile() method.
  • Encrypt the PDF with open and permissions passwords using PdfDocument.Security.Encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize) method.
  • Save the result file using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;
using Spire.Pdf.Security;

namespace PdfEncryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load the PDF document
            pdf.LoadFromFile(@"Sample.pdf");

            //Encrypt the PDF with open and permissions passwords
            pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit);

            //Save the result file
            pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF);
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Security

Namespace PdfEncryption
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()

            'Load the PDF document
            pdf.LoadFromFile("Sample.pdf")

            'Encrypt the PDF with open and permissions passwords
            pdf.Security.Encrypt("open", "permission", PdfPermissionsFlags.Print Or PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit)

            'Save the result file
            pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Decrypt PDF Documents in C# and VB.NET

You can decrypt an encrypted PDF document by setting its open password and permissions password as empty using the PdfDocument.getSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize, originalPermissionPassword) method. The following are the detail steps to do so:

  • Initialize an instance of PdfDocument class.
  • Load the encrypted PDF with password using PdfDocument.LoadFromFile(filePath, password) method.
  • Decrypt the document by setting the open and permissions passwords as empty using PdfDocument.Security.Encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize, originalPermissionPassword) method.
  • Save the result file using PdfDocument.SaveToFile() method.

C#

using Spire.Pdf;
using Spire.Pdf.Security;

namespace PdfDecryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load the encrypted PDF document with password
            pdf.LoadFromFile(@"Encrypt.pdf", "open");

            //Set the open and permissions passwords as empty to decrypt the PDF
            pdf.Security.Encrypt(string.Empty, string.Empty, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permission");

            //Save the result document
            pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF);
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Security

Namespace PdfDecryption
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument instance
            Dim pdf As PdfDocument = New PdfDocument()

            'Load the encrypted PDF document with password
            pdf.LoadFromFile("Encrypt.pdf", "open")

            'Set the open and permissions passwords as empty to decrypt the PDF
            pdf.Security.Encrypt(String.Empty, String.Empty, PdfPermissionsFlags.[Default], PdfEncryptionKeySize.Key128Bit, "permission")

            'Save the result document
            pdf.SaveToFile("Decrypt.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Java: Convert Docx to Doc or Doc to Docx

Nowadays, Docx has become the most commonly used Microsoft Word file format in our daily life. However, for those people who are still using the very old versions of Microsoft Office, such as Office 2003, Docx files might cause trouble because Office 2003 cannot open files with the .docx extension. In this case, it may be necessary to convert between Docx and Doc file formats. This article will introduce how to convert Docx to Doc or Doc to Docx using Java.

Add Dependencies

This article uses Spire.Doc for Java to achieve the conversions between Docx and Doc. Before coding, you need to add needed dependencies for including Spire.Doc for Java into your Java project. There are two ways to do that.

Method 1: If you are using maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>10.6.6</version>
    </dependency>
</dependencies>

Method 2: If you are not using maven, you can download the JAR file from this link, extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.

Convert Docx to Doc using Java

You can convert a Docx file to Doc easily by using the Document.saveToFile(filePath, FileFormat.Doc) method. In addition, you are also able to convert Docx to Word 6 or Word 95 format by setting the FileFormat as Doc_Pre_97, but note that loading Word 6/95 documents is not supported at present.

The following steps show you how to convert a Docx file to Doc:

  • Initialize an instance of Document class.
  • Load the Docx file using Document.loadFromFile() method.
  • Save the file to Doc format using Document.saveToFile(filePath, FileFormat.Doc) method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertDocxToDoc {
    public static void main(String[] args){
        //Initialize an instance of Document class
        Document document = new Document();
        //Load a Docx file
        document.loadFromFile("Sample.docx");

        //Convert the Docx file to Doc
        document.saveToFile("ToDoc.doc", FileFormat.Doc);
    }
}

Convert Doc to Docx using Java

You can convert a Doc file to Docx by using the Document.saveToFile(filePath, FileFormat.Docx) method. Besides, you can also specify the FileFormat as Docx_2010, Docx_2013.

The following steps show you how to convert a Doc file to Docx:

  • Initialize an instance of Document class.
  • Load the Doc file using Document.loadFromFile() method.
  • Save the file to Doc format using Document.saveToFile(filePath, FileFormat.Docx) method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertDocToDocx {
    public static void main(String[] args){
        //Initialize an instance of Document class
        Document document = new Document();
        //Load a Doc file
        document.loadFromFile("Sample.doc");

        //Convert the Doc file to Docx
        document.saveToFile("ToDoc.docx", FileFormat.Docx);
    }
}

Conclusion

This article demonstrated how to convert Docx to Doc and Doc to Docx in Java using the Document.saveToFile() method provided by Spire.Doc. Apart from this, you can implement conversions from Doc/Docx to other Word formats, such as Docm, Dot, Dotm, and Dotx. Furthermore, the Document.saveToFile() method can also be used to convert Word documents to other file formats, such as PDF, HTML, Images, PCL, Text, OpenXml, Odt, WPS, and more.  

C# and VB.NET – Convert Docx to Doc or Doc to Docx

Both Doc and Docx are Microsoft Word file formats. Doc is used by Microsoft Word 2003 and the older versions. Docx is used by Microsoft Word 2007 and the newer versions. In some circumstances, developers may need to implement conversions between Doc and Docx. In this article, I will demonstrate how to convert Docx to Doc or Doc to Docx using C# and VB.NET.

Installation

To achieve the conversion, this article uses Spire.Doc for .NET. You can download the DLL file of the API from this website or install it via NuGet by selecting Tools > NuGet Package Manager > Package Manager Console and then executing the following command:

PM> Install-Package Spire.Doc

Convert Docx to Doc using C# and VB.NET

A Docx file can be converted to Doc easily by using the Document.SaveToFile(filePath, FileFormat.Doc) method. Besides, Spire.Doc also allows you to convert Docx to Word 6 or Word 95 format by specifying the FileFormat as DocPre97, but it should be pointed out that it does not support loading such documents.

The following steps show you how to convert a Docx file to Doc:

  • Initialize an instance of Document class.
  • Load the Docx file using Document.LoadFromFile() method.
  • Save the file to Doc format using Document.SaveToFile(filePath, FileFormat.Doc) method.

C#

using Spire.Doc;

namespace ConvertDocxToDoc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of Document class
            Document document = new Document();
            //Load a Docx file
            document.LoadFromFile(@"Sample.docx");

            //Convert the Docx file to Doc
            document.SaveToFile("ToDoc.doc", FileFormat.Doc);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace ConvertDocxToDoc
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize an instance of Document class
            Dim document As Document = New Document()
            'Load a Docx file
            document.LoadFromFile("Sample.docx")

            'Convert the Docx file to Doc
            document.SaveToFile("ToDoc.doc", FileFormat.Doc)
        End Sub
    End Class
End Namespace

Convert Doc to Docx using C# and VB.NET

You can convert a Doc file to Docx by using the Document.SaveToFile(filePath, FileFormat.Docx) method. Besides, you can also specify the FileFormat as other Docx format like Docx2010 and Docx2013.

The following are the steps to convert a Doc document to Docx:

  • Initialize an instance of Document class.
  • Load the Doc file using Document.LoadFromFile() method.
  • Save the file to Doc format using Document.SaveToFile(filePath, FileFormat.Docx) method.

C#

using Spire.Doc;

namespace ConvertDocToDocx
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of Document class
            Document document = new Document();
            //Load a Docx file
            document.LoadFromFile(@"Sample.doc");

            //Convert the Doc file to Docx
            document.SaveToFile("ToDoc.docx", FileFormat.Docx);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace ConvertDocToDocx
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Initialize an instance of Document class
            Dim document As Document = New Document()
            'Load a Docx file
            document.LoadFromFile("Sample.doc")

            'Convert the Doc file to Docx
            document.SaveToFile("ToDoc.docx", FileFormat.Docx)
        End Sub
    End Class
End Namespace

Conclusion

In this article, I have introduced how to implement the conversions from Docx to Doc and Doc to Docx in C# and VB.NET using Spire.Doc. Apart from these, you can also use the library to implement conversions between other Word formats, such as Doc or Docx to Docm, Dot, Dotm, and Dotx.

Convert Excel to CSV or CSV to Excel using Java

Excel and CSV are two of the popular file formats for storing tabular data. The difference between them is that Excel is a binary file format that holds information about all the worksheets in a workbook including both content and formatting, while CSV is a simple type of plain text file in which values are separated by commas. Sometimes, developers may need to do conversions between Excel and CSV. In this article, I will introduce how to convert Excel to CSV or CSV to Excel using Java.

Add Dependencies

To implement the conversions between Excel and CSV, this article uses Spire.XLS for Java.

You can either download the library’s jar from the official website or install it from Maven by adding the following code to your maven-based project’s pom.xml file.

<repositories>    
    <repository>    
        <id>com.e-iceblue</id>    
        <name>e-iceblue</name>    
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>    
    </repository>    
</repositories>    
<dependencies>    
    <dependency>    
        <groupId> e-iceblue </groupId>    
        <artifactId>spire.xls </artifactId>    
        <version>12.6.0</version>    
    </dependency>    
</dependencies>

Convert Excel XLS or XLSX to CSV using Java

You can convert a specific worksheet or all worksheets in an Excel workbook as CSV. The following steps show you how to convert a specific worksheet to CSV:

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.loadFromFile(filePath) method.
  • Get the desired worksheet by its index using Workbook.getWorksheets().get(sheetIndex) method.
  • Save the worksheet as CSV using Worksheet.saveToFile(filePath, separator) method.
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ConvertAWorksheetToCsv {
    public static void main(String []args){
        //Create an instance of Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Save the worksheet as CSV
        sheet.saveToFile("ExcelToCSV.csv", ",");
    }
}
Convert Excel to CSV using Java

If you want to convert all worksheets in an Excel workbook as separate CSV files, you need to loop through each worksheet in the workbook, then call Worksheet.saveToFile(string fileName, string separator) method to save the worksheet as CSV, as shown in the following example.

Convert Multiple Worksheets to Separate CSV Files using Java

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ConvertAWorksheetToCsv {
    public static void main(String []args){
        //Create an instance of Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Loop through the worksheets in the file
        for(int i = 0; i< workbook.getWorksheets().getCount(); i++) {
            Worksheet sheet = workbook.getWorksheets().get(i);
            //Save each worksheet as separate CSV
            sheet.saveToFile("ExcelToCSV_" + i + ".csv", ",");
        }
    }
}

Convert CSV to Excel XLS or XLSX using Java

A CSV file can also be converted to Excel. The following are the steps to do so:

  • Create an instance of Workbook class.
  • Load a CSV file using Workbook.loadFromFile(filePath, separator) method.
  • Loop through the worksheets in the CSV file.
  • In the loop, access the used range of the current worksheet using Worksheet.getAllocatedRange() method, and set ignore error option using CellRange.setIgnoreErrorOptions() method. Then autofit columns and rows using CellRange.autoFitColumns() and CellRange.autoFitRows() methods.
  • Save the CSV to Excel using Workbook.saveToFile(filePath, ExcelVersion) method.
import com.spire.xls.*;

import java.util.EnumSet;

public class ConvertCsvToExcel {
    public static void main(String []args) {
        //Create an instance of Workbook class
        Workbook workbook = new Workbook();
        //Load a CSV file
        workbook.loadFromFile("ExcelToCSV.csv", ",");

        //Loop through the worksheets in the CSV file
        for (int i = 0; i < workbook.getWorksheets().getCount(); i++)
        {
            Worksheet sheet = workbook.getWorksheets().get(i);
            //Access the used range in each worksheet
            CellRange usedRange = sheet.getAllocatedRange();
            //Ignore errors when saving numbers in the used range with text
            usedRange.setIgnoreErrorOptions(EnumSet.of(IgnoreErrorType.NumberAsText));
            //Autofit columns and rows
            usedRange.autoFitColumns();
            usedRange.autoFitRows();
        }

        //Save the CSV file as Excel .xlsx file
        workbook.saveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013);
    }
}
Convert CSV to Excel using Java
Design a site like this with WordPress.com
Get started