горизонтально расположенные полосы: белая, синяя, красная

Как конвертировать PDF в Excel на C# и VB.NET

Конвертирование только таблиц из PDF в Excel

using System;
using System.IO;
namespace Sample
{
  class Sample
  {
    static void Main(string[] args)
    {
      string pathToPdf = @"..\..\Table.pdf";
      string pathToExcel = Path.ChangeExtension(pathToPdf, ".xls");

      // Convert only tables from PDF to XLS spreadsheet and skip all textual data.
      SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
      // This property is necessary only for registered version
      //f.Serial = "XXXXXXXXXXX";

      // 'true' = Convert all data to spreadsheet (tabular and even textual).
      // 'false' = Skip textual data and convert only tabular (tables) data.
      f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = false;

      // 'true' = Preserve original page layout.
      // 'false' = Place tables before text.
      f.ExcelOptions.PreservePageLayout = true;

      // The information includes the names for the culture, the writing system,
      // the calendar used, the sort order of strings, and formatting for dates and numbers.
      System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
      ci.NumberFormat.NumberDecimalSeparator = ",";
      ci.NumberFormat.NumberGroupSeparator = ".";

      f.ExcelOptions.CultureInfo = ci;
      f.OpenPdf(pathToPdf);
      if (f.PageCount > 0)
      {
        int result = f.ToExcel(pathToExcel);

        // Open the resulted Excel workbook.
        if (result==0)
        {
          System.Diagnostics.Process.Start(pathToExcel);
        }
      }
    }
  }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
  Sub Main()
    Dim pathToPdf As String = "..\Table.pdf"
    Dim pathToExcel As String = Path.ChangeExtension(pathToPdf, ".xls")

    ' Convert only tables from PDF to XLS spreadsheet and skip all textual data.
    Dim f As New SautinSoft.PdfFocus()

    ' This property is necessary only for registered version
    'f.Serial = "XXXXXXXXXXX";

    ' 'true' = Convert all data to spreadsheet (tabular and even textual).
    ' 'false' = Skip textual data and convert only tabular (tables) data.
    f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = False

    ' 'true' = Preserve original page layout.
    ' 'false' = Place tables before text.
    f.ExcelOptions.PreservePageLayout = True

    ' The information includes the names for the culture, the writing system,
    ' the calendar used, the sort order of strings, and formatting for dates and numbers.
    Dim ci As New System.Globalization.CultureInfo("en-US")
    ci.NumberFormat.NumberDecimalSeparator = ","
    ci.NumberFormat.NumberGroupSeparator = "."

    f.ExcelOptions.CultureInfo = ci
    f.OpenPdf(pathToPdf)
    If f.PageCount > 0 Then
      Dim result As Integer = f.ToExcel(pathToExcel)

      ' Open the resulted Excel workbook.
      If result = 0 Then
        System.Diagnostics.Process.Start(pathToExcel)
      End If
    End If
  End Sub
End Module

Конвертирование PDF-файла в Excel

using System;
using System.IO;
namespace Sample
{
  class Sample
  {
    static void Main(string[] args)
    {
      string pathToPdf = @"..\..\Table.pdf";
      string pathToExcel = Path.ChangeExtension(pathToPdf, ".xls");

      // Convert PDF file to Excel file
      SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

      // This property is necessary only for registered version
      //f.Serial = "XXXXXXXXXXX";

      // 'true' = Convert all data to spreadsheet (tabular and even textual).
      // 'false' = Skip textual data and convert only tabular (tables) data.
      f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = true;

      // 'true' = Preserve original page layout.
      // 'false' = Place tables before text.
      f.ExcelOptions.PreservePageLayout = true;

      // The information includes the names for the culture, the writing system,
      // the calendar used, the sort order of strings, and formatting for dates and numbers.
      System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
      ci.NumberFormat.NumberDecimalSeparator = ",";
      ci.NumberFormat.NumberGroupSeparator = ".";

      f.ExcelOptions.CultureInfo = ci;
      f.OpenPdf(pathToPdf);
      if (f.PageCount > 0)
      {
        int result = f.ToExcel(pathToExcel);

        //Open a produced Excel workbook
        if (result==0)
        {
          System.Diagnostics.Process.Start(pathToExcel);
        }
      }
    }
  }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
  Sub Main()
    Dim pathToPdf As String = "..\Table.pdf"
    Dim pathToExcel As String = Path.ChangeExtension(pathToPdf, ".xls")

    ' Convert PDF file to Excel file
    Dim f As New SautinSoft.PdfFocus()

    ' This property is necessary only for registered version
    'f.Serial = "XXXXXXXXXXX";

    ' 'true' = Convert all data to spreadsheet (tabular and even textual).
    ' 'false' = Skip textual data and convert only tabular (tables) data.
    f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = True

    ' 'true' = Preserve original page layout.
    ' 'false' = Place tables before text.
    f.ExcelOptions.PreservePageLayout = True

    ' The information includes the names for the culture, the writing system,
    ' the calendar used, the sort order of strings, and formatting for dates and numbers.
    Dim ci As New System.Globalization.CultureInfo("en-US")
    ci.NumberFormat.NumberDecimalSeparator = ","
    ci.NumberFormat.NumberGroupSeparator = "."

    f.ExcelOptions.CultureInfo = ci
    f.OpenPdf(pathToPdf)
    If f.PageCount > 0 Then
      Dim result As Integer = f.ToExcel(pathToExcel)

      'Open a produced Excel workbook
      If result=0 Then
        System.Diagnostics.Process.Start(pathToExcel)
      End If
    End If
  End Sub
End Module

Конвертирование PDF в Excel в многопоточном режиме

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using SautinSoft;

namespace Sample
{
  class Sample
  {
    static void Main(string[] args)
    {
      ConvertPdfToExcelInThread();
    }
    public class TArgument
    {
      public string PdfFile { get; set; }
      public int PageNumber { get; set; }
    }
      public static void ConvertPdfToExcelInThread()
      {
        string pdfs = @"..\..\";
        string[] files = Directory.GetFiles(pdfs, "*.pdf");
        List<Thread> threads = new List<Thread>();
        for (int i = 0; i < files.Length; i++)
        {
          TArgument targ = new TArgument()
          {
            PdfFile = files[i],
            PageNumber = 1
          };
          var t = new Thread((a) => ConvertToExcel(a));
          t.Start(targ);
          threads.Add(t);
        }
        foreach (var thread in threads)
        thread.Join();
        Console.WriteLine("Done.");
        Console.ReadLine();
      }
      public static void ConvertToExcel(object targ)
      {
        TArgument targum = (TArgument)targ;

        string pdfFile = targum.PdfFile;
        int page = targum.PageNumber;

        string excelFile = Path.ChangeExtension(pdfFile, ".xls");
        SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();

        // 'true' = Convert all data to spreadsheet (tabular and even textual).
        // 'false' = Skip textual data and convert only tabular (tables) data.
        f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = true;

        // 'true' = Preserve original page layout.
        // 'false' = Place tables before text.
        f.ExcelOptions.PreservePageLayout = true;

        // The information includes the names for the culture, the writing system,
        // the calendar used, the sort order of strings, and formatting for dates and numbers.
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
        ci.NumberFormat.NumberDecimalSeparator = ",";
        ci.NumberFormat.NumberGroupSeparator = ".";

        f.ExcelOptions.CultureInfo = ci;
        f.OpenPdf(pdfFile);
        bool done = false;
        if (f.PageCount > 0)
        {
          if (page >= f.PageCount)
            page = 1;
          if (f.ToExcel(excelFile, page, page) == 0)
            done = true;
            f.ClosePdf();
        }
        if (done)
          Console.WriteLine("{0}\t - Done!", Path.GetFileName(pdfFile));
        else
          Console.WriteLine("{0}\t - Error!", Path.GetFileName(pdfFile));
      }
    }
  }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports System.Threading
Imports SautinSoft

Module Sample
  Sub Main()
    ConvertPdfToExcelInThread()
  End Sub

  Public Class TArgument
    Public Property PdfFile() As String
    Public Property PageNumber() As Integer
  End Class

  Public Sub ConvertPdfToExcelInThread()
    Dim pdfs As String = "..\"
    Dim files() As String = Directory.GetFiles(pdfs, "*.pdf")
    Dim threads As New List(Of Thread)()

    For i As Integer = 0 To files.Length - 1
      Dim targ As New TArgument() With {
      .PdfFile = files(i),
      .PageNumber = 1}

      Dim t = New Thread(Sub(a) ConvertToExcel(a))
      t.Start(targ)
      threads.Add(t)
      Next i

    For Each t As Thread In threads
    t.Join()
    Next t
    Console.WriteLine("Done.")
    Console.ReadLine()
  End Sub

  Public Sub ConvertToExcel(ByVal targ As Object)
    Dim targum As TArgument = DirectCast(targ, TArgument)
    Dim pdfFile As String = targum.PdfFile
    Dim page As Integer = targum.PageNumber
    Dim excelFile As String = Path.ChangeExtension(pdfFile, ".xls")
    Dim f As New SautinSoft.PdfFocus()

    ' 'true' = Convert all data to spreadsheet (tabular and even textual).
    ' 'false' = Skip textual data and convert only tabular (tables) data.
    f.ExcelOptions.ConvertNonTabularDataToSpreadsheet = True

    ' 'true' = Preserve original page layout.
    ' 'false' = Place tables before text.
    f.ExcelOptions.PreservePageLayout = True

    ' The information includes the names for the culture, the writing system,
    ' the calendar used, the sort order of strings, and formatting for dates and numbers.
    Dim ci As New System.Globalization.CultureInfo("en-US")
    ci.NumberFormat.NumberDecimalSeparator = ","
    ci.NumberFormat.NumberGroupSeparator = "."
    f.ExcelOptions.CultureInfo = ci
    f.OpenPdf(pdfFile)

    Dim done As Boolean = False
    If f.PageCount > 0 Then
      If page >= f.PageCount Then
        page = 1
      End If

      If f.ToExcel(excelFile, page, page) = 0 Then
        done = True
      End If
      f.ClosePdf()
    End If

    If done Then
      Console.WriteLine("{0}" & vbTab & " - Done!", Path.GetFileName(pdfFile))
    Else
      Console.WriteLine("{0}" & vbTab & " - Error!", Path.GetFileName(pdfFile))
    End If
  End Sub
End Module

Конвертирование PDF в книгу Excel в памяти

using System;
using System.IO;
namespace Sample
{
  class Sample
  {
    static void Main(string[] args)
    {
      string pathToPdf = @"..\..\Table.pdf";
      string pathToExcel = Path.ChangeExtension(pathToPdf, ".xls");

      // Here we have our PDF and Excel docs as byte arrays
      byte[] pdf = File.ReadAllBytes(pathToPdf);
      byte[] xls = null;

      // Convert PDF document to Excel workbook in memory
      SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
      // This property is necessary only for registered version
      //f.Serial = "XXXXXXXXXXX";

      // The information includes the names for the culture, the writing system,
      // the calendar used, the sort order of strings, and formatting for dates and numbers.
      System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
      ci.NumberFormat.NumberDecimalSeparator = ",";
      ci.NumberFormat.NumberGroupSeparator = ".";

      f.ExcelOptions.CultureInfo = ci;
      f.OpenPdf(pdf);
      if (f.PageCount > 0)
      {
        xls = f.ToExcel();
        //Save Excel workbook to a file in order to show it
        if (xls!=null)
        {
          File.WriteAllBytes(pathToExcel, xls);
          System.Diagnostics.Process.Start(pathToExcel);
        }
      }
    }
  }
}
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports SautinSoft

Module Sample
  Sub Main()
    Dim pathToPdf As String = "..\Table.pdf"
    Dim pathToExcel As String = Path.ChangeExtension(pathToPdf, ".xls")

    ' Here we have our PDF and Excel docs as byte arrays
    Dim pdf() As Byte = File.ReadAllBytes(pathToPdf)
    Dim xls() As Byte = Nothing

    ' Convert PDF document to Excel workbook in memory
    Dim f As New SautinSoft.PdfFocus()

    ' This property is necessary only for registered version
    'f.Serial = "XXXXXXXXXXX";

    ' The information includes the names for the culture, the writing system,
    ' the calendar used, the sort order of strings, and formatting for dates and numbers.
    Dim ci As New System.Globalization.CultureInfo("en-US")
    ci.NumberFormat.NumberDecimalSeparator = ","
    ci.NumberFormat.NumberGroupSeparator = "."

    f.ExcelOptions.CultureInfo = ci
    f.OpenPdf(pdf)
    If f.PageCount > 0 Then
      xls = f.ToExcel()
      
      'Save Excel workbook to a file in order to show it
      If xls IsNot Nothing Then
        File.WriteAllBytes(pathToExcel, xls)
        System.Diagnostics.Process.Start(pathToExcel)
      End If
    End If
  End Sub
End Module

Другие примеры кода SautinSoft.PdfFocus

PDF в Word PDF в HTML PDF в Img Image из PDF ✦ PDF в Excel PDF в XML PDF в Text PDF во Всё
 ВВЕРХ