0

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

粗体标题和常规字体样式的其余文本。这是打印预览!

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

在此处输入图像描述

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

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);
        }
    }
}
4

0 回答 0