Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi in my application im trying to pring the current page but it throughs an error that "Font 'Monotype Corsiva' does not support style 'Regular'."
check my below code. asp.net c# code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing.Text;

public partial class LandScapePrint : System.Web.UI.Page
{
    private int fontcount;
    private int fontposition = 1;
    private float ypos = 1;
    private PrintPreviewDialog previewDlg = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //InstalledFontCollection ifc = new InstalledFontCollection();
        ////Get font families
        //FontFamily[] ffs = ifc.Families;
        //Font f;
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
          
            //PrintDocument pd = new PrintDocument();
            ////Add print-page event handler
            //pd.PrintPage+=new PrintPageEventHandler(this.pd_PrintPage);
            //pd.DefaultPageSettings.Landscape = true;
            //pd.Print();
        previewDlg = new PrintPreviewDialog();
        //Create a PrintDocument object
        PrintDocument pd = new PrintDocument();
        //Add print-page event handler
        pd.PrintPage +=new PrintPageEventHandler(this.pd_PrintPage);
      
        pd.Print();
    }

    protected void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        ypos = 1;
            float pageheight = e.MarginBounds.Height;
            //Create a Graphics object
            Graphics g = e.Graphics;
            //Get installed fonts
            InstalledFontCollection ifc =
            new InstalledFontCollection();
            //Get font families
            FontFamily[] ffs = ifc.Families;
            //Draw string on the paper
            while (ypos + 60 < pageheight &&
            fontposition < ffs.GetLength(0))
            {
                //Get the font name 
                Font f =
                new Font(ffs[fontposition].GetName(0), 25);
                //if (ffs.ToString()=="Monotype Corsiva")
                //{
                //    ffs=FontStyle
                //}
                //Draw string
                g.DrawString(ffs[fontposition].GetName(0), f,
                new SolidBrush(Color.Black), 1, ypos);
                fontposition = fontposition + 1;
                ypos = ypos + 60;
            }
            if (fontposition < ffs.GetLength(0))
            {
                //Has more pages??
                e.HasMorePages = true;
            }
    }
}


and the html code is;

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LandScapePrint.aspx.cs" Inherits="LandScapePrint" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title></title>
    <style type="text/css">
        .style1
        {
            font-family: Arial;
        }
    </style>
</head>
<body>
    <form id="Form1" runat="server">
        <table>
            <tbody>
                <tr>
                    <td colspan="2" class="style1">
                        <strong>Test Form</strong></td>
                </tr>
                <tr>
                    <td class="style1">
                        First&nbsp;Name</td>
                    <td class="style1">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style1">
                        Last&nbsp;Name</td>
                    <td class="style1">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button id="Submit" onclick="Submit_Click"
                              runat="server" Text="Submit" CssClass="style1"></asp:Button>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>
Posted
Comments
Parviz Bazgosha 25-Jul-13 6:25am    
becasue your font dont have that style .

for fix problem you must download that font .(i think thats can get copyed form computer which
have windows xp ) . or change your font style to bold .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900