2011-04-27 223 views
1

我創建標籤一個WinForm。C#WinForm的PrintPreviewDialog上:打印預覽顯示多個標籤每頁

它調用在使用PrintPageEventArgs一個PrintPreviewDialog並顯示信息的PrintDocumentPrintPageEventHandler

void Document_Printed(object sender, PrintPageEventArgs e) { 
    // Code goes here 
} 

如果標籤是一個小的地址標籤去,而不是在PrintPreviewDialog看到一個標籤到8.5x11信,我想看到適合對PageSettings.PaperSize標籤的數量。

示例:假設四(4)項適合選定的媒體(艾利打印機標籤或任何東西)上。

  • 如果我的最終用戶指定了1到4份要打印的副本,我希望我的「打印預覽」對話框顯示所有副本。

  • 如果我的最終用戶指定超過四(4)項,打印預覽對話框應該顯示多個頁面(我以前從來沒有解決多頁)。

我大小我的數據,以滿足我的標籤尺寸,但我不知道如何讓我的PrintPageEventHandlerPrintPreviewDialog顯示超過1個標籤。

有人能告訴我如何做到這一點?如何顯示和打印每張紙上的多個標籤(文檔?)?

編輯:這裏是我的代碼,這是基於2個的RectangleF對象:pageRect和LabelRect:

void Document_Printed(object sender, PrintPageEventArgs e) { 
    if (printPreview == null) return; 
    int labelSupport = 1; 
    RectangleF pageRect = new RectangleF(0, 0, printPreview.Document.DefaultPageSettings.PaperSize.Width, printPreview.Document.DefaultPageSettings.PaperSize.Height); 
    float fW = (pageRect.Width < LabelRect.Width) ? (pageRect.Width/LabelRect.Width) : (LabelRect.Width/pageRect.Width); 
    float fH = (pageRect.Height < LabelRect.Height) ? (pageRect.Height/LabelRect.Height) : (LabelRect.Height/pageRect.Height); 
    // START Portion I need HELP with! 
    if (1 < LabelsPerPage) { 
    if (Landscape) { 
    } else { 
    } 
    } else { 
    if (Landscape) { 
    } else { 
    } 
    } 
    // END (I think) Portion I need HELP with! 
    SizeF ratio = new SizeF(fW, fH); 
    Graphics G = e.Graphics; 
    foreach (Label item in labelList) { 
    Console.WriteLine(item.Name); 
    using (SolidBrush b = new SolidBrush(Color.Black)) { 
     using (Pen p = new Pen(b)) { 
     float x = ratio.Width * (float)item.Location.X; 
     float y = ratio.Height * (float)item.Location.Y; 
     float w = ratio.Width * (float)item.Size.Width; 
     float h = ratio.Height * (float)item.Size.Height; 
     RectangleF r = new RectangleF(x, y, w, h); 
     G.DrawString(item.Text, item.Font, b, r); 
     } 
    } 
    } 
} 

編輯2:我需要一種方法來打印1頁上的2頁或更多的文檔。到目前爲止還沒有任何東西能夠解決這個關鍵問題這是我需要的答案。

+1

瞭解PrintPageEventHandler – 2011-04-27 20:41:16

+0

的一些渲染代碼可能會很有用。已經添加了一個代碼示例,從** EDIT:**部分開始。 – jp2code 2011-04-27 20:48:52

+0

這看起來沒問題。我會在該方法的頂部放置一個斷點,然後逐行放置,以確保您期望發生的事情實際上正在發生。例如,您的繪圖循環中的每個項目的大小和位置值是否更改?看起來他們應該但是如果你沒有在別處正確地分配這些值,那麼你可能再次繪製相同的東西,或者在前面的標籤上繪製每個新的標籤。 – 2011-04-27 21:01:44

回答

1

打印第一頁後實際上很容易:只需將PrintPageEventArgs HasMorePages property設置爲true即可。

棘手的部分是要知道何時通過設置HasMorePages爲false,以阻止這一切。我通過將每個打印作業的數據存儲在列表中並使用索引值跟蹤我在此列表中的位置來完成此操作。

每次PrintPage事件觸發時,我都會增加我在List中使用的索引,以便爲PrintPage事件提供我想要打印的頁面的詳細信息,如果我在最後一個元素上,將HasMorePages設置爲false。

void Document_Printed(object sender, PrintPageEventArgs e) { 
    // Get data for this page. 
    //xxx = pageDataList[indexValue]; 

    // Code to print stuff. 

    indexValue++; 
    e.HasMorePages = (pageDataList.Length == indexValue); 
} 

這種方法可以爲您提供labelList我在你的代碼中看到工作太,也許。既然你將要分批打印4份,你顯然必須調整一下邏輯,但我認爲你明白了。

+0

你知道,我看到了'HasMorePages'屬性,但我只是*查看*它,而不是*設置*它。 – jp2code 2011-04-27 21:08:31

+0

大鼠。按[CR]提交註釋,而不是突破下一行。 {嘆氣!}無論如何,我會試試看是否可以在8.5x11的一張紙上打印/顯示多個小標籤。 – jp2code 2011-04-27 21:09:48

+0

我不得不縮小我想要在屏幕上顯示的頁面數量,但否則這確實有效! – jp2code 2011-06-20 21:40:39