imgen
imgen

Reputation: 21

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#.

Upvotes: 2

Views: 9608

Answers (3)

serge_gubenko
serge_gubenko

Reputation: 20482

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

Upvotes: 3

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

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"
  • ...

Upvotes: 1

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90213

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.

Upvotes: 0

Related Questions