Guest User

Untitled

a guest
Nov 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7.  
  8. namespace BBxCommon
  9. {
  10.     public class FileUtilities
  11.     {
  12.         public static void ByteArrayToFile(string fileName, byte[] byteArray)
  13.         {
  14.             // execute with using so the filestream is properly disposed
  15.             using (FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  16.             {
  17.                 // Read the array and writes to file
  18.                 fileStream.Write(byteArray, 0, byteArray.Length);
  19.                 fileStream.Close();
  20.             }
  21.         }
  22.  
  23.         // execute with using so the filestream is properly disposed
  24.         public static byte[] ReadFileToByteArray(string filePath)
  25.         {
  26.             byte[] buffer;
  27.  
  28.             using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  29.             {
  30.                 int length = (int)fileStream.Length;          
  31.                 buffer = new byte[length];
  32.                 int count;
  33.                 int sum = 0;
  34.  
  35.                 // read until Read method returns 0 (end of the stream has been reached)
  36.                 while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
  37.                 {
  38.                     sum += count;  // sum is a buffer offset for next reading
  39.                 }
  40.  
  41.                 fileStream.Close();
  42.             }
  43.             return buffer;
  44.         }
  45.  
  46.         public class BBxPDF2Excel
  47.         {
  48.             public BBxPDF2Excel()
  49.             {
  50.             }
  51.  
  52.             public BBxPDF2Excel(string tempPath)
  53.             {
  54.                 TempPath = tempPath;
  55.             }
  56.  
  57.             // Default folder name for temp folder during extraction and conversion
  58.             private const string DEFAULT_TEMP_NAME = @"PDFExtractTemp";
  59.  
  60.             // Application and Library name
  61.             private const string DEFAULT_APP_NAME = @"PDF2Excel.exe";
  62.             private const string DEFAULT_DLL_NAME = @"GDIPLUS.dll";
  63.  
  64.             private String _TempPath;
  65.             public String TempPath
  66.             {
  67.                 get
  68.                 {
  69.                     // If this property has not been set, default it
  70.                     if (String.IsNullOrEmpty(_TempPath))
  71.                     {
  72.                         _TempPath = String.Format(@"C:\{0}\", DEFAULT_TEMP_NAME);
  73.                     }
  74.                     return _TempPath;
  75.                 }
  76.                 set
  77.                 {
  78.                     _TempPath = value;
  79.                 }
  80.             }
  81.  
  82.             // Basic conversion types supported by utility
  83.             public enum ConversionType
  84.             {
  85.                 cxls,
  86.                 cxlsx,
  87.                 ccsv
  88.             }
  89.  
  90.             /// <summary>
  91.             /// Converts a PDF via file path to excel binary by type
  92.             /// </summary>
  93.             /// <param name="pdfPath"></param>
  94.             /// <param name="type"></param>
  95.             /// <returns></returns>
  96.             public byte[] ConvertPDFToByteArray(string pdfPath, ConversionType type)
  97.             {
  98.                 return ConvertPDFToByteArray(ReadFileToByteArray(pdfPath), type);
  99.             }
  100.  
  101.             /// <summary>
  102.             /// Converts a PDF via binary to excel binary by type
  103.             /// </summary>
  104.             /// <param name="pdf"></param>
  105.             /// <param name="type"></param>
  106.             /// <returns></returns>
  107.             public byte[] ConvertPDFToByteArray(byte[] pdf, ConversionType type)
  108.             {
  109.                 return new byte[0x1];
  110.             }
  111.  
  112.             /// <summary>
  113.             /// Converts PDF via file path to excel stored from file path by type
  114.             /// </summary>
  115.             /// <param name="pdfPath"></param>
  116.             /// <param name="type"></param>
  117.             /// <returns></returns>
  118.             public string ConvertPDFToReturnedFilePath(string pdfPath, ConversionType type)
  119.             {
  120.                 return ConvertPDFToReturnedFilePath(ReadFileToByteArray(pdfPath), type);
  121.             }
  122.  
  123.             /// <summary>
  124.             /// Converts PDF via binary to excel stored from file path by type
  125.             /// </summary>
  126.             /// <param name="pdf"></param>
  127.             /// <param name="type"></param>
  128.             /// <returns></returns>
  129.             public string ConvertPDFToReturnedFilePath(byte[] pdf, ConversionType type)
  130.             {
  131.                 return String.Empty;
  132.             }
  133.  
  134.             /// <summary>
  135.             /// Stubbed out Raw version of executing the command line utility
  136.             /// </summary>
  137.             /// <param name="args"></param>
  138.             public void ExecuteRawCommand(string args)
  139.             {
  140.                 // Extract Lib for usage
  141.                 ExtractResource();
  142.  
  143.                 // Construct command
  144.                 Process p = new Process();
  145.                 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // Hide the command window
  146.                 p.StartInfo.FileName = String.Format(@"{0}\{1}", TempPath, DEFAULT_APP_NAME);
  147.                 p.StartInfo.Arguments = args;
  148.                 p.Start();
  149.  
  150.                 // We want to wait for this command to finish, and not let go of the thread quite yet
  151.                 p.WaitForExit();
  152.  
  153.                 // Remove the exe for cleanup
  154.                 CleanupResources();
  155.             }
  156.  
  157.             /// <summary>
  158.             /// Extracts application from resource
  159.             /// </summary>
  160.             private void ExtractResource()
  161.             {
  162.                 // Ensure the temp directory is created, if not, create it
  163.                 if (!Directory.Exists(TempPath))
  164.                 {
  165.                     Directory.CreateDirectory(TempPath);
  166.                 }
  167.  
  168.                 // Extract the application and the lib
  169.                 ByteArrayToFile(String.Format(@"{0}\{1}", TempPath, DEFAULT_APP_NAME), rcPDF2Excel.PDF2Excel);
  170.                 ByteArrayToFile(String.Format(@"{0}\{1}", TempPath, DEFAULT_DLL_NAME), rcPDF2Excel.GDIPLUS);
  171.             }
  172.  
  173.             /// <summary>
  174.             /// Cleans up temp directory from resource extraction
  175.             /// </summary>
  176.             private void CleanupResources()
  177.             {
  178.                 // If the temp directory doesnt exist, we know an error occured and dont need
  179.                 // to waste cpu deleting something that doesnt exist
  180.                 if (!Directory.Exists(TempPath))
  181.                 {
  182.                     return;
  183.                 }
  184.  
  185.                 // Cleanup the temp directory
  186.                 File.Delete(String.Format(@"{0}\{1}", TempPath, DEFAULT_APP_NAME));
  187.                 File.Delete(String.Format(@"{0}\{1}", TempPath, DEFAULT_DLL_NAME));
  188.             }
  189.         }
  190.  
  191.         public class ExcelParser
  192.         {
  193.  
  194.         }
  195.  
  196.     }
  197. }
Add Comment
Please, Sign In to add comment