Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- namespace BBxCommon
- {
- public class FileUtilities
- {
- public static void ByteArrayToFile(string fileName, byte[] byteArray)
- {
- // execute with using so the filestream is properly disposed
- using (FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- // Read the array and writes to file
- fileStream.Write(byteArray, 0, byteArray.Length);
- fileStream.Close();
- }
- }
- // execute with using so the filestream is properly disposed
- public static byte[] ReadFileToByteArray(string filePath)
- {
- byte[] buffer;
- using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- int length = (int)fileStream.Length;
- buffer = new byte[length];
- int count;
- int sum = 0;
- // read until Read method returns 0 (end of the stream has been reached)
- while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
- {
- sum += count; // sum is a buffer offset for next reading
- }
- fileStream.Close();
- }
- return buffer;
- }
- public class BBxPDF2Excel
- {
- public BBxPDF2Excel()
- {
- }
- public BBxPDF2Excel(string tempPath)
- {
- TempPath = tempPath;
- }
- // Default folder name for temp folder during extraction and conversion
- private const string DEFAULT_TEMP_NAME = @"PDFExtractTemp";
- private String _TempPath;
- public String TempPath
- {
- get
- {
- // If this property has not been set, default it
- if (String.IsNullOrEmpty(_TempPath))
- {
- _TempPath = String.Format(@"C:\{0}\", DEFAULT_TEMP_NAME);
- }
- return _TempPath;
- }
- set
- {
- _TempPath = value;
- }
- }
- // Basic conversion types supported by utility
- public enum ConversionType
- {
- cxls,
- cxlsx,
- ccsv
- }
- /// <summary>
- /// Converts a PDF via file path to excel binary by type
- /// </summary>
- /// <param name="pdfPath"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public byte[] ConvertPDFToByteArray(string pdfPath, ConversionType type)
- {
- return ConvertPDFToByteArray(ReadFileToByteArray(pdfPath), type);
- }
- /// <summary>
- /// Converts a PDF via binary to excel binary by type
- /// </summary>
- /// <param name="pdf"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public byte[] ConvertPDFToByteArray(byte[] pdf, ConversionType type)
- {
- return new byte[0x1];
- }
- /// <summary>
- /// Converts PDF via file path to excel stored from file path by type
- /// </summary>
- /// <param name="pdfPath"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public string ConvertPDFToReturnedFilePath(string pdfPath, ConversionType type)
- {
- return ConvertPDFToReturnedFilePath(ReadFileToByteArray(pdfPath), type);
- }
- /// <summary>
- /// Converts PDF via binary to excel stored from file path by type
- /// </summary>
- /// <param name="pdf"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public string ConvertPDFToReturnedFilePath(byte[] pdf, ConversionType type)
- {
- return String.Empty;
- }
- /// <summary>
- /// Stubbed out Raw version of executing the command line utility
- /// </summary>
- /// <param name="args"></param>
- public void ExecuteRawCommand(string args)
- {
- // Extract Lib for usage
- ExtractResource();
- // Construct command
- Process p = new Process();
- p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // Hide the command window
- p.StartInfo.FileName = String.Format(@"{0}\PDF2Excel.exe", TempPath);
- p.StartInfo.Arguments = args;
- p.Start();
- // We want to wait for this command to finish, and not let go of the thread quite yet
- p.WaitForExit();
- // Remove the exe for cleanup
- CleanupResources();
- }
- /// <summary>
- /// Extracts application from resource
- /// </summary>
- private void ExtractResource()
- {
- // Ensure the temp directory is created, if not, create it
- if (!Directory.Exists(TempPath))
- {
- Directory.CreateDirectory(TempPath);
- }
- // Extract the application and the lib
- ByteArrayToFile(String.Format(@"{0}\PDF2Excel.exe", TempPath), rcPDF2Excel.PDF2Excel);
- ByteArrayToFile(String.Format(@"{0}\GDIPLUS.dll", TempPath), rcPDF2Excel.GDIPLUS);
- }
- /// <summary>
- /// Cleans up temp directory from resource extraction
- /// </summary>
- private void CleanupResources()
- {
- // If the temp directory doesnt exist, we know an error occured and dont need
- // to waste cpu deleting something that doesnt exist
- if (!Directory.Exists(TempPath))
- {
- return;
- }
- // Cleanup the temp directory
- File.Delete(String.Format(@"{0}\PDF2Excel.exe", TempPath));
- File.Delete(String.Format(@"{0}\GDIPLUS.dll", TempPath));
- }
- }
- public class ExcelParser
- {
- }
- }
- }
Add Comment
Please, Sign In to add comment