简体   繁体   English

使用ghostscript将Postscript转换为Text文件

[英]Convert Postscript to Text file using ghostscript

my client wants me to do one task. 我的客户要我做一项任务。 which is whenever I print ctrl+P from the browser it will go automatically that contents to the database which is sql. 这是每当我从浏览器打印ctrl+P它会自动将该内容转到数据库sql。

Now, Let me explain what I have tried to achieve this. 现在,让我解释一下我试图实现的目标。 Usually printerPlusPlus which is third party tool. 通常printerPlusPlus是第三方工具。 Which adds virtual printer and prints the files PS to the temp directory than I can read the contents of that postscript file and save it to the database. 这会添加虚拟打印机并将文件PS打印到临时目录,而不是我可以读取该postscript文件的内容并将其保存到数据库中。

My real question is there anything from which I can convert this post script files to text or read them and save the texts to the database? 我真正的问题是,我可以将这些后期脚本文件转换为文本或读取它们并将文本保存到数据库中吗? Or is there any better way to achieve this task? 或者有更好的方法来完成这项任务吗?

Ghostscript is the alternate and ow-some feature to convert the postscripts to the text or pdf. Ghostscript是将postcripts转换为text或pdf的备用和ow-some功能。 But, I am completely clueless about the documentation and how to execute their commands. 但是,我对文档以及如何执行命令完全不了解。

_viewer.Interpreter.RunFile("C:\\PrinterPlusPlus\\Temp\\ankit_SONY-VAIO_sony_20151227_185020_3.ps");

            GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
            dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            dev.ResolutionXY = new GhostscriptImageDeviceResolution(96, 96);
            dev.InputFiles.Add(@"C:\\PrinterPlusPlus\\Temp\\ankit_SONY-VAIO_sony_20151227_185020_3.ps");
            dev.OutputPath = @"C:\\PrinterPlusPlus\\Temp\\ankit_SONY-VAIO_sony_20151227_185020_3.txt";
            dev.Process();

            _preview.Activate();

I tried this but this seems to be not working and adding ASCII text to the txt file. 我试过这个,但这似乎无法正常工作并将ASCII文本添加到txt文件中。

I found ghostscript little confusing. 我发现ghostscript有点混乱。 But, I found the solution from here 但是,我从这里找到了解决方案

string inputFile = @"E:\gss_test\test_postscript.ps";
    GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();

    // pipe handle format: %handle%hexvalue
    string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

    using (GhostscriptProcessor processor = new GhostscriptProcessor())
    {
        List<string> switches = new List<string>();
        switches.Add("-empty");
        switches.Add("-dQUIET");
        switches.Add("-dSAFER");
        switches.Add("-dBATCH");
        switches.Add("-dNOPAUSE");
        switches.Add("-dNOPROMPT");
        switches.Add("-sDEVICE=pdfwrite");
        switches.Add("-o" + outputPipeHandle);
        switches.Add("-q");
        switches.Add("-f");
        switches.Add(inputFile);

        try
        {
            processor.StartProcessing(switches.ToArray(), null);

            byte[] rawDocumentData = gsPipedOutput.Data;

            //if (writeToDatabase)
            //{
            //    Database.ExecSP("add_document", rawDocumentData);
            //}
            //else if (writeToDisk)
            //{
            //    File.WriteAllBytes(@"E:\gss_test\output\test_piped_output.pdf", rawDocumentData);
            //}
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            gsPipedOutput.Dispose();
            gsPipedOutput = null;
        }
  }

This reads the postscript files easily :) 这很容易读取postscript文件:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM