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

效果

相关推荐
原子延迟8 小时前
iLovePDF免费吗?不限次数的PDF压缩5款(2026)
图像处理·人工智能·计算机视觉·pdf
2601_965454238 小时前
AI视频制作:券商研报怎么转口播视频:PDF 解读、配音和图表自动成片的方法
人工智能·pdf·音视频
燕落南枝8 小时前
HTML 表单入门:表单容器、文本框与密码框属性详解
前端·microsoft·html
reasonsummer18 小时前
【办公类-53-12】20261016(预测)Python模仿制作2026学年第二学期校历(excel+pdf,上下学期都包含)
pdf·excel
开开心心就好1 天前
多份文档怎么批量打印?支持多种格式
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
开开心心就好1 天前
文件被占用删不掉?解锁工具强制删除
java·运维·服务器·开发语言·前端·pdf·ocr
禁默1 天前
机器人的手,为什么拿个杯子都这么难?一文看懂“灵巧操作“
人工智能·microsoft·机器人
BD_Marathon1 天前
PyPDFLoader和MinerU加载pdf文件
pdf
小鹿软件办公2 天前
微软公布更多 AI 功能加持的 Windows 11 全新“照片”应用
人工智能·microsoft
白雪讲堂2 天前
从流量到增长:庞文英《GEO实战:AI时代的增长密码》重磅上市,开启AI营销新纪元
microsoft