显示但不打印?

时间:2013-10-11 14:41:13

标签: c#

我正在尝试打印如下所示的文档

标题以粗体显示,其余文本采用常规字体样式。这是打印预览!

问题是,在打印之后,不会显示粗体标题,但会留下空间。如何解决这个问题?

enter image description here

我编写了以下类来打印该文档。

using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Printing;

namespace documentPrinter
{
    public partial class documentPrinter : PrintDocument
    {
        public String documenTitle { get; set; }
        public String doContent { get; set; }
        String contenToPrint;
        public Font contentFont { get; set; }
        public Font titleFont { get; set; }
        bool prinTitle = true;

        protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
        {
            PageSettings PgS = e.PageSettings;
            PgS.Color = false;
            PgS.Landscape = false;
            PgS.PaperSize = new PaperSize("A4", 827, 1169);
            PgS.Margins = new Margins(100, 100, 100, 100);
            //base.OnQueryPageSettings(e);
        }

        protected override void OnBeginPrint(PrintEventArgs e)
        {
            contenToPrint = "\n\n\n" + doContent;
            //base.OnBeginPrint(e);
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;

            // Sets the value of charactersOnPage to the number of characters  
            // of stringToPrint that will fit within the bounds of the page.

            if (prinTitle)
            {
                e.Graphics.MeasureString(documenTitle, 
                                         titleFont,
                                         e.MarginBounds.Size,
                                         StringFormat.GenericTypographic,
                                         out charactersOnPage,
                                         out linesPerPage);
                e.Graphics.DrawString(documenTitle, 
                                      titleFont,
                                      Brushes.Black,
                                      e.MarginBounds,
                                      StringFormat.GenericTypographic);
                prinTitle = false;
            }

            e.Graphics.MeasureString(contenToPrint,
                                     contentFont,
                                     e.MarginBounds.Size,
                                     StringFormat.GenericTypographic, 
                                     out charactersOnPage,
                                     out linesPerPage);

            // Draws the string within the bounds of the page.
            e.Graphics.DrawString(contenToPrint, contentFont, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic);

            // Remove the portion of the string that has been printed.
            contenToPrint = contenToPrint.Substring(charactersOnPage); 

            // Check to see if more pages are to be printed.
            e.HasMorePages = (contenToPrint.Length > 0);

            //base.OnPrintPage(e);
        }
    }
}

0 个答案:

没有答案