أسهل طريقة لإضافة رأس وتذييل إلى ملف Printing.PrintDocument (.Net 2.0)؟

StackOverflow https://stackoverflow.com/questions/59213

سؤال

ما هي أسهل طريقة لإضافة رأس وتذييل إلى كائن .Net PrintDocument، سواء بشكل عملي أو في وقت التصميم؟

على وجه التحديد، أحاول طباعة عنصر تحكم شبكة تابع لجهة خارجية (Infragistics GridEx v4.3)، والذي يأخذ كائن PrintDocument ويرسم نفسه فيه.

تحتوي الصفحة الناتجة فقط على الشبكة ومحتوياتها - ولكنني أرغب في إضافة رأس أو عنوان لتحديد التقرير المطبوع، وربما تذييلًا لإظهار من قام بطباعته، ومتى، ومن الناحية المثالية رقم الصفحة وإجمالي الصفحات.

أنا أستخدم VB.Net 2.0.

شكرا لمساعدتك!

هل كانت مفيدة؟

المحلول

يقوم كائن printdocument بتشغيل حدث printpage لكل صفحة تريد طباعتها.يمكنك رسم نص/أسطر/إلخ في قائمة انتظار الطباعة باستخدام معلمة الحدث printpageeventargs:

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

قم بتعتيمه باستخدام الأحداث عند تمريره إلى الشبكة، حتى تتمكن من التعامل مع الحدث.

نصائح أخرى

التالي بوجي بويإجابة، إليك ما توصلت إليه (والذي قمت بتبسيطه على سبيل المثال لأغراض):

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

        Dim oDoc As New Printing.PrintDocument
        oDoc.DefaultPageSettings.Landscape = True
        AddHandler oDoc.PrintPage, AddressOf PrintPage

        oDoc.DocumentName = "Printout"

        InfragisticsWinGrid.PrintPreview(InfragisticsWinGrid.DisplayLayout, oDoc)

    End If
End Sub


Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    ' Draw title
    e.Graphics.DrawString("Report Title"), New Font("Arial", 16), Brushes.Black, 95, 70)

    ' Draw footer
    e.Graphics.DrawImage(DirectCast(mResources.GetObject("footer_logo"), Drawing.Bitmap), 95, e.PageBounds.Height - 87)
    Dim drawFont As New Font("Arial", 8.75)

    e.Graphics.DrawString("Report Title", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 90)
    e.Graphics.DrawString("Printed", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 76)
    e.Graphics.DrawString("Printed By", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 62)

    ' Draw some grid lines to add structure to the footer information
    e.Graphics.DrawLine(Pens.Gray, 246, e.PageBounds.Height - 90, 246, e.PageBounds.Height - 48)
    e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 75, 550, e.PageBounds.Height - 75)
    e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 61, 550, e.PageBounds.Height - 61)

    e.Graphics.DrawString("Report", drawFont, Brushes.Black, 250, e.PageBounds.Height - 90)
    e.Graphics.DrawString(Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString, drawFont, Brushes.Black, 250, e.PageBounds.Height - 76)
    e.Graphics.DrawString("Andrew", drawFont, Brushes.Black, 250, e.PageBounds.Height - 62)

End Sub

كان علي أن ألعب بقيم e.PageBounds.Height - x للحصول على العناصر المرسومة لتصطف.

شكرًا لك مرة أخرى بوجي بوي بالنسبة للمؤشر - كان الوصول إلى ReportPage.Graphics() هو بالضبط ما كنت أبحث عنه:o)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top