自定义paperize printDocument webforms

时间:2014-03-30 21:58:19

标签: c# webforms printdocument

我试图在之前设计和打印的自定义尺寸纸张上打印。它就像发票一样。但它仍然以A4尺寸打印。我的代码在我的设置中有什么问题?

protected void PrintFormLS(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

     pd.PrinterSettings.PrinterName = "HP LaserJet P1005";
     pd.DefaultPageSettings.PaperSize.RawKind = 119;
     pd.DefaultPageSettings.PaperSize = new PaperSize("lsForm", 563, 1251);
     pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("lsForm", 563, 1251);
     pd.OriginAtMargins = true;
     Margins margins = new Margins(1, 1, 1, 1);
     pd.DefaultPageSettings.Margins = margins;
     pd.DefaultPageSettings.Landscape = true;
     pd.Print();

}

1 个答案:

答案 0 :(得分:1)

我通过在打印机属性中创建自定义大小并在代码中指定它的编号来解决此问题。

pd.PrinterSettings.PrinterName = "HP LaserJet P1005";
pd.OriginAtMargins = true;
PaperSize pageSize = new PaperSize();
pageSize.RawKind = 512; //this is number of created custom size 563x1251
Margins margins = new Margins(1, 1, 1, 1);
pd.DefaultPageSettings.Margins = margins;
pd.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
希望它会对某人有所帮助。