在参数中使用 Unicode 字符的 Windows 中使用 GhostScript 9.10

     2023-02-25     237

关键词:

【中文标题】在参数中使用 Unicode 字符的 Windows 中使用 GhostScript 9.10【英文标题】:Using GhostScript 9.10 in Windows with Unicode characters in parameters 【发布时间】:2014-01-21 07:32:32 【问题描述】:

我想在 .NET / C# 应用程序中使用 Ghostscript 将 .tiff 文件转换为 PDF。 我的问题:当文件路径包含非ansi字符(例如元音变音)时,函数

gsapi_init_with_args 

失败。 (使用 GS 8.x,它可以正常工作!)。 我发现了在 9.x 中行为发生了变化的信息,并且我还发现了一个名为

的函数
gsapi_init_with_argsW

而且这个函数应该可以在 .NET 上正常工作(参见http://permalink.gmane.org/gmane.comp.printing.ghostscript.cvs/31721)

所以我使用以下 DLLImport:

[DllImport(@"gsdll32.dll")]
public static extern int gsapi_init_with_argsW( IntPtr instace, int argc, string[] argv);

但这仍然不起作用,我收到错误:

Error: /undefinedfilename
in (C:\\304NDERUNGEN\\TEST.PDF)

文件名应该是

C:\\ÄNDERUNGEN\\TEST.PDF

因此无法正确识别变音符号“Ä”。

我在网上搜索了很多,但没有找到解决方案。

有什么想法吗? 谢谢!

【问题讨论】:

DllImport(@"gsdll32.dll", CharSet=CharSet.Unicode) 怎么样? 如果我这样做,应用程序在调用gsapi_init_with_args时会挂起,我不知道为什么......我意识到标准ghost DLL中不存在gsapi_init_with_argsW(DLLImport会自动回退到gsapi_init_with_args中在这种情况下,我没有收到错误但使用了标准的 gsapi_init_with_args 函数),所以我现在在调用 gsapi_init_with_args 之前直接调用 gsapi_set_arg_encoding(instance, 2)。这仍然会导致上述相同的问题。 unicode 的 gsapi_set_arg_encoding 是否默认为 UTF8? .NET 使用 UTF-16,您是否指定了正确的编码?不得已:使用转换为 UTF8 的内存字节指针传递自定义构造的内存指针数组? gsapi_set_arg_encoding 调用中的“2”代表 UTF-16。如果 1 是 UTF-8,0 是 ANSI。 这可能是 gsapi 中的错误。您是否尝试过将其作为命令行进程运行? 【参考方案1】:

我怀疑您需要在此处使用 UTF-8。通过GS_ARG_ENCODING_UTF8 拨打gs_set_arg_encoding

您传递给 Ghostscript 的任何字符串都应声明为 IntPtr。要将 C# 字符串转换为以 null 结尾的 UTF-8 编码字符串,请使用此 function provided by Hans Passant:

public static IntPtr NativeUtf8FromString(string managedString) 

    int len = Encoding.UTF8.GetByteCount(managedString);
    byte[] buffer = new byte[len + 1]; // null-terminator allocated
    Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
    IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
    Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
    return nativeUtf8;

确保您记得致电Marshal.FreeHGlobal 进行清理。

整体代码可能有点像这样:

public class Ghostscript

    public const int GS_ARG_ENCODING_LOCAL = 0;
    public const int GS_ARG_ENCODING_UTF8 = 1;

    [DllImport("gsdll32.dll")]
    private static extern int gsapi_new_instance(out IntPtr inst, IntPtr handle);

    [DllImport("gsdll32.dll")]
    private static extern int gsapi_set_arg_encoding(IntPtr inst, int encoding);

    [DllImport("gsdll32.dll")]
    private static extern int gsapi_init_with_args(IntPtr inst, int argc, IntPtr[] argv);

    [DllImport("gsdll32.dll")]
    private static extern int gsapi_exit(IntPtr inst);

    [DllImport("gsdll32.dll")]
    private static extern void gsapi_delete_instance(IntPtr inst);

    private static void checkReturnValue(int retval)
    
        if (retval != 0)
            throw ...; // implement error handling here
    

    public static void run(string[] argv)
    
        IntPtr inst;
        checkReturnValue(gsapi_new_instance(out inst, IntPtr.Zero));
        try
        
            IntPtr[] utf8argv = new IntPtr[argv.length];
            for (int i=0; i<utf8argv.Length; i++)
                utf8argv[i] = NativeUtf8FromString(argv[i]);
            try
            
                checkReturnValue(gsapi_set_arg_encoding(inst, GS_ARG_ENCODING_UTF8));
                checkReturnValue(gsapi_init_with_args(inst, utf8argv.Length, utf8argv));
                checkReturnValue(gsapi_exit(inst));
            finally
            
                for (int i=0; i<utf8argv.Length; i++)
                    Marshal.FreeHGlobal(utf8argv[i]);
            
        
        finally
        
            gsapi_delete_instance(inst);
        
    

【讨论】:

完美!立即生效。非常感谢! 好吧,我必须说我大吃一惊!我希望您至少需要进行一些调试和修复!无论如何,我很高兴能帮上忙。 原来的问题已经解决了,但是现在我在一些机器上遇到了 AccessViolationException。你有什么想法吗? 不。我想你需要一个包含所有相关细节的新问题。

在 .Net 中使用大于 2 个字节的 unicode 字符

】在.Net中使用大于2个字节的unicode字符【英文标题】:Usingunicodecharactersbiggerthan2byteswith.Net【发布时间】:2013-05-2420:51:41【问题描述】:我正在使用此代码生成U+10FFFCvars=Encoding.UTF8.GetString(newbyte[]0xF4,0x8F,0xBF,0xBC);我知道它是供私人... 查看详情

如何在 Perl POD 派生的手册页中使用 Unicode 字符?

】如何在PerlPOD派生的手册页中使用Unicode字符?【英文标题】:HowcanIuseUnicodecharactersinPerlPOD-derivedmanpages?【发布时间】:2012-10-1510:53:52【问题描述】:如果这不可能,那么处理源自UTF-8编码POD的手册页的最佳做法是什么?为了在POD... 查看详情

如何在 Pod 和 perldoc 中使用 Unicode 字符?

】如何在Pod和perldoc中使用Unicode字符?【英文标题】:HowdoIuseUnicodecharactersinPodandperldoc?【发布时间】:2010-09-1711:55:50【问题描述】:我需要在我的perl文档中使用utf-8字符。如果我使用:perldocMyMod.pm我看到奇怪的字符。如果我使用... 查看详情

如何在 jsoncpp 中使用 decodeString 来解码包含 unicode 字符的字符串

】如何在jsoncpp中使用decodeString来解码包含unicode字符的字符串【英文标题】:HowtousedecodeStringinjsoncpptodecodeastringcontainingunicodecharacters【发布时间】:2016-03-0211:04:59【问题描述】:我已经阅读了所有jsoncpp文档here并且我知道如何将jso... 查看详情

是否有任何从未使用过的字符(在 ASCII 或 Unicode 中)

】是否有任何从未使用过的字符(在ASCII或Unicode中)【英文标题】:Isthereanycharacterthatisneverused(inASCIIorUnicode)【发布时间】:2013-03-1718:05:37【问题描述】:我只需要一个可以在字符数组中设置的字符,以指示特定位置或一系列位... 查看详情

如何在 iOS 中定位和显示 Unicode 字符

】如何在iOS中定位和显示Unicode字符【英文标题】:HowtolocateanddisplayaUnicodecharacteriniOS【发布时间】:2011-12-1107:03:34【问题描述】:我目前在词汇测验程序中使用星号字符来分隔音节。但是,我更喜欢使用位于行高顶部和底部中间... 查看详情

如何在 Python 中动态指定 unicode 字符串?

】如何在Python中动态指定unicode字符串?【英文标题】:HowtodynamicallyspecifyunicodestringinPython?【发布时间】:2021-08-1809:37:13【问题描述】:根据here和here,Python允许使用unicode字符的名称来获取字符。例如:print(\'\\Nlargeredcircle\')????我... 查看详情

使用 Spring 应用程序中的休眠将表情符号 unicode 字符保存在 mysql 数据库中

】使用Spring应用程序中的休眠将表情符号unicode字符保存在mysql数据库中【英文标题】:Saveemojiunicodecharactersinmysqldatabasewithhibernatefromspringapplication【发布时间】:2017-10-0612:36:45【问题描述】:我有一个使用spring、hibernate和mysql作为... 查看详情

在 Oracle SQL 中使用正则表达式在字符串字段中查找 Unicode 字符

】在OracleSQL中使用正则表达式在字符串字段中查找Unicode字符【英文标题】:FindingUnicodeCharactersinStringFieldwithRegexinOracleSQL【发布时间】:2021-05-0601:40:06【问题描述】:我有一个字符串字段(cmets),其中包含一个用户ID,例如“THOMASAN... 查看详情

如何在 python 包装中使用 unicode 字符串用于带有 cython 的 c++ 类?

】如何在python包装中使用unicode字符串用于带有cython的c++类?【英文标题】:Howtouseunicodestringsinpythonwrappingforc++classwithcython?【发布时间】:2019-08-0719:23:22【问题描述】:我目前正在从事一个宠物项目。我现在的目标是用cython为pytho... 查看详情

如何在 Django 模型选择中使用 unicode 字符?

】如何在Django模型选择中使用unicode字符?【英文标题】:HowtohaveunicodecharactersinDjangomodelchoices?【发布时间】:2015-07-1117:40:50【问题描述】:我正在尝试在字段选择中使用特殊字符。我有以下代码:CHOICES=((\'1\',\'b\'),(\'2\',\'p\'),(\'3\... 查看详情

如何使用 Gmail API 中的 Drafts.send 在主题中发送 unicode 字符?

】如何使用GmailAPI中的Drafts.send在主题中发送unicode字符?【英文标题】:HowtosendunicodecharactersinsubjectusingDrafts.sendinGmailAPI?【发布时间】:2014-08-2321:08:59【问题描述】:如果用户在gmailWebUI中创建了主题中包含Unicode字符的草稿,则调... 查看详情

如何在javascript中获取字符的unicode代码点?

我正在使用条形码扫描仪读取我网站上的条形码(该网站是在OpenUI5中制作的)。扫描仪的工作方式类似于键入其所读取字符的键盘。在键入的结尾和开头,它使用特殊字符。这些字符对于每种类型的扫描仪都是不同的。一些可... 查看详情

关于unicode和多字符字集。

我用vs2010写了个socket的小程序。客户端原先用Unicode写的,可是服务端收到乱码。改成多字符字集后就能正常显示了。这是什么原因呢?我若想保持使用Unicode字符集写,该怎么改呢?使用多字符字集写有什么坏处呢?问题解决是... 查看详情

“填充”标签中的 Unicode 字符

】“填充”标签中的Unicode字符【英文标题】:\'Fill\'Unicodecharactersinlabels【发布时间】:2013-09-1205:33:43【问题描述】:如何在Swing的标签中“填充”Unicode字符?我正在尝试为我最近编写的国际象棋程序制作用户界面(与上图类似... 查看详情

如何在 Python 中获得可靠的 unicode 字符数?

】如何在Python中获得可靠的unicode字符数?【英文标题】:HowtogetareliableunicodecharactercountinPython?【发布时间】:2011-10-1820:17:36【问题描述】:GoogleAppEngine使用Python2.5.2,显然启用了UCS4。但GAE数据存储在内部使用UTF-8。因此,如果您... 查看详情

如何从雪花中删除 Unicode 替换字符

】如何从雪花中删除Unicode替换字符【英文标题】:HowtoremoveUnicodereplacementcharacterfromSnowflake【发布时间】:2020-02-2521:33:01【问题描述】:使用COPYINTO命令将数据加载到Snowflake时,有一个名为:REPLACE_INVALID_CHARACTERS的参数。根据文档... 查看详情

如何在 Perl 中找到 Unicode 字符串的长度?

】如何在Perl中找到Unicode字符串的长度?【英文标题】:HowdoIfindthelengthofaUnicodestringinPerl?【发布时间】:2010-11-2212:42:37【问题描述】:length()的perldoc页面告诉我应该使用bytes::length(EXPR)来查找以字节为单位的Unicode字符串,或者bytes... 查看详情