C# 实现文件夹多PDF元数据分页(First/Next)查看功能咨询
实现方案
第一步:定义类级全局变量
在当前窗体的类中新增两个变量,用于存储当前目录下所有PDF路径和当前查看的索引:
// 存储当前打开PDF所在目录的所有PDF文件路径 private List<string> _currentPdfList; // 存储当前查看的PDF在列表中的索引 private int _currentPdfIndex = -1;
第二步:改造原有Info_Click事件,初始化PDF列表
在原有点击查看元数据的逻辑中,新增同目录PDF扫描、索引初始化的逻辑:
private void Info_Click(object sender, EventArgs e) { // 先获取当前打开的PDF的完整路径,不同组件属性名可能有差异,可根据实际使用的PDF组件调整 string currentPdfPath = pdfViewer1.DocumentPath; if (string.IsNullOrEmpty(currentPdfPath) || !File.Exists(currentPdfPath)) return; // 扫描同目录下所有PDF文件,按文件名排序 string pdfDir = Path.GetDirectoryName(currentPdfPath); _currentPdfList = Directory.GetFiles(pdfDir, "*.pdf", SearchOption.TopDirectoryOnly) .OrderBy(p => p) .ToList(); // 定位当前打开PDF的索引 _currentPdfIndex = _currentPdfList.FindIndex(p => p.Equals(currentPdfPath, StringComparison.OrdinalIgnoreCase)); // 原有元数据加载逻辑 PdfInformation info = pdfViewer1.Document.GetInformation(); StringBuilder sz = new StringBuilder(); // 新增当前文件名显示,方便用户识别 sz.AppendLine($"FileName:\t {Path.GetFileName(currentPdfPath)}"); sz.AppendLine($"Title:\t\t {info.Title}"); sz.AppendLine($"Subject:\t\t {info.Subject}"); sz.AppendLine($"Author:\t\t {info.Author}"); InfoForm1_Link.Show(); InfoForm1_Link.InfoText = sz.ToString(); }
第三步:封装公共的PDF元数据加载方法
把重复的读取元数据、更新窗体的逻辑抽为公共方法,避免每个按钮事件写重复代码:
private void LoadPdfMetadata(int targetIndex) { // 边界校验,不合法的索引直接返回 if (_currentPdfList == null || _currentPdfList.Count == 0) return; if (targetIndex < 0 || targetIndex >= _currentPdfList.Count) return; _currentPdfIndex = targetIndex; try { // 单独加载PDF文件读取元数据,不会修改主界面pdfViewer1显示的内容 // 如果你需要同时切换主界面的PDF显示,可替换为pdfViewer1.Load()的逻辑 using (PdfDocument doc = PdfDocument.Load(_currentPdfList[_currentPdfIndex])) { PdfInformation info = doc.GetInformation(); StringBuilder sz = new StringBuilder(); sz.AppendLine($"FileName:\t {Path.GetFileName(_currentPdfList[_currentPdfIndex])}"); sz.AppendLine($"Title:\t\t {info.Title}"); sz.AppendLine($"Subject:\t\t {info.Subject}"); sz.AppendLine($"Author:\t\t {info.Author}"); if (InfoForm1_Link != null && !InfoForm1_Link.IsDisposed) { InfoForm1_Link.InfoText = sz.ToString(); } } } catch (Exception ex) { // 处理文件损坏、被占用等异常场景 if (InfoForm1_Link != null && !InfoForm1_Link.IsDisposed) { InfoForm1_Link.InfoText = $"当前文件加载失败:{ex.Message}"; } } // 可选:更新按钮可用状态,避免用户点到无效边界 if (InfoForm1_Link != null && !InfoForm1_Link.IsDisposed) { // 需提前在InfoForm1中把四个按钮的Enabled属性暴露为公共属性 InfoForm1_Link.FirstBtnEnabled = _currentPdfIndex > 0; InfoForm1_Link.PreviousBtnEnabled = _currentPdfIndex > 0; InfoForm1_Link.NextBtnEnabled = _currentPdfIndex < _currentPdfList.Count - 1; InfoForm1_Link.LastBtnEnabled = _currentPdfIndex < _currentPdfList.Count - 1; } }
第四步:填充四个按钮的事件逻辑
直接调用封装好的公共方法即可:
InfoForm1_Link.ButtonInfoFormClickedFirst += (s, eF) => { LoadPdfMetadata(0); }; InfoForm1_Link.ButtonInfoFormClickedPrevious += (s, eP) => { LoadPdfMetadata(_currentPdfIndex - 1); }; InfoForm1_Link.ButtonInfoFormClickedNext += (s, eN) => { LoadPdfMetadata(_currentPdfIndex + 1); }; InfoForm1_Link.ButtonInfoFormClickedLast += (s, eL) => { LoadPdfMetadata(_currentPdfList.Count - 1); };
注意事项
- 代码中
PdfDocument.Load为对应PDF组件的加载方法,如果你使用的是其他PDF类库,替换为对应API即可。 - 如果需要切换元数据时同步更新主界面的PDF预览,把第三步中单独加载PdfDocument的逻辑替换为
pdfViewer1.Load(_currentPdfList[_currentPdfIndex]),再从pdfViewer1中拿元数据即可。
内容的提问来源于stack exchange,提问作者EdV
相关产品推荐
相关产品推荐

