zoukankan      html  css  js  c++  java
  • c#控制和切图 PDF文档

    1、添加 AcroPDFLib.dll和 AxAcroPDFLib.dll

    本例使用Winform,在设计器工具箱中会找不到AxAcroPDFLib.AxAcroPDF 控件,需在选择项中COM组件添加Adobe PDF Reader和Adobe Aceobat DC BrowerControl Implementation

    2、打开pdf文档 

    (1)打开文档路径

    private void btnSelect_Click(object sender, EventArgs e)
    {
    OpenFileDialog openFile = new OpenFileDialog();
    DialogResult result = openFile.ShowDialog();
    
    if (result == DialogResult.Cancel)
    {
    return;
    }
    string strFileName = openFile.FileName;
    
    string strFileNameDup = strFileName;
    
    if (strFileNameDup.ToUpper().EndsWith(".PDF"))
    {
    txtFilePath.Text = strFileName;
    }
    else
    {
    MessageBox.Show("!!", "请选择PDF文件", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
    MessageBoxDefaultButton.Button1);
    }
    }

    (2)打开PDF

    private void btnOpen_Click(object sender, EventArgs e)
    {
    if (txtFilePath.TextLength == 0)
    {
    MessageBox.Show("请选择文件");
    }
    StringBuilder shortFileName = new StringBuilder(4089);
    uint shortFileNameSize = (uint)shortFileName.Capacity;
    if (GetShortPathName(txtFilePath.Text, shortFileName, shortFileNameSize) != 0)
    {
    axAcroPDF.LoadFile(shortFileName.ToString());
    }
    else
    {
    MessageBox.Show("未找到文件");
    }
    }

    3、页面操作

     (1)简单页面操控axAcroPDF.gotoPreviousPage();//上页

    axAcroPDF.gotoNextPage();//下页

     axAcroPDF.gotoFirstPage();//首页

    axAcroPDF.gotoLastPage();//尾页

    (2)跳转到指定页

     axAcroPDF.setCurrentPage(Convert.ToInt32(txtIputPage.Text));

    其中txtIputPage.Text为见面控件text传入 的值。

    4、pdf转换成图片

    添加O2S.Components.PDFRender4NET.dll的引用

    (1)获取PDF文档页数

    FileStream fs = new FileStream(pdfPath, FileMode.Open, FileAccess.Read);
    StreamReader r = new StreamReader(fs);
    string pdfText = r.ReadToEnd();//获取PDF文本
    Regex rx1 = new Regex(@"/Types*/Page[^s]");//使用正则表达式计算:"/Type /Page" 标记出现的次数,计算pdf有多少页
    MatchCollection matches = rx1.Matches(pdfText);

    (2)转换图片

    public static void PDFTOImage(string InputPath, string imageOutputPath,
    int startPageNum, int endPageNum, ImageFormat imageFormat)
    {
    PDFFile pdfFile = PDFFile.Open(InputPath);
    if (startPageNum <= 0)
    {
    startPageNum = 1;
    }
    if (endPageNum > pdfFile.PageCount)
    {
    endPageNum = pdfFile.PageCount;
    }
    if (startPageNum > endPageNum)
    {
    int tempPageNum = startPageNum;
    startPageNum = endPageNum;
    endPageNum = startPageNum;
    }
    // 转换成图片
    for (int i = startPageNum; i <= endPageNum; i++)
    {
    Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * 5);
    pageImage.Save(imageOutputPath + "Convent" + i.ToString() + "." + imageFormat.ToString(), imageFormat);
    pageImage.Dispose();
    }
    pdfFile.Dispose();
    }
  • 相关阅读:
    更改套接字I/O缓冲大小
    读取创建套接字时默认IO缓冲大小
    利用getsockopt读取套接字可选项
    如何查看安装的ubuntu是多少位的系统
    使用虚函数所带来的扩展性
    python学习第17天----接口类/抽象类、多态、封装
    python学习第16天----继承、查找顺序(深度、广度优先)
    python学习第15天----名称空间、组合
    python学习第14天----函数复习、面向对象初始
    python学习第13天----lambda、sorted、map、filter、递归、二分查找
  • 原文地址:https://www.cnblogs.com/machenghu/p/6418810.html
Copyright © 2011-2022 走看看