Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.62 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.             private String _TempPath;
  61.             public String TempPath
  62.             {
  63.                 get
  64.                 {
  65.                     // If this property has not been set, default it
  66.                     if (String.IsNullOrEmpty(_TempPath))
  67.                     {
  68.                         _TempPath = String.Format(@"C:\{0}\", DEFAULT_TEMP_NAME);
  69.                     }
  70.                     return _TempPath;
  71.                 }
  72.                 set
  73.                 {
  74.                     _TempPath = value;
  75.                 }
  76.             }
  77.  
  78.             // Basic conversion types supported by utility
  79.             public enum ConversionType
  80.             {
  81.                 cxls,
  82.                 cxlsx,
  83.                 ccsv
  84.             }
  85.  
  86.             /// <summary>
  87.             /// Converts a PDF via file path to excel binary by type
  88.             /// </summary>
  89.             /// <param name="pdfPath"></param>
  90.             /// <param name="type"></param>
  91.             /// <returns></returns>
  92.             public byte[] ConvertPDFToByteArray(string pdfPath, ConversionType type)
  93.             {
  94.                 return ConvertPDFToByteArray(ReadFileToByteArray(pdfPath), type);
  95.             }
  96.  
  97.             /// <summary>
  98.             /// Converts a PDF via binary to excel binary by type
  99.             /// </summary>
  100.             /// <param name="pdf"></param>
  101.             /// <param name="type"></param>
  102.             /// <returns></returns>
  103.             public byte[] ConvertPDFToByteArray(byte[] pdf, ConversionType type)
  104.             {
  105.                 return new byte[0x1];
  106.             }
  107.  
  108.             /// <summary>
  109.             /// Converts PDF via file path to excel stored from file path by type
  110.             /// </summary>
  111.             /// <param name="pdfPath"></param>
  112.             /// <param name="type"></param>
  113.             /// <returns></returns>
  114.             public string ConvertPDFToReturnedFilePath(string pdfPath, ConversionType type)
  115.             {
  116.                 return ConvertPDFToReturnedFilePath(ReadFileToByteArray(pdfPath), type);
  117.             }
  118.  
  119.             /// <summary>
  120.             /// Converts PDF via binary to excel stored from file path by type
  121.             /// </summary>
  122.             /// <param name="pdf"></param>
  123.             /// <param name="type"></param>
  124.             /// <returns></returns>
  125.             public string ConvertPDFToReturnedFilePath(byte[] pdf, ConversionType type)
  126.             {
  127.                 return String.Empty;
  128.             }
  129.  
  130.             /// <summary>
  131.             /// Stubbed out Raw version of executing the command line utility
  132.             /// </summary>
  133.             /// <param name="args"></param>
  134.             public void ExecuteRawCommand(string args)
  135.             {
  136.                 // Extract Lib for usage
  137.                 ExtractResource();
  138.  
  139.                 // Construct command
  140.                 Process p = new Process();
  141.                 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // Hide the command window
  142.                 p.StartInfo.FileName = String.Format(@"{0}\PDF2Excel.exe", TempPath);
  143.                 p.StartInfo.Arguments = args;
  144.                 p.Start();
  145.  
  146.                 // We want to wait for this command to finish, and not let go of the thread quite yet
  147.                 p.WaitForExit();
  148.  
  149.                 // Remove the exe for cleanup
  150.                 CleanupResources();
  151.             }
  152.  
  153.             /// <summary>
  154.             /// Extracts application from resource
  155.             /// </summary>
  156.             private void ExtractResource()
  157.             {
  158.                 // Ensure the temp directory is created, if not, create it
  159.                 if (!Directory.Exists(TempPath))
  160.                 {
  161.                     Directory.CreateDirectory(TempPath);
  162.                 }
  163.  
  164.                 // Extract the application and the lib
  165.                 ByteArrayToFile(String.Format(@"{0}\PDF2Excel.exe", TempPath), rcPDF2Excel.PDF2Excel);
  166.                 ByteArrayToFile(String.Format(@"{0}\GDIPLUS.dll", TempPath), rcPDF2Excel.GDIPLUS);
  167.             }
  168.  
  169.             /// <summary>
  170.             /// Cleans up temp directory from resource extraction
  171.             /// </summary>
  172.             private void CleanupResources()
  173.             {
  174.                 // If the temp directory doesnt exist, we know an error occured and dont need
  175.                 // to waste cpu deleting something that doesnt exist
  176.                 if (!Directory.Exists(TempPath))
  177.                 {
  178.                     return;
  179.                 }
  180.  
  181.                 // Cleanup the temp directory
  182.                 File.Delete(String.Format(@"{0}\PDF2Excel.exe", TempPath));
  183.                 File.Delete(String.Format(@"{0}\GDIPLUS.dll", TempPath));
  184.             }
  185.         }
  186.  
  187.         public class ExcelParser
  188.         {
  189.  
  190.         }
  191.  
  192.     }
  193. }
Add Comment
Please, Sign In to add comment