|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article provides information on how to print an Office Web Component (v11) chart from the .NET Windows.Forms environment. There is another excellent article available relating to this subject which is definitely worth the read at Create Column Charts Using OWC11. BackgroundI have always wanted to contribute to this forum and after spending the better part of the day working this out I saw it as a subject that was worthwhile. Printing OWC objects from Windows.Forms is not strikingly obvious (at least not to me!). There are examples using OWC from a web server and after some digging around you'll see examples using So, what of a solution for Windows.Forms! I've only been fiddling with OWC for a couple of days so the method I'm describing here might not be the best, however, it does seem to work quite well. Using the CodeThe result I ended on can be outlined as follows:
Of course you will need to have Office Web Components 11 installed. This is available on the Microsoft Website or from the Microsoft Office 2003 Installation Kit. Note, there are licensing issues relating to this install. Office 2003 Web Components in .NET does not provide event handling by default however this can be overcome by following the instructions in this link: HOW TO: Handle Events for the Office 2003 Web Components in Visual Studio.NET. Sample CodeOn a Windows.Form draw out a The sample code begins with creating the actual chart that will be printed. There are a number of articles around describing how to do this so I won't dwell on this subject. So, under the Print button we declare a couple of variables: ' variable declaration
Dim ba() As Byte
Dim ms As IO.MemoryStream
We then extract the binary stream (byte array) from our ' note, see OWC11 documentation for further information on PrintQuality3D property
AxChartSpace1.PrintQuality3D = 1.0#
' you can make the chart whatever width/height you want but for
' simplicity I'll leave it as what the form size is
ba = CType(AxChartSpace1.GetPicture("gif", _
AxChartSpace1.Width, _
AxChartSpace1.Height), _
Byte())
ms = New IO.MemoryStream(ba)
The next step is to load the stream into our m_imgChart = Image.FromStream(ms, False, True)
To finish up on the print button code we use the printdocChart.DocumentName = ""My OWC Chart""
printdocChart.DefaultPageSettings.Landscape = True
'
printdocChart.Print()
m_imgChart.Dispose()
We also need to set the Private Sub printdocChart_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles printdocChart.PrintPage
e.Graphics.DrawImage(m_imgChart, 30, 30, m_imgChart.Width, m_imgChart.Height)
e.HasMorePages = False
End Sub
|
||||||||||||||||||||||