ฉันกำลังเขียน Windows Service เพื่อสแกนชุดไดเรกทอรีเพื่อหาไฟล์ PDF ใหม่และแปลงเป็น TIFF ด้วย Ghostscript.NET ตอนที่ฉันคอมไพล์และรันโค้ดเหมือนโปรแกรมปกติ มันทำงานได้อย่างสมบูรณ์ แต่เมื่อฉันใช้โค้ดเดียวกันกับ Service ผลลัพธ์ TIFF จะไม่ปรากฏขึ้นเลย ฉันได้ตั้งค่าไดเรกทอรีปลายทางเพื่อให้สามารถเขียนสำหรับทุกคนได้ และ PDF ต้นฉบับกำลังถูกลบออกตามที่ควรจะเป็น ดังนั้นจึงไม่ควรเป็นปัญหาการอนุญาตสำหรับผู้ใช้ "ระบบท้องถิ่น" การตรวจสอบไดเร็กทอรีสำหรับการเข้าถึงความล้มเหลวและความสำเร็จจะแสดงรายการความสำเร็จเท่านั้น
มีฟังก์ชันที่อ่านประชากรสีของ PDF เพื่อพิจารณาว่าเป็นเอกสารสีหรือสแกนขาวดำเป็นสี ส่วนนั้นใช้งานได้ ดังนั้นจึงไม่มีปัญหาในการเข้าถึงและอ่าน PDF
ฉันได้ลองลบ '-q' ออกจากสวิตช์ Ghostscript และฉันไม่มีรายงานข้อผิดพลาดใด ๆ และ "-dDEBUG" ส่งออกขยะจำนวนมาก ฉันไม่รู้ว่ามันพูดอะไร - แต่ไม่มีสิ่งใดถูกแท็กว่าเป็นข้อผิดพลาด
public static void ConvertPDF(string file, GSvalues gsVals)
{
gsProc = new Ghostscript.NET.Processor.GhostscriptProcessor();
System.Collections.Generic.List<string> switches = new System.Collections.Generic.List<string>();
switches.Add("-empty"); // GS.NET ignores the first switch
switches.Add("-r" + gsVals.Resolution); // dpi
switches.Add("-dDownScaleFactor=" + gsVals.ScaleFactor); // Scale the image back down
switches.Add("-sCompression=lzw"); // Compression
switches.Add("-dNumRenderingThreads=" + Environment.ProcessorCount);
switches.Add("-c \"30000000 setvmthreshold\"");
switches.Add("-dNOGC");
string device;
if (_checkPdf(file, gsVals.InkColorLevels, gsVals))
{
gsVals.WriteLog("Color PDF");
device = "-sDEVICE=tiffscaled24"; // 24bit Color TIFF
}
else
{
gsVals.WriteLog("Grayscale PDF");
device = "-sDEVICE=tiffgray"; // grayscale TIFF
}
switches.Add(device);
// Strip the filename out of the full path to the file
string filename = System.IO.Path.GetFileNameWithoutExtension(file);
// Set the output file tag
string oFileName = _setFileName(oPath + "\\" + filename.Trim(), GSvalues.Extension);
string oFileTag = "-sOutputFile=" + oFileName;
switches.Add(oFileTag);
switches.Add(file);
// Process the PDF file
try
{
string s = string.Empty;
foreach (string sw in switches) s += sw + ' ';
gsVals.DebugLog("Switches:\n\t" + s);
gsProc.StartProcessing(switches.ToArray(), new GsStdio());
while (gsProc.IsRunning) System.Threading.Thread.Sleep(1000);
}
catch (Exception e)
{
gsVals.WriteLog("Exception caught: " + e.Message);
Console.Read();
}
gsVals.DebugLog("Archiving PDF");
try
{
System.IO.File.Move(file, _setFileName(gsVals.ArchiveDir + "\\" + filename, ".pdf"));
}
catch (Exception e)
{
gsVals.WriteLog("Error moving PDF: " + e.Message);
}
}
private static string _setFileName(string path, string tifExt)
{
if (System.IO.File.Exists(path + tifExt)) return _setFileName(path, 1, tifExt);
else return path + tifExt;
}
private static string _setFileName(string path, int ctr, string tifExt)
{
// Test the proposed altered filename. It it exists, move to the next iteration
if(System.IO.File.Exists(path + '(' + ctr.ToString() + ')' + tifExt)) return _setFileName(path, ++ctr, tifExt);
else return path + '(' + ctr.ToString() + ')' + tifExt;
}
นี่คือเอาต์พุตตัวอย่างของสวิตช์ที่สร้างขึ้น (ดึงมาจากบันทึกเอาต์พุต):
Switches: -empty -r220 -dDownScaleFactor=1 -sCompression=lzw -dNumRenderingThreads=4 -c "30000000 setvmthreshold" -dNOGC -sDEVICE=tiffscaled24 -sOutputFile=\\[servername]\amb_ops_scanning$\Test.tiff \\[servername]\amb_ops_scanning$\Test.pdf
การตั้งค่าจะถูกอ่านในไฟล์ XML และจัดเก็บไว้ในคลาส GSVals คลาสยังจัดการการเขียนลงในบันทึกของระบบสำหรับเอาต์พุตหรือไปยังไฟล์ข้อความในเวอร์ชันโปรแกรมปกติ GSTDIO เป็นคลาสสำหรับจัดการอินพุตและเอาต์พุต GS ซึ่งเพียงเปลี่ยนเส้นทางเอาต์พุตทั้งหมดไปยังบันทึกเดียวกันกับ GSVals รหัสเดียวที่เปลี่ยนแปลงระหว่างเวอร์ชันโปรแกรมและเวอร์ชันบริการคือรหัสการจัดการบริการ และเอาต์พุตจะเปลี่ยนจากไฟล์ข้อความเป็นบันทึกของระบบ ไม่มีการเปลี่ยนแปลงใด ๆ เกี่ยวกับการประมวลผล Ghostscript
สิ่งนี้กำลังถูกคอมไพล์เป็น x86 เพื่อการพกพา แต่กำลังรันบน x64 ติดตั้ง GS 9.15 แล้ว ทั้งเวอร์ชัน x86 และ x64 GS.NET เป็นเวอร์ชัน 4.0.30319 ที่ติดตั้งผ่าน NuGet ใน VS 2012 ILMerge 2.13.0307 ถูกใช้เพื่อจัดทำแพ็กเกจ GS.NET dll ลงใน exe เพื่อการพกพาด้วยเช่นกัน สิ่งเหล่านี้ไม่มีการเปลี่ยนแปลงระหว่าง EXE ปกติและเวอร์ชัน Windows Service และอย่างที่บอกไปว่า EXE ปกติทำงานได้โดยไม่มีปัญหาใดๆ