读书人

c# 打印解决方案

发布时间: 2012-03-29 12:53:13 作者: rapoo

c# 打印
代码

[解决办法]
没有,试试,JS打印。。。
<script language="javascript" type="text/javascript">
function printpage(myDiv){

//var newstr = document.all.item(myDiv).innerHTML;
var newstr = document.getElementById(myDiv).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
<div id="myDiv" >打印内容,内加任何东东,</div>
[解决办法]
webbrower打印
window.print()
调用Office实现WEB打印
导出报表
Graphics.DrawString 输出字符,导出打印
打印组件http://topic.csdn.net/u/20090921/20/625a532c-1c97-4bfb-bdf1-7a1ab04fd797.html
[解决办法]
<script type="text/javascript" language="javascript">
function printContent() {
var printContent = $("printtable").innerHTML;
var bodyContent = document.body.innerHTML;
document.body.innerHTML = printContent;
window.print();
document.body.innerHTML = bodyContent;
window.location.reload();
return;
}
</script>
自己有打印预览
[解决办法]
winfrom的话就需要用Graphics对象来打印了
[解决办法]
http://www.cnblogs.com/scottckt/archive/2008/01/31/1059576.html
http://developer.51cto.com/art/200908/146857.htm
[解决办法]
<script language="javascript" type="text/javascript">
function printpage(myDiv){

//var newstr = document.all.item(myDiv).innerHTML;
var newstr = document.getElementById(myDiv).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
<div id="myDiv" >打印内容div>


web打印也可以
[解决办法]
调用别人写好的代码的人飘过。。。
[解决办法]

HTML code
<HTML><HEAD><TITLE>网页打印去掉页眉页脚,以及如何控制不想打印出的页面元素【梨城在线】</TITLE><META http-equiv=Content-Type content="text/html; charset=gb2312"><SCRIPT language=javascript>function printpr()   //预览函数{document.all("qingkongyema").click();//打印之前去掉页眉,页脚document.all("dayinDiv").style.display="none"; //打印之前先隐藏不想打印输出的元素(此例中隐藏“打印”和“打印预览”两个按钮)var OLECMDID = 7;var PROMPT = 1; var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';document.body.insertAdjacentHTML('beforeEnd', WebBrowser); WebBrowser1.ExecWB(OLECMDID, PROMPT);WebBrowser1.outerHTML = "";document.all("dayinDiv").style.display="";//打印之后将该元素显示出来(显示出“打印”和“打印预览”两个按钮,方便别人下次打印)}function printTure()   //打印函数{    document.all('qingkongyema').click();//同上    document.all("dayinDiv").style.display="none";//同上    window.print();    document.all("dayinDiv").style.display="";}function doPage(){    layLoading.style.display = "none";//同上}</SCRIPT><script language="JavaScript">var hkey_root,hkey_path,hkey_keyhkey_root="HKEY_CURRENT_USER"hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"//设置网页打印的页眉页脚为空function pagesetup_null(){try{var RegWsh = new ActiveXObject("WScript.Shell")hkey_key="header" RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"")hkey_key="footer"RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"")}catch(e){}}//设置网页打印的页眉页脚为默认值function pagesetup_default(){try{var RegWsh = new ActiveXObject("WScript.Shell")hkey_key="header" RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b页码,&p/&P")hkey_key="footer"RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d")}catch(e){}}</script></HEAD><BODY leftMargin=0 topMargin=0 rightMargin=0 bottomMargin=0 style="BACKGROUND-POSITION: center 50%"><DIV align=center>你希望打印的内容放置此处..........SDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDSDSDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD  <table width="200" border="1">    <tr>      <td>1</td>      <td>2</td>    </tr>    <tr>      <td>3</td>      <td>4</td>    </tr>  </table>  <form name="form1" method="post" action="">    <input name="textfield" type="text" value="SSAASSSAAS">  </form>  <p>SASASASAAAAAAA</p></DIV><DIV align="center" id="dayinDiv" name="dayinDiv"><table width="100%" border="0" cellspacing="0" cellpadding="0">    <tr>      <td align="center">翻页代码及其他不需要打印的内容放置此处</td>    </tr></table><input type="button" class="tab" value="打印" onClick="printTure();">  <input type="button" class="tab" value="打印预览" onClick="printpr();"><input type="button" name="qingkongyema" id="qingkongyema" class="tab" value="清空页码" onClick="pagesetup_null()">  <input type="button" class="tab" value="恢复页码" onClick="pagesetup_default()"></DIV></BODY></HTML> 


[解决办法]
参观学习。
[解决办法]

C# code
// The funtion that print the title, page number, and the header row    private void DrawHeader(Graphics g)    {        CurrentY = (float)TopMargin;        // Printing the page number (if isWithPaging is set to true)        if (IsWithPaging)        {            PageNumber++;            string PageString = "Page " + PageNumber.ToString();            StringFormat PageStringFormat = new StringFormat();            PageStringFormat.Trimming = StringTrimming.Word;            PageStringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;            PageStringFormat.Alignment = StringAlignment.Far;            Font PageStringFont = new Font("Tahoma", 8, FontStyle.Regular, GraphicsUnit.Point);            RectangleF PageStringRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(PageString, PageStringFont).Height);            g.DrawString(PageString, PageStringFont, new SolidBrush(Color.Black), PageStringRectangle, PageStringFormat);            CurrentY += g.MeasureString(PageString, PageStringFont).Height;        }        // Printing the title (if IsWithTitle is set to true)        if (IsWithTitle)        {            StringFormat TitleFormat = new StringFormat();            TitleFormat.Trimming = StringTrimming.Word;            TitleFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;            if (IsCenterOnPage)                TitleFormat.Alignment = StringAlignment.Center;            else                TitleFormat.Alignment = StringAlignment.Near;            RectangleF TitleRectangle = new RectangleF((float)LeftMargin, CurrentY, (float)PageWidth - (float)RightMargin - (float)LeftMargin, g.MeasureString(TheTitleText, TheTitleFont).Height);            g.DrawString(TheTitleText, TheTitleFont, new SolidBrush(TheTitleColor), TitleRectangle, TitleFormat);            CurrentY += g.MeasureString(TheTitleText, TheTitleFont).Height;        }        // Calculating the starting x coordinate that the printing process will start from        float CurrentX = (float)LeftMargin;        if (IsCenterOnPage)                        CurrentX += (((float)PageWidth - (float)RightMargin - (float)LeftMargin) - mColumnPointsWidth[mColumnPoint]) / 2.0F;        // Setting the HeaderFore style        Color HeaderForeColor = TheDataGridView.ColumnHeadersDefaultCellStyle.ForeColor;        if (HeaderForeColor.IsEmpty) // If there is no special HeaderFore style, then use the default DataGridView style            HeaderForeColor = TheDataGridView.DefaultCellStyle.ForeColor;        SolidBrush HeaderForeBrush = new SolidBrush(HeaderForeColor);        // Setting the HeaderBack style        Color HeaderBackColor = TheDataGridView.ColumnHeadersDefaultCellStyle.BackColor;        if (HeaderBackColor.IsEmpty) // If there is no special HeaderBack style, then use the default DataGridView style            HeaderBackColor = TheDataGridView.DefaultCellStyle.BackColor;        SolidBrush HeaderBackBrush = new SolidBrush(HeaderBackColor);        // Setting the LinePen that will be used to draw lines and rectangles (derived from the GridColor property of the DataGridView control)        Pen TheLinePen = new Pen(TheDataGridView.GridColor, 1);        // Setting the HeaderFont style        Font HeaderFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;        if (HeaderFont == null) // If there is no special HeaderFont style, then use the default DataGridView font style            HeaderFont = TheDataGridView.DefaultCellStyle.Font;        // Calculating and drawing the HeaderBounds                RectangleF HeaderBounds = new RectangleF(CurrentX, CurrentY, mColumnPointsWidth[mColumnPoint], RowHeaderHeight);        g.FillRectangle(HeaderBackBrush, HeaderBounds);        // Setting the format that will be used to print each cell of the header row        StringFormat CellFormat = new StringFormat();        CellFormat.Trimming = StringTrimming.Word;        CellFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;        // Printing each visible cell of the header row        RectangleF CellBounds;        float ColumnWidth;                for (int i = (int)mColumnPoints[mColumnPoint].GetValue(0); i < (int)mColumnPoints[mColumnPoint].GetValue(1); i++)        {            if (!TheDataGridView.Columns[i].Visible) continue; // If the column is not visible then ignore this iteration            ColumnWidth = ColumnsWidth[i];            // Check the CurrentCell alignment and apply it to the CellFormat            if (TheDataGridView.ColumnHeadersDefaultCellStyle.Alignment.ToString().Contains("Right"))                CellFormat.Alignment = StringAlignment.Far;            else if (TheDataGridView.ColumnHeadersDefaultCellStyle.Alignment.ToString().Contains("Center"))                CellFormat.Alignment = StringAlignment.Center;            else                CellFormat.Alignment = StringAlignment.Near;            CellBounds = new RectangleF(CurrentX, CurrentY, ColumnWidth, RowHeaderHeight);            // Printing the cell text            g.DrawString(TheDataGridView.Columns[i].HeaderText, HeaderFont, HeaderForeBrush, CellBounds, CellFormat);            // Drawing the cell bounds            if (TheDataGridView.RowHeadersBorderStyle != DataGridViewHeaderBorderStyle.None) // Draw the cell border only if the HeaderBorderStyle is not None                g.DrawRectangle(TheLinePen, CurrentX, CurrentY, ColumnWidth, RowHeaderHeight);            CurrentX += ColumnWidth;        }        CurrentY += RowHeaderHeight;    }    // The function that print a bunch of rows that fit in one page    // When it returns true, meaning that there are more rows still not printed, so another PagePrint action is required    // When it returns false, meaning that all rows are printed (the CureentRow parameter reaches the last row of the DataGridView control) and no further PagePrint action is required 


[解决办法]

C# code
private bool DrawRows(Graphics g)    {        // Setting the LinePen that will be used to draw lines and rectangles (derived from the GridColor property of the DataGridView control)        Pen TheLinePen = new Pen(TheDataGridView.GridColor, 1);        // The style paramters that will be used to print each cell        Font RowFont;        Color RowForeColor;        Color RowBackColor;        SolidBrush RowForeBrush;        SolidBrush RowBackBrush;        SolidBrush RowAlternatingBackBrush;        // Setting the format that will be used to print each cell        StringFormat CellFormat = new StringFormat();        CellFormat.Trimming = StringTrimming.Word;        CellFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;        // Printing each visible cell        RectangleF RowBounds;        float CurrentX;        float ColumnWidth;        while (CurrentRow < TheDataGridView.Rows.Count)        {            if (TheDataGridView.Rows[CurrentRow].Visible) // Print the cells of the CurrentRow only if that row is visible            {                // Setting the row font style                RowFont = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.Font;                if (RowFont == null) // If the there is no special font style of the CurrentRow, then use the default one associated with the DataGridView control                    RowFont = TheDataGridView.DefaultCellStyle.Font;                // Setting the RowFore style                RowForeColor = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.ForeColor;                if (RowForeColor.IsEmpty) // If the there is no special RowFore style of the CurrentRow, then use the default one associated with the DataGridView control                    RowForeColor = TheDataGridView.DefaultCellStyle.ForeColor;                RowForeBrush = new SolidBrush(RowForeColor);                // Setting the RowBack (for even rows) and the RowAlternatingBack (for odd rows) styles                RowBackColor = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.BackColor;                if (RowBackColor.IsEmpty) // If the there is no special RowBack style of the CurrentRow, then use the default one associated with the DataGridView control                {                    RowBackBrush = new SolidBrush(TheDataGridView.DefaultCellStyle.BackColor);                    RowAlternatingBackBrush = new SolidBrush(TheDataGridView.AlternatingRowsDefaultCellStyle.BackColor);                }                else // If the there is a special RowBack style of the CurrentRow, then use it for both the RowBack and the RowAlternatingBack styles                {                    RowBackBrush = new SolidBrush(RowBackColor);                    RowAlternatingBackBrush = new SolidBrush(RowBackColor);                }                // Calculating the starting x coordinate that the printing process will start from                CurrentX = (float)LeftMargin;                if (IsCenterOnPage)                                        CurrentX += (((float)PageWidth - (float)RightMargin - (float)LeftMargin) - mColumnPointsWidth[mColumnPoint]) / 2.0F;                // Calculating the entire CurrentRow bounds                                RowBounds = new RectangleF(CurrentX, CurrentY, mColumnPointsWidth[mColumnPoint], RowsHeight[CurrentRow]);                // Filling the back of the CurrentRow                if (CurrentRow % 2 == 0)                    g.FillRectangle(RowBackBrush, RowBounds);                else                    g.FillRectangle(RowAlternatingBackBrush, RowBounds);                // Printing each visible cell of the CurrentRow                                for (int CurrentCell = (int)mColumnPoints[mColumnPoint].GetValue(0); CurrentCell < (int)mColumnPoints[mColumnPoint].GetValue(1); CurrentCell++)                {                    if (!TheDataGridView.Columns[CurrentCell].Visible) continue; // If the cell is belong to invisible column, then ignore this iteration                    // Check the CurrentCell alignment and apply it to the CellFormat                    if (TheDataGridView.Columns[CurrentCell].DefaultCellStyle.Alignment.ToString().Contains("Right"))                        CellFormat.Alignment = StringAlignment.Far;                    else if (TheDataGridView.Columns[CurrentCell].DefaultCellStyle.Alignment.ToString().Contains("Center"))                        CellFormat.Alignment = StringAlignment.Center;                    else                        CellFormat.Alignment = StringAlignment.Near;                                        ColumnWidth = ColumnsWidth[CurrentCell];                    RectangleF CellBounds = new RectangleF(CurrentX, CurrentY, ColumnWidth, RowsHeight[CurrentRow]);                    // Printing the cell text                    g.DrawString(TheDataGridView.Rows[CurrentRow].Cells[CurrentCell].EditedFormattedValue.ToString(), RowFont, RowForeBrush, CellBounds, CellFormat);                                        // Drawing the cell bounds                    if (TheDataGridView.CellBorderStyle != DataGridViewCellBorderStyle.None) // Draw the cell border only if the CellBorderStyle is not None                        g.DrawRectangle(TheLinePen, CurrentX, CurrentY, ColumnWidth, RowsHeight[CurrentRow]);                    CurrentX += ColumnWidth;                }                CurrentY += RowsHeight[CurrentRow];                // Checking if the CurrentY is exceeds the page boundries                // If so then exit the function and returning true meaning another PagePrint action is required                if ((int)CurrentY > (PageHeight - TopMargin - BottomMargin))                {                    CurrentRow++;                    return true;                }            }            CurrentRow++;        }        CurrentRow = 0;        mColumnPoint++; // Continue to print the next group of columns        if (mColumnPoint == mColumnPoints.Count) // Which means all columns are printed        {            mColumnPoint = 0;            return false;        }        else            return true;    }    // The method that calls all other functions    public bool DrawDataGridView(Graphics g)    {        try        {            Calculate(g);            DrawHeader(g);            bool bContinue = DrawRows(g);            return bContinue;        }        catch (Exception ex)        {            MessageBox.Show("Operation failed: " + ex.Message.ToString(), Application.ProductName + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);            return false;        }    }} 

读书人网 >asp.net

热点推荐