PdfFocusToWord(Stream, Int32, Int32) Method |
Saves a specific PDF page or diapason of pages to a Word (Docx or RTF) document as a Stream object.
Namespace: SautinSoftAssembly: SautinSoft.PdfFocus (in SautinSoft.PdfFocus.dll) Version: 2024.8.6
Syntax public int ToWord(
Stream stream,
int fromPage,
int toPage
)
Public Function ToWord (
stream As Stream,
fromPage As Integer,
toPage As Integer
) As Integer
Parameters
- stream Stream
- Stream object to save a Word document. Stream must be not null.
- fromPage Int32
- The starting page for exporting to Word
- toPage Int32
- The ending page for exporting to Word
Return Value
Int32
0 - saving successfully.
2 - problem with parsing of PDF document. Please email this PDF document at
support@sautinsoft.com.
3 - problem with rendering of Word (Docx or RTF) document. Please email this PDF document at
support@sautinsoft.com.
Example How to convert PDF to Word in memory using C#
using System;
using System.IO;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
ConvertPdfToDocxBytes();
}
private static void ConvertPdfToDocxBytes()
{
string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");
byte[] pdf = File.ReadAllBytes(pdfFile);
byte[] docx = null;
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
f.OpenPdf(pdf);
if (f.PageCount > 0)
{
docx = f.ToWord();
if (docx != null)
{
string wordFile = "Result.docx";
File.WriteAllBytes(wordFile, docx);
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(wordFile) { UseShellExecute = true });
}
}
}
private static void ConvertPdfToRtfStream()
{
string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");
MemoryStream rtfStream = new MemoryStream();
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
using (FileStream pdfStream = new FileStream(pdfFile, FileMode.Open, FileAccess.Read))
{
f.OpenPdf(pdfStream);
if (f.PageCount > 0)
{
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Rtf;
int res = f.ToWord(rtfStream);
if (res == 0)
{
string rtfFile = "Result.rtf";
File.WriteAllBytes(rtfFile, rtfStream.ToArray());
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(rtfFile) { UseShellExecute = true });
}
}
}
}
}
}
How to convert PDF to Word in memory using VB.Net
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft
Module Sample
Sub Main()
ConvertPdfToDocxBytes()
End Sub
Private Sub ConvertPdfToDocxBytes()
Dim pdfFile As String = Path.GetFullPath("..\..\..\simple text.pdf")
Dim pdf() As Byte = File.ReadAllBytes(pdfFile)
Dim docx() As Byte = Nothing
Dim f As New SautinSoft.PdfFocus()
f.OpenPdf(pdf)
If f.PageCount > 0 Then
docx = f.ToWord()
If docx IsNot Nothing Then
Dim wordFile As String = "Result.docx"
File.WriteAllBytes(wordFile, docx)
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(wordFile) With {.UseShellExecute = True})
End If
End If
End Sub
Private Sub ConvertPdfToRtfStream()
Dim pdfFile As String = Path.GetFullPath("..\..\..\simple text.pdf")
Dim rtfStream As New MemoryStream()
Dim f As New SautinSoft.PdfFocus()
Using pdfStream As New FileStream(pdfFile, FileMode.Open, FileAccess.Read)
f.OpenPdf(pdfStream)
If f.PageCount > 0 Then
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Rtf
Dim res As Integer = f.ToWord(rtfStream)
If res = 0 Then
Dim rtfFile As String = "Result.rtf"
File.WriteAllBytes(rtfFile, rtfStream.ToArray())
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(rtfFile) With {.UseShellExecute = True})
End If
End If
End Using
End Sub
End Module
See Also