2013-08-19 33 views
1

我想打印一些像这样的文字。用字体样式打印C#

这是如何我想打印文本。

我使用的代码是

private void button3_Click(object sender, EventArgs e) 
    { 
     stringToPrint = "This is how i want to print the text"; 
     printFont = new Font("Times New Roman", 10); 
     pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); 
     try 
     { 
      pd.Print(); 
     } 
     catch (Exception e) 
     { 
     } 
    } 

void pd_PrintPage(object sender, PrintPageEventArgs ev) 
    { 
     int charactersOnPage = 0; 
     int linesPerPage = 0; 

     ev.Graphics.MeasureString(stringToPrint, printFont, 
      ev.MarginBounds.Size, StringFormat.GenericTypographic, 
      out charactersOnPage, out linesPerPage); 

     ev.Graphics.DrawString(stringToPrint, printFont, Brushes.Black, 
      ev.MarginBounds, StringFormat.GenericTypographic); 

     stringToPrint = stringToPrint.Substring(charactersOnPage); 

     ev.HasMorePages = (stringToPrint.Length > 0); 

    } 

我想改变字体从常规到粗体或给一个下划线的字符串在一些特定的单词。

如果还有其他更好的方法来完成这项工作,那么请告诉我,我会改变我的代码。 请帮帮我! :)

+0

你是否也有被你打印之前在同一文件中创建多个fontstyles任何成功? –

+0

不,如果我这样做了,那么它不会是一个问题! –

+0

您是否收到错误,或者是它只是将相同字体全部打印出来的问题? –

回答

-5

你可以试试:

printFont = new Font("Arial", 24,FontStyle.Bold); 

ThangNguyen

+3

这将会加粗他字符串中的所有单词,这不是他想要的。他的问题根本不容易。 –