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#.
Asked
Active
Viewed 9,238 times
3 Answers
3
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

Ken White
- 123,280
- 14
- 225
- 444

serge_gubenko
- 20,186
- 2
- 61
- 64
-
Hi, I'm now using gsdll32.dll so no gswin32.exe is available. I have to call the gsapi_init_with_args. from the GhostScript website, i got something like this: gs -dPDFA -dBATCH -dNOPAUSE -dNOOUTERSAVE -dUseCIEColor -sDEVICE=pdfwrite -sOutputFile=out-x3.pdf PDFA_def.ps input.pdf but when I use the arguments with gsapi_init_with_args the result PDF isn't right, it reports that it doesn't conform to any standard. So it's tricky. Anyone can help. – imgen Nov 13 '09 at 04:57
-
in fact it's not tricky at all, I gave you a command line for gswin32.exe in order to make sure -dPDFA and the rest of switches are working fine for you, you should have gswin32.exe with your installation as I see you're using ghostscript for win. Just run the line I gave you to check if it works. After you're shure the command works you can translate it into the gsapi_init_with_args call regards – serge_gubenko Nov 13 '09 at 14:23
1
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"
- ...

Daniel Ballinger
- 13,187
- 11
- 69
- 96
0
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.

Kurt Pfeifle
- 86,724
- 23
- 248
- 345