开启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}");
                }
            }
        }

效果

相关推荐
其实秋天的枫1 天前
【2026年最新】驾考科目一考试题库2309道电子版pdf
经验分享·pdf
墨染天姬1 天前
【AI】如何基于cursor创建MCP索引pdf
人工智能·pdf
qq_429499571 天前
分享免费的PDF 翻译 原格式
pdf
来自外太空的鱼-张小张1 天前
jeecg预览pdf、jeecg无法预览pdf、jeecg自带预览pdf
pdf·状态模式
彧翎Pro1 天前
ASP.NET Core 外部依赖调用治理实战:HttpClientFactory、Polly 与幂等边界
microsoft·asp.net·php
火山引擎开发者社区1 天前
从监控盲区到业务洞察:深入解读 APMPlus 生产指标
大数据·人工智能·microsoft
SEO-狼术1 天前
All-About-PDF provides DRM
pdf
好运的阿财1 天前
OpenClaw四种角色详解
人工智能·python·程序人生·microsoft·开源·ai编程
Access开发易登软件1 天前
在 Access 中实现 Web 风格 To Do List
前端·数据结构·microsoft·list·vba·access·access开发
宝桥南山1 天前
GitHub Copilot - 尝试使用一下GitHub Copilot SDK
microsoft·ai·微软·github·aigc·copilot