3

I am attempting to translate this qpdf command:

qpdf --qdf --object-streams=disable input.pdf editable.pdf

into the equivalent method calls I would need when using the qpdf dll (available from here: https://sourceforge.net/projects/qpdf/).

I ran the qpdf dll through dumpbin to get the function names, and by looking at the header files that were included for use with a c++ project I can see the parameters for the functions.

For example the function needed to impart the --object-streams option above would (from what I can tell) be this function:

void setObjectStreamMode(qpdf_object_stream_e);

from the c++ header file which becomes:

[DllImport("qpdf21.dll")]
static extern void _ZN10QPDFWriter19setObjectStreamModeE20qpdf_object_stream_e(int stateEnum);

in the C# file.

The problem is when I use the above function I get an

AccessViolationException: Attempted to read or write protected memory

error, which makes me think I need to create a QPDF object somehow, but I have never used object oriented pinvokes, so I'm at a loss of how to make the object accessible in c#.

If anyone is already familiar with using the dll in C#, or even in C++, and could tell me the correct functions to call to replicate the command I would appreciate it!

prw56
  • 326
  • 2
  • 12

2 Answers2

4

I've managed to figure it out, the below code replicates the command, turns out I was looking into the wrong header file:

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr qpdf_init();

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern void qpdf_cleanup(ref IntPtr qpdfData);

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern int qpdf_read(IntPtr qpdfdata, [MarshalAs(UnmanagedType.LPStr)] string fileName, IntPtr password);

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern void qpdf_set_object_stream_mode(IntPtr qpdf, int mode);

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern void qpdf_set_qdf_mode(IntPtr qpdf, int value);

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern int qpdf_init_write(IntPtr qpdf, [MarshalAs(UnmanagedType.LPStr)] string fileName);

[DllImport("qpdf21.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern int qpdf_write(IntPtr qpdf);

static void Main(string[] args)
{
    //call init
    IntPtr qpdfData = qpdf_init();

    //call read (which gets and processes the input file)
    //0 result == good
    int result = qpdf_read(qpdfData, @"scan.pdf", IntPtr.Zero);

    //call init_write
    result = qpdf_init_write(qpdfData, @"scanEditable.pdf");

    //set write options
    //disable object stream mode
    qpdf_set_qdf_mode(qpdfData, 1);
    qpdf_set_object_stream_mode(qpdfData, 0);

    //call write
    result = qpdf_write(qpdfData);

    //call cleanup
    qpdf_cleanup(ref qpdfData);
}
prw56
  • 326
  • 2
  • 12
3

Looks like you've figured out a good answer here. You've discovered the C API, which is intended for helping to use QPDF from languages other than C++ through the DLL. The C API is primarily documented in the qpdf-c.h header file. You can find some information in the Using Other Languages section of the manual as well. The C API does not expose the full functionality of qpdf's C++ library. If you find missing pieces, please feel free to create an issue at github. I try to update the C API when I add new interfaces, but I don't do it for every interface, and some of the functionality in the CLI is implemented directly in the tool and doesn't map to a single library function.

If the C API is not rich enough for your use case, it's also possible to write your own C++ class with some functions declared extern "C", export them, and build an additional DLL that you can use in the manner you have found above. You can look at qpdf-c.cc as an example of how this works.