2014-12-02 82 views
1

我構建上有兩個相似圖片的ASP網站頁面上打印與點擊兩個圖像,但每幅圖像:如何自己的頁面

<asp:Image ID="ImgPg1" runat="server" ImageUrl="Page1.ashx" /> 
<asp:Image ID="ImgPg2" runat="server" ImageUrl="Page2.ashx" /> 

我想用一個打印兩個圖像單擊。

Page1.ashx的圖像需要第1頁上打印,需要在第2頁

要打印你怎麼每頁打印一個圖像上Page2.ashx的形象呢?

這就是我所做的,但我發現PrintDocument在服務器端運行,所以出現一個錯誤說「找不到打印機」。下面的代碼示例打印一個矩形作爲測試,但它是.ashx頁面對它們的反正。

protected void Print_Click(object sender, EventArgs e) 
    { 
     // Page.ClientScript.RegisterClientScriptBlock(GetType(), "Print CI", "window.print()", true); 

     PrintDocument pd = new PrintDocument(); 
     pd.DefaultPageSettings.Landscape = false; 
     nPage = 0; 
     pd.PrintPage += new PrintPageEventHandler(printImages); 

     pd.Print(); 

    } 

    private void printImages(object sender, PrintPageEventArgs ev) 
    { 
     Rectangle rect = new Rectangle(0, 0, 353, 230);  // 0.236 Inches @ 96 dpi = 23 

     switch (nPage) 
     { 
      case 0:  // Page 1 just for test 
       ev.Graphics.FillRectangle(new SolidBrush(Color.LightGray), rect); 
       ev.Graphics.DrawRectangle(new Pen(Color.Black, 1), rect); 
       break; 
      case 1:  // Page 2 just for test 
       rect = new Rectangle(360, 0, 353, 230); 
       ev.Graphics.FillRectangle(new SolidBrush(Color.LightPink), rect); 
       ev.Graphics.DrawRectangle(new Pen(Color.Black, 1), rect); 
       break; 
     } 

     ++nPage; 
     if (nPage < 2) 
      ev.HasMorePages = true; 
     else 
      ev.HasMorePages = false; 

    } 

我之所以需要打印一個圖像每頁是因爲一個圖像是一種形式的前部,另一形象是它的背面。圖像尺寸小於8.5 x 11的紙張(大約1/4的紙張)。

感謝, 巴勃羅

回答

2

你真的要惹試圖編程方式發送的東西到打印機身邊?除非有良好的商業原因,否則我認爲'最好的例子'是將你的.ashx頁面創建爲一個打印友好的頁面,只有兩個圖像,然後讓用戶從瀏覽器打印頁面。

但是......要回答您的實際問題,爲了確保在不同頁面上打印兩個圖像,您可以在兩者之間使用CSS分頁符。

.PageBreak { 
    page-break-after: always; 
} 

<asp:Image ID="ImgPg1" runat="server" ImageUrl="Page1.ashx" CssClass="PageBreak" /> 
<asp:Image ID="ImgPg2" runat="server" ImageUrl="Page2.ashx" /> 

這將強制打印機在第一張圖像之後和第二張之前輸入新的紙張。假設圖像大小適合8.5 x 11的紙張,這應該工作得很好。在分頁-後

更多信息: http://www.w3schools.com/cssref/pr_print_pageba.asp

而且,很多偉大的.ashx的提示的位置: http://www.dotnetperls.com/ashx

讓我們知道如何去!

+0

凱西感謝您的回覆。我需要兩頁打印的原因是因爲一張圖片是一張表格的正面,另一張是背面。 ashx頁面只有圖像,沒有別的。現在,我已取消註釋'Page.ClientScript.RegisterClientScriptBlock(...)'在'Print_Click(...)'上的註釋並評論其他行。我已經嘗試了您的建議,但仍然只有一頁打印有兩張圖像。我可能沒有正確使用你的建議。 – Pabinator 2014-12-03 16:16:16

+0

嗯。我承認我沒有試過這個。我曾經使用過CSS分頁符,並取得了成功。也許嘗試將每個圖像包裝在div標籤中,然後將樣式放在div上? – 2014-12-03 16:23:47

+0

幾乎相同。由於div標籤,兩幅圖像都在一個頁面上,但現在一個在另一個上面。 – Pabinator 2014-12-03 16:31:54

1

凱西的答案幫了很大忙。這是最後的結果。

頁眉代碼:

<script type="text/javascript"> 
    function printCI() { 
     var printWindow = window.open('', 'CI', 'height=600,width=800'); 

     printWindow.document.write('<html><head><title>CI Form</title>'); 
     printWindow.document.write('<style>.PageBreak{page-break-after: always;}</style>'); 
     printWindow.document.write('</head><body >'); 
     printWindow.document.write('<div class=\'PageBreak\'><img src=\'Page1.ashx\' /></div>'); 
     printWindow.document.write('<div><img src=\'Page2.ashx\' /></div>'); 
     printWindow.document.write('</body></html>'); 
     printWindow.document.close(); 
     printWindow.print(); 
    } 
</script>  

CS代碼:

protected void Print_Click(object sender, EventArgs e) 
{ 
    // Print CI with a javascript so we can print in two pages 
    Page.ClientScript.RegisterClientScriptBlock(GetType(), "Print CI", "printCI()", true); 
} 
相關問題