zoukankan      html  css  js  c++  java
  • C#打印页面的纸张设置问题Spread表格控件

    这段时间学习spread控件,用到打印设置上边,其他的设置都还好说,但是打印纸张的大小,纸张类型等把我折腾的够呛,找了半天才找到,记录下来备查。

    1、打印纸张类型:

    System.Drawing.Printing.PaperSize pkSize;
    System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
    for (int i = 0; i < printDoc.PrinterSettings.PaperSizes.Count; i++)
    {//这个是获取纸张类型,即A4、A3这些类型名称
    pkSize = printDoc.PrinterSettings.PaperSizes[i];
    combPaperType.Items.Add(pkSize.PaperName);
    }

    combPaperType.Text = "A4";

    2、打印纸张来源:

    System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();

    for (int i = 0; i < ps.PaperSources.Count; i++)
    {//这个是获取纸张来源,即打印机中纸张的位置信息等,如纸盒1、纸盒2
     combPaperType.Items.Add(ps.PaperSources[i].SourceName);
    }
    combPaperType.SelectedIndex = 0;

    3、打印纸张大小:

    设置纸张大小:

    System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();

    numPaperHeight.Value = PXToCM(printDoc.PrinterSettings.PaperSizes[combPaperType.SelectedIndex].Height,0);
    numPaperWidth.Value = PXToCM(printDoc.PrinterSettings.PaperSizes[combPaperType.SelectedIndex].Width,1);

    获取纸张大小

    FarPoint.Win.Spread.PrintInfo pi = new FarPoint.Win.Spread.PrintInfo();

    pi.PaperSize = new System.Drawing.Printing.PaperSize(combPaperType.Text, CMToPX(iX,0),  CMToPX(iY,1));


    /// <summary>
    /// 像素转化为mm,返回0.1MM单位
    /// </summary>
    /// <param name="px">像素</param>
    /// <param name="x">0:X轴宽度;1:Y轴高度</param>
    /// <returns></returns>
    private int PXToCM(int px,int x)
    {
    float dpi = this.CreateGraphics().DpiX;
    if (x == 1)
    {
    dpi = this.CreateGraphics().DpiY;
    }

    return Convert.ToInt32((px ) * (254 )/ (dpi ));
    }
    /// <summary>
    /// MM转化为像素
    /// </summary>
    /// <param name="mm">单位:0.1mm</param>
    /// <param name="x">0:X轴宽度;1:Y轴高度</param>
    /// <returns></returns>
    private int CMToPX(int mm, int x)
    {
    float dpi = this.CreateGraphics().DpiX;
    if (x == 1)
    {
    dpi = this.CreateGraphics().DpiY;
    }
    return Convert.ToInt32(((mm) * (dpi ) / (254)));
    }

    纸张大小,不能直接用系统函数得到的值,系统函数得到的值是像素,spread打印时设置PaperSize的值也是像素,但是我们在外部使用时应该显示为MM(毫米);这个时候需要先根据选中的纸张类型通过系统方法得到系统的纸张大小(像素),将该值转换为MM后显示在NumericUpDown控件中,在实际打印或打印预览时,再讲该值转换为像素,添加到spread控件的PaperSize属性中,进行实际打印。(如果不考虑手工修改纸张大小的问题,到时不用来回转换了)

    这个就需要进行数值转换。需要按下列公式进行转换:

    //比例:一英寸等于25.4mm;(dpi值/比例)*长度=像素
    //那么毫米换算成像素的公式为
    //水平方向的换算: x * px /25.4
    //垂直方向的换算: y * py /25.4
    //像素换算为毫米 x * 25.4 / px

    转换时出现另外一个问题:float和int数据类型转换时会有差值出现,用Convert.ToInt32()转换好点。(不过不考虑这点差值也可以,仅1、2MM的差距)

  • 相关阅读:
    python学习笔记 async and await
    python学习笔记 异步asyncio
    python学习笔记 协程
    python学记笔记 2 异步IO
    python学习笔记 可变参数关键字参数**kw相关学习
    逆波兰表达式 栈表达式计算
    Codeforces 270E Flawed Flow 网络流问题
    Codeforces 219D Choosing Capital for Treeland 2次DP
    kuangbin 带你飞 概率期望
    函数式编程思想:以函数的方式思考,第3部分
  • 原文地址:https://www.cnblogs.com/jiutianxingchen/p/3636157.html
Copyright © 2011-2022 走看看