【问题标题】:How to Use Ghostscript DLL to convert PDF to PDF/A如何使用 Ghostscript DLL 将 PDF 转换为 PDF/A
【发布时间】:2023-04-08 20:56:01
【问题描述】:

如何使用 GhostScript DLL 将 PDF 转换为 PDF/A。我知道我必须调用名为 gsapi_init_with_args 的 gsdll32.dll 的导出函数,但我如何传递正确的参数?顺便说一句,我正在使用 C#。

【问题讨论】:

    标签: c# pdf ghostscript


    【解决方案1】:

    请尝试从命令行运行它以测试它是否正在执行您需要的操作。

    gswin32.exe -dPDFA -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=PDFA.pdf 1.pdf
    

    A Simple C# Wrapper for Ghostscript

    【讨论】:

    • 嗨,我现在使用的是 gsdll32.dll,所以没有 gswin32.exe 可用。我必须调用 gsapi_init_with_args。从 GhostScript 网站,我得到了这样的信息: gs -dPDFA -dBATCH -dNOPAUSE -dNOOUTERSAVE -dUseCIEColor -sDEVICE=pdfwrite -sOutputFile=out-x3.pdf PDFA_def.ps input.pdf 但是当我将参数与 gsapi_init_with_args 一起使用时PDF 不正确,它报告它不符合任何标准。所以这很棘手。任何人都可以提供帮助。
    • 其实一点也不难,我给了你一个 gswin32.exe 的命令行,以确保 -dPDFA 和其他开关对你正常工作,你应该有 gswin32.exe随着您的安装,我看到您正在使用ghostscript for win。只需运行我给你的线路,检查它是否有效。在您确定命令有效后,您可以将其转换为 gsapi_init_with_args 调用问候
    【解决方案2】:

    取决于您的检查工具报告的与标准的确切偏差...您可能需要更改 PDFA_def.ps 以适应您的环境(并且您可能需要为每个新的 PDF/A 动态重写该文件转换)。这是一个简短的文件,并且评论很好。

    尝试将 -Ic:/path/to/gsinstalldir/lib 和 PDFA_def.ps 的直接调用添加到建议的命令行 serge 中:

    gswin32c.exe ^ -Ic:/path/to/gsinstalldir/lib ^ -dPDFA ^ -分贝^ -dNOPAUSE ^ -dUseCIEColor ^ -sDEVICE=pdfwrite ^ -sOutputFile=输出-PDFA.pdf ^ PDFA_def.gs ^ 输入.pdf

    或

    gswin32c.exe ^ -Ic:/path/to/gsinstalldir/lib ^ -dPDFA ^ -分贝^ -dNOPAUSE ^ -dUseCIEColor ^ -sDEVICE=pdfwrite ^ -sOutputFile=输出-PDFA.pdf ^ c:/path/to/customized/PDFA_def.gs ^ 输入.pdf

    首先测试命令行,然后按照 serge 的建议进行操作。

    【讨论】:

      【解决方案3】:

      我已经使用ghostscriptsharp 中的以下内容让它工作了:

      [DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
      private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle);
      
      [DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
      private static extern int InitAPI(IntPtr instance, int argc, string[] argv);
      
      [DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
      private static extern int ExitAPI(IntPtr instance);
      
      [DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
      private static extern void DeleteAPIInstance(IntPtr instance);
      
          private static void CallAPI(string[] args)
          {
              IntPtr gsInstancePtr;
              lock (resourceLock)
              {
                  CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
                  try
                  {
                      int result = InitAPI(gsInstancePtr, args.Length, args);
      
                      if (result < 0)
                      {
                          throw new ExternalException("Ghostscript conversion error", result);
                      }
                  }
                  finally
                  {
                      Cleanup(gsInstancePtr);
                  }
              }
          }
      
          private static object resourceLock = new object();
      
          private static void Cleanup(IntPtr gsInstancePtr)
          {
              ExitAPI(gsInstancePtr);
              DeleteAPIInstance(gsInstancePtr);
          }
      

      args 将是一个字符串数组,例如:

      • “-sDEVICE=pdfwrite”
      • “-dPDFA”
      • ...

      【讨论】:

        猜你喜欢
        • 2016-06-15
        • 2010-12-12
        • 2020-03-07
        • 1970-01-01
        • 2014-11-20
        • 2019-04-02
        • 2020-07-13
        • 2012-08-30
        • 2017-02-21
        相关资源
        最近更新 更多