Can I return byte[] with GhostscriptProcessor?

家住魔仙堡 提交于 2021-02-08 05:45:18

问题


Is it possible to return byte[] using the GhostscriptProcessor? For example:

public static byte[] ConvertToPDFA(byte[] pdfData)
{
  GhostscriptProcessor gsproc = new GhostscriptProcessor(Properties.Resources.gsdll32);

  //return byte[] from the created PDF/A

The method StartProcessing is a void method, but is there any alternative thet can create a PDF/A from a PDF File and return a byte[] from its content?


回答1:


It's possible:

public class PipedOutputSample
{
    public void Start()
    {
        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;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/25240436/can-i-return-byte-with-ghostscriptprocessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!