English 中文(简体)
How to Use Ghostscript DLL to convert PDF to PDF/A
原标题:

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, i m using C#.

问题回答

pls, try to run this from the command line to test if it s doing what you need.

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

A Simple C# Wrapper for Ghostscript

I ve had it working using the following from 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 will be a array of strings like:

  • "-sDEVICE=pdfwrite"
  • "-dPDFA"
  • ...

Depends on what exact deviation from the standard your checker tools do report... You may need to alter your PDFA_def.ps to fit your environment (and you may need to dynamically re-write that file for every new PDF/A conversion). It s a short file, and well commented.

Try to add -Ic:/path/to/gsinstalldir/lib and the direct invocation of PDFA_def.ps to the commandline serge suggested:

 gswin32c.exe ^
    -Ic:/path/to/gsinstalldir/lib ^
    -dPDFA ^
    -dBATCH ^
    -dNOPAUSE ^
    -dUseCIEColor ^
    -sDEVICE=pdfwrite ^
    -sOutputFile=output-PDFA.pdf ^
    PDFA_def.gs ^
    input.pdf

or

 gswin32c.exe ^
    -Ic:/path/to/gsinstalldir/lib ^
    -dPDFA ^
    -dBATCH ^
    -dNOPAUSE ^
    -dUseCIEColor ^
    -sDEVICE=pdfwrite ^
    -sOutputFile=output-PDFA.pdf ^
    c:/path/to/customized/PDFA_def.gs ^
    input.pdf

Test commandline first, then do as serge recommended.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签