Require Complete
IDE0060
This commit is contained in:
89
Adaptation/FileHandlers/pcl/Convert.cs
Normal file
89
Adaptation/FileHandlers/pcl/Convert.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using Adaptation.Shared;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace Adaptation.FileHandlers.pcl;
|
||||
|
||||
internal class Convert
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Convert the raw data file to parsable file format - in this case from PCL to PDF
|
||||
/// </summary>
|
||||
/// <param name="sourceFile">source file to be converted to PDF</param>
|
||||
/// <returns></returns>
|
||||
private static string ConvertSourceFileToPdf(Logistics logistics, string ghostPCLFileName)
|
||||
{
|
||||
string result = Path.ChangeExtension(logistics.ReportFullPath, ".pdf");
|
||||
if (!File.Exists(result))
|
||||
{
|
||||
//string arguments = string.Concat("-i \"", sourceFile, "\" -o \"", result, "\"");
|
||||
string arguments = string.Concat("-dSAFER -dBATCH -dNOPAUSE -dFIXEDMEDIA -dFitPage -dAutoRotatePages=/All -dDEVICEWIDTHPOINTS=792 -dDEVICEHEIGHTPOINTS=612 -sOutputFile=\"", result, "\" -sDEVICE=pdfwrite \"", logistics.ReportFullPath, "\"");
|
||||
//Process process = Process.Start(configData.LincPDFCFileName, arguments);
|
||||
Process process = Process.Start(ghostPCLFileName, arguments);
|
||||
_ = process.WaitForExit(30000);
|
||||
if (!File.Exists(result))
|
||||
throw new Exception("PDF file wasn't created");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<string> GetTextFromPDF(string pdfTextStripperFileName, string sourceFileNamePdf, string altHeaderFileName)
|
||||
{
|
||||
string[] result;
|
||||
ProcessStartInfo processStartInfo = new(pdfTextStripperFileName, $"s \"{sourceFileNamePdf}\"")
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
};
|
||||
Process process = Process.Start(processStartInfo);
|
||||
_ = process.WaitForExit(30000);
|
||||
if (!File.Exists(altHeaderFileName))
|
||||
result = Array.Empty<string>();
|
||||
else
|
||||
result = File.ReadAllLines(altHeaderFileName);
|
||||
return new(result);
|
||||
}
|
||||
|
||||
internal static ReadOnlyCollection<string> PDF(Logistics logistics, string ghostPCLFileName, string pdfTextStripperFileName, List<FileInfo> fileInfoCollection)
|
||||
{
|
||||
ReadOnlyCollection<string> result;
|
||||
string sourceFileNamePdf = ConvertSourceFileToPdf(logistics, ghostPCLFileName);
|
||||
fileInfoCollection.Add(new FileInfo(sourceFileNamePdf));
|
||||
string altHeaderFileName = Path.ChangeExtension(logistics.ReportFullPath, ".txt");
|
||||
if (File.Exists(altHeaderFileName))
|
||||
{
|
||||
result = new(File.ReadAllLines(altHeaderFileName));
|
||||
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
//Pdfbox, IKVM.AWT.WinForms
|
||||
org.apache.pdfbox.pdmodel.PDDocument pdfDocument = org.apache.pdfbox.pdmodel.PDDocument.load(sourceFileNamePdf);
|
||||
org.apache.pdfbox.util.PDFTextStripper stripper = new();
|
||||
string text = stripper.getText(pdfDocument);
|
||||
pdfDocument.close();
|
||||
File.AppendAllText(altHeaderFileName, text);
|
||||
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
|
||||
result = new(text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (!File.Exists(pdfTextStripperFileName))
|
||||
throw;
|
||||
result = GetTextFromPDF(pdfTextStripperFileName, sourceFileNamePdf, altHeaderFileName);
|
||||
if (result.Count == 0)
|
||||
throw;
|
||||
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user