Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Printing EMF

Back to Forum: Tech Off
  • User profile image
    kettch

    I am pulling reports from Reporting Services in EMF format. My question is, how do I print them? I've not found any documentation on this, and what I have found is not working.

    I currently have this event to handle printing:

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

    Dim pageImage As New System.Drawing.Imaging.Metafile(reportStreams(currentPageIndex))

    e.Graphics.DrawImage(pageImage, e.PageBounds)

    currentPageIndex += 1

    e.HasMorePages = (currentPageIndex < reportStreams.Count)

    End Sub


    However, the event always ends up getting run exactly one more time than there are pages, causing the reportstreams collection to hit an index out of range exception. The reportstreams Generic.List is typed with MemoryStreams and each item in the list contains one page of the report. I've confirmed that all of the pages are there by just saving the files to a folder, and everything is there. This behavior occurs whether I call printdocument.print, or view it in the print preview control.

    I'm probably screwing this up royally, so any pointers or relavent documentation would be greatly appreciated.

    Thanks

  • User profile image
    defstream

    The reportStreams array is 0 based, and the Count returns the length starting at 1 for the first item. So you must subtract one from the count to determine the last page. Like so...

    e.HasMorePages = (currentPageIndex < (reportStreams.Count - 1 ))

  • User profile image
    kettch

    defstream wrote:
     The reportStreams array is 0 based, and the Count returns the length starting at 1 for the first item. So you must subtract one from the count to determine the last page. Like so...

    e.HasMorePages = (currentPageIndex < (reportStreams.Count - 1 ))



    But if I do that, then for a three page report the event is only going to run twice. For some reason it respects what you posted:

    e.HasMorePages = (currentPageIndex < (reportStreams.Count - 1 ))

    and only runs twice for a three page report, but this runs four times:

    e.HasMorePages = (currentPageIndex < reportStreams.Count)

    The logic checks out, I can step through and watch e.HasMorePages get set to false, but the event still runs again.

  • User profile image
    defstream

    Then you might want to check the bounds of the reportStreams in the beginning of the method to ensure that you don't get the out of bounds exceptions. 

    if(currentPageIndex==reportStreams.Count) return;

  • User profile image
    kettch

    Yeah, I tried putting in a constraint like that before, and what happens is that I don't get page one. I put something like this into the PrintPage event:

    My.Computer.FileSystem.WriteAllBytes("c:\temp\test" & currentPageIndex.ToString & ".emf", reportStreams(currentPageIndex).ToArray, False)

    and it wrote a file for all three pages, so there is valid data at every location in the reportstreams collection, but somehow it doesn't make it to onto a page.

  • User profile image
    kettch

    I figured I'd bump this coming off of the weekend.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.