开启Microsoft Print To PDF开启自定纸张 C#

有时需要测试打印效果,Microsoft Print To PDF不能设定自定义指定大小,所以写了个软件实现,以下是C#部分代码

cs 复制代码
          private void btn_开启自定纸张_Click(object sender, EventArgs e)
        {
            try
            {
                // 读取注册表值
                string driverDirectory = ReadRegistryValue();
                if (string.IsNullOrEmpty(driverDirectory))
                {
                    LogMessage("无法获取打印机驱动目录!");
                    return;
                }
                string V4Dir = "C:\\Windows\\System32\\spool\\V4Dirs\\";
                // 查找并修改GPD文件
                ModifyGpdFiles_AddStr($"{V4Dir}{driverDirectory}");

                LogMessage("自定义纸张设置已开启");
            }
            catch (Exception ex)
            {
                LogMessage($"操作失败: {ex.Message}");
            }
        }

      private void ModifyGpdFiles_AddStr(string driverDirectory)
        {
            if (!Directory.Exists(driverDirectory))
            {
                LogMessage($"驱动目录不存在: {driverDirectory}");
                return;
            }

            string[] gpdFiles = Directory.GetFiles(driverDirectory, "*.gpd");
            if (gpdFiles.Length == 0)
            {
                LogMessage($"在目录中未找到GPD文件: {driverDirectory}");
                return;
            }

            string customSizeOption = 
@"*Option: CUSTOMSIZE
{
*rcNameID: =USER_DEFINED_SIZE_DISPLAY
*MinSize: PAIR(0, 0)
*MaxSize: PAIR(5346000, 7560000)
*MaxPrintableWidth: 5346000
}";
            foreach (string gpdFile in gpdFiles)
            {
                try
                {
                    string content = File.ReadAllText(gpdFile, Encoding.Default);

                    // 检查是否已存在CUSTOMSIZE选项
                    if (!content.Contains("*Option: CUSTOMSIZE"))
                    {
                        // 查找*Option: A5部分
                        int optionA5Index = content.IndexOf("*Option: A4");
                        if (optionA5Index >= 0)
                        {
                            // 在A5选项前插入自定义纸张选项
                            string modifiedContent = content.Insert(optionA5Index, customSizeOption + Environment.NewLine);
                            File.WriteAllText(gpdFile, modifiedContent, Encoding.Default);
                            LogMessage($"已在A5选项前添加内容: {gpdFile}");
                        }
                        else
                        {
                            LogMessage($"未找到A5选项,跳过文件: {gpdFile}");
                        }
                    }
                    else
                    {
                        LogMessage($"{gpdFile}文件已包含自定义纸张设置");
                    }
                }
                catch (Exception ex)
                {
                    LogMessage($"处理文件时出错 {Path.GetFileName(gpdFile)}: {ex.Message}");
                }
            }
        }

效果

相关推荐
AI导出鸭PC端10 小时前
Perplexity批量导出没有PDF模式?AI导出鸭插件补上最后一块拼图
人工智能·pdf·c#·ai导出鸭
Ravenq~11 小时前
无头转换:把 .docx 变成 PDF,不打开编辑器
pdf·编辑器
联犀1 天前
纯 Go 的 CAD 开源库 go-cad:直接解析 DWG/DXF,现在也能导出 PDF
开发语言·golang·pdf
解决问题no解决代码问题2 天前
日志脱敏双链路统一优化:从两套枚举到单一规则源,兼顾安全与可维护性
microsoft
Dovis(誓平步青云)2 天前
启元 T1 Pro 人机相声搭档:语音-触摸双通道的启元机器人节奏引擎设计
开发语言·前端·javascript·microsoft·交互
宝桥南山2 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
Liangwei Lin3 天前
Exchange Server 2019 部署手册
microsoft
ms365copilot4 天前
Microsoft 365 Copilot跨工作资料整理
microsoft·copilot
中二骚年4 天前
Windows 安全中心 / 防火墙故障,calc和Microsoft Store,终端(管理员)无法正常打开。 原因 + 修复方案
windows·安全·microsoft
FinelyYang4 天前
uniapp使用kkfileview在线预览ppt,excel,word,pdf类型文件
pdf·uni-app·powerpoint