在同一文档中以横向和纵向打印

时间:2014-04-12 20:58:38

标签: c# winforms graphics

我的客户希望以横向打印字母,例如

  

ABCDE

并在同一页面上打印另一个字母,但是从上到下以纵向打印,例如:

A
B
C
D
E

在c#中使用windows窗体中的打印控件。那我怎么能这样做? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 你可以用很多语言来做,只需按照本网站的每一步操作即可 你能做到这一点。 http://www.aspose.com/docs/display/wordsnet/How+to++Print+Multiple+Pages+on+One+Sheet 它也有助于你的方向选择
  2. 用于设置方向使用此代码
  3.  protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
        {
            // Run base code
            base.OnPrintPage(e);
    
            //Declare local variables needed
    
            int printHeight;
            int printWidth;
            int leftMargin;
            int rightMargin;
            Int32 lines;
            Int32 chars;
    
            //Set print area size and margins
            {
                printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
                printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
                leftMargin = base.DefaultPageSettings.Margins.Left;  //X
                rightMargin = base.DefaultPageSettings.Margins.Top;  //Y
            }
    
            //Check if the user selected to print in Landscape mode
            //if they did then we need to swap height/width parameters
            if (base.DefaultPageSettings.Landscape)
            {
                int tmp;
                tmp = printHeight;
                printHeight = printWidth;
                printWidth = tmp;
            }
    
            //Now we need to determine the total number of lines
            //we're going to be printing
            Int32 numLines = (int)printHeight / PrinterFont.Height;
    
            //Create a rectangle printing are for our document
            RectangleF printArea = new RectangleF(leftMargin, rightMargin, printWidth, printHeight);
    
            //Use the StringFormat class for the text layout of our document
            StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
    
            //Fit as many characters as we can into the print area      
    
            e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, new SizeF(printWidth, printHeight), format, out chars, out lines);
    
            //Print the page
            e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format);
    
            //Increase current char count
            curChar += chars;
    
            //Detemine if there is more text to print, if
            //there is the tell the printer there is more coming
            if (curChar < _text.Length)
            {
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                curChar = 0;
            }
        }
    
        #endregion