ASP.NETを使用してWebコントロールを印刷しているときにヘッダーとフッターを非表示にします
-
05-07-2019 - |
質問
ASP.NETでwebcontolを印刷するときに、「Page 1 of 1」やフッター(url)などのヘッダーを非表示にするにはどうすればよいですか?
現在、[印刷]ボタンで新しいページを開いてクリックします
protected void Page_Load(object sender, EventArgs e)
{
if( null != Session["Control"] )
{
Control ctrl = ( Control )Session["Control"];
PrintManager.PrintWebControl( ctrl );
Session["Control"] = null;
}
}
これにより、ヘッダーとフッターが印刷されます。それを避ける方法
解決
この設定は、ユーザーがブラウザで構成します。彼らはコードからそれを無効にする方法はありません。最善の策は、設定を構成/無効にする方法に関する指示を含めることです。
こちらの例をご覧ください。 http://www.xheo.com/products/sps/default。 aspx?print = true
他のヒント
CSSスタイルを使用し、印刷のメディアタイプに適用するように指定する必要があります。ヘルプについては、この記事を参照してください。 http://www.cantoni.org/articles/printstyle
基本的に、印刷スタイル専用の個別のスタイルシートを作成します。ページ上で何かを非表示にする場合は、その要素スタイル属性の1つとして {display:none}
を使用します。次に、HEAD要素でスタイルシートをリンクします。
<link href="print.css" media="print" type="text/css" rel="stylesheet" />
このために、私たちはそのような印刷クラスを使用しています
public static void PrintWebControl(Control ctrl)
{
PrintWebControl(ctrl, string.Empty);
}
public static void PrintWebControl(Control ctrl, string Script)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ctrl is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
if (Script != string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
}
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
// done changes on below 1 line for unassigned header URL //
frm.ResolveUrl("");
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ctrl);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
ここでは、フォームプロパティの行を変更して設定するだけです
frm.ResolveUrl("");
そのため、ページを印刷するときにURlは表示されません。
Firefoxを使用していて、クライアントに thisをインストールさせることができる場合アドオン。
それ以外の場合、Internet Explorerを使用している場合:google for&quot; MeadCo ScriptX &quot;
(FFオプションは無料、IEオプションは基本機能のみ無料)