Set output format to DOCX or RTF in C# and .NET
Processing PDF files and converting them to editable formats is a common task in C# and .NET application development. It's often necessary to convert PDF documents to Microsoft Office formats such as DOCX or RTF for editing, sharing, or integration with other systems. In this article, we'll explore what conversion to DOCX or RTF is, the benefits of customizing the output format, and a practical implementation example using the powerful SautinSoft's library from PDF Focus .NET.
Converting a PDF to DOCX or RTF is the process of converting content from the fixed PDF format to a more flexible text document format that can be easily edited in standard Word or other applications. Simply put, you can "parse" the content of a PDF file and "rebuild" it into a format more suitable for editing and formatting.
Format changer supports various scenarios:
- Editing and collaboration: Exporting PDF to Word allows for easy changes.
- Document automation: Implement automatic conversions from PDF to DOCX/RTF for bulk processing.
- Business process integration: Use in CRM, ERP, and reporting systems.
- Creation of Lego data models: For analyzing and further processing PDF data.
Why is it important to configure the output format?
Proper configuration optimizes document workflows, speeds up editing, and facilitates integration into business processes. For example, DOCX is preferable
for preparing presentations or reports, while RTF is suitable for simpler tasks with limited formatting requirements.
Configuring the output format in your conversion code gives you flexibility and control over the result, improving application performance.
Complete code
using System;
using System.IO;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
ConvertPdfToDocx();
//ConvertPdfToRtf();
}
private static void ConvertPdfToDocx()
{
// Before starting, we recommend to get a free key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// SautinSoft.PdfFocus.SetLicense("...");
string pdfFile = Path.GetFullPath(@"..\..\..\text and graphics.pdf");
string wordFile = "Result.docx";
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
f.OpenPdf(pdfFile);
if (f.PageCount > 0)
{
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Docx;
int result = f.ToWord(wordFile);
// Show the produced result.
if (result == 0)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(wordFile) { UseShellExecute = true });
}
}
}
private static void ConvertPdfToRtf()
{
string pdfFile = Path.GetFullPath(@"..\..\..\text and graphics.pdf");
string wordFile = "Result.rtf";
// Get your free key here:
// https://sautinsoft.com/start-for-free/
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
f.OpenPdf(pdfFile);
if (f.PageCount > 0)
{
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Rtf;
int result = f.ToWord(wordFile);
// Show the produced result.
if (result == 0)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(wordFile) { UseShellExecute = true });
}
}
}
}
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft
Module Sample
Sub Main()
' Before starting, we recommend to get a free key:
' https://sautinsoft.com/start-for-free/
' Apply the key here
' SautinSoft.PdfFocus.SetLicense("...");
ConvertPdfToDocx()
'ConvertPdfToRtf()
End Sub
Private Sub ConvertPdfToDocx()
Dim pdfFile As String = Path.GetFullPath("..\..\..\text and graphics.pdf")
Dim wordFile As String = "Result.docx"
Dim f As New SautinSoft.PdfFocus()
f.OpenPdf(pdfFile)
If f.PageCount > 0 Then
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Docx
Dim result As Integer = f.ToWord(wordFile)
' Show the produced result.
If result = 0 Then
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(wordFile) With {.UseShellExecute = True})
End If
End If
End Sub
Private Sub ConvertPdfToRtf()
Dim pdfFile As String = Path.GetFullPath("..\..\..\text and graphics.pdf")
Dim wordFile As String = "Result.rtf"
Dim f As New SautinSoft.PdfFocus()
f.OpenPdf(pdfFile)
If f.PageCount > 0 Then
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Rtf
Dim result As Integer = f.ToWord(wordFile)
' Show the produced result.
If result = 0 Then
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(wordFile) With {.UseShellExecute = True})
End If
End If
End Sub
End Module
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: