zoukankan      html  css  js  c++  java
  • 获取指定路径下所有PDF文件的总页数

    在开发过程中遇见了这样一个问题,某个文件夹下包含了很多PDF文件,现在要统计这些文件的总页数,当然可以逐个打开,然后将页数累加起来,但是相对来说很麻烦,于是写了一个winform的程序,来实现页数的统计!

    客户端界面:

    选择按钮后台代码:

             /// <summary>
             /// 浏览目录
          /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnPath_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog myDialog = new FolderBrowserDialog();
                myDialog.ShowNewFolderButton = false;
                myDialog.Description = "选择目录";
                if (myDialog.ShowDialog()==DialogResult.OK)
                {
                    txtPath.Text = myDialog.SelectedPath;
                }
            }

    获取页数按钮后台代码:

            /// <summary>
            /// 统计路径下所有PDF文件的页数
          /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                int pdfCounts = 0;
                if (String.IsNullOrEmpty(txtPath.Text))
                {
                    MessageBox.Show("选择文件路径");
                }
                else
                {
                    string[] pdfFiles = System.IO.Directory.GetFiles(txtPath.Text);
                    foreach (string pdfFile in pdfFiles)
                    {
                        pdfCounts = pdfCounts + GetPageCount(pdfFile);
                    }
                    txtCount.Text = pdfCounts.ToString();          
                }
            }
            /// <summary>
            /// 获取单个PDF文件的页数
          /// </summary>
            /// <param name="filePath"></param>
            /// <returns></returns>
            private int GetPageCount(string filePath)
            {
                PDDocument document = null;
                PDDocumentInformation info = null;
                int pages = 0;
                try
                {
                    document = PDDocument.load(filePath);
                    info = document.getDocumentInformation();
                    // 输出总页数
                    pages = document.getNumberOfPages();
    
                }
                catch (Exception e)
                {
                    string errorMessage = e.Message;
                    return 0;
                }
                finally
                {
                    if (null != document)
                        document.close();
    
                }
                return pages;
            
            }

    这里需要注意:方法GetPageCount适用的前提是,需要引入IKVM.GNU.Classpath.dll和PDFBox-0.7.3.dll 这两个文件都可以在网上查找到!

  • 相关阅读:
    青瓷qici
    青瓷qici
    青瓷qici
    青瓷引擎_珍膳小游戏_巧妇有好米来做煮
    2013年12月30日 星期一 谷歌、百度地图拽取
    不懂网络知识,测试工程师真的能走得很远么
    测试和开发之间的博弈--没有硝烟的战争
    docker命令每次需要sudo操作解决方案
    Ubuntu 18.04安装Docker CE + NVIDIA-Docker
    windos10下安装ubuntu18双系统
  • 原文地址:https://www.cnblogs.com/wangjianhui008/p/3243002.html
Copyright © 2011-2022 走看看