gpt4 book ai didi

c# - 在 C# 中使用 QPDF

转载 作者:行者123 更新时间:2023-11-30 02:19:55 29 4
gpt4 key购买 nike

我正在尝试翻译这个 qpdf 命令:

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

进入我在使用 qpdf dll 时需要的等效方法调用(可从此处获得:https://sourceforge.net/projects/qpdf/)。

我通过 dumpbin 运行 qpdf dll 以获取函数名称,并且通过查看用于 c++ 项目的头文件,我可以看到函数的参数。

例如,传递上面的 --object-streams 选项所需的函数(据我所知)是这个函数:

void setObjectStreamMode(qpdf_object_stream_e);

从 c++ 头文件变为:

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

在 C# 文件中。

问题是当我使用上面的函数时我得到了一个

AccessViolationException:试图读取或写入 protected 内存

错误,这让我觉得我需要以某种方式创建一个 QPDF 对象,但我从未使用过面向对象的 pinvoke,所以我不知道如何在 C# 中访问该对象。

如果有人已经熟悉在 C# 甚至 C++ 中使用 dll,并且可以告诉我调用正确的函数来复制命令,我将不胜感激!

最佳答案

我已经弄清楚了,下面的代码复制了命令,结果我查看了错误的头文件:

[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);
}

关于c# - 在 C# 中使用 QPDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50276134/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com