How to convert PDF document to another format
with page progress bar на C# и .NET
«SautinSoft.PdfFocus" is an easy-to-use and at the same time productive .NET application for converting PDF documents to Word, Excel, Images and other formats.
In some cases, when converting large multi-page PDF documents, it is required to display information about the current page from all pages of the document to the console window.
Полный код.
using System;
using System.IO;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
string pdfFile = @"..\..\example.pdf";
string wordFile = "result.docx";
// Convert a PDF file to a Word file.
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
// This property is necessary only for registered version
// f.Serial = "XXXXXXXXXXX";
// Initializing the ProgressBar for pages (current/total)
f.NotifyPageProgress += PageProgressBar;
f.OpenPdf(pdfFile);
if (f.PageCount > 0)
{
// You may choose output format between Docx and Rtf.
f.WordOptions.Format = SautinSoft.PdfFocus.CWordOptions.eWordDocument.Docx;
int result = f.ToWord(wordFile);
// Show the resulting Word document.
if (result == 0)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(wordFile) { UseShellExecute = true });
}
}
Console.ReadKey();
}
private static void PageProgressBar(int progress, int total)
{
// Draw empty progress bar.
Console.CursorLeft = 0;
Console.Write("["); // start
Console.CursorLeft = 32;
Console.Write("]"); // end
Console.CursorLeft = 1;
float onechunk = 30.0f / total;
// Draw filled part.
int position = 1;
for (int i = 0; i <= onechunk * progress; i++)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.CursorLeft = position++;
Console.Write(" ");
}
// Draw unfilled part.
for (int i = position; i <= 31; i++)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.CursorLeft = position++;
Console.Write(" ");
}
// Draw totals.
Console.CursorLeft = 35;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(progress.ToString() + " of " + total.ToString() + " ");
}
}
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft
Module Sample
Sub Main()
ConvertPdfToDocxBytes()
'ConvertPdfToRtfStream()
End Sub
Private Sub ConvertPdfToDocxBytes()
Dim pdfFile As String = "..\simple text.pdf"
' Assume that we already have a PDF document as array of bytes.
Dim pdf() As Byte = File.ReadAllBytes(pdfFile)
Dim docx() As Byte = Nothing
' Convert PDF to word in memory
Dim f As New SautinSoft.PdfFocus()
'this property is necessary only for registered version
'f.Serial = "XXXXXXXXXXX"
f.OpenPdf(pdf)
If f.PageCount > 0 Then
' Convert pdf to word in memory.
docx = f.ToWord()
' Save word document to a file only for demonstration purposes.
If docx IsNot Nothing Then
'3. Save to DOCX document to a file for demonstration purposes.
Dim wordFile As String = Path.ChangeExtension(pdfFile, ".docx")
File.WriteAllBytes(wordFile, docx)
System.Diagnostics.Process.Start(wordFile)
End If
End If
End Sub
Private Sub ConvertPdfToRtfStream()
Dim pdfFile As String = "..\simple text.pdf"
Dim rtfStream As New MemoryStream()
' Convert PDF to word in memory
Dim f As New SautinSoft.PdfFocus()
'this property is necessary only for registered version
'f.Serial = "XXXXXXXXXXX"
' Assume that we already have a PDF document as stream.
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)
' Save rtfStream to a file for demonstration purposes.
If res = 0 Then
Dim rtfFile As String = Path.ChangeExtension(pdfFile, ".rtf")
File.WriteAllBytes(rtfFile, rtfStream.ToArray())
System.Diagnostics.Process.Start(rtfFile)
End If
End If
End Using
End Sub
End Module
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: