3

I want to print a list of multipage .tiff files. The problem i'm having is that i'm not getting multiple pages, only the first page gets 'printed'. Can someone point out to me what i'm exactly doing wrong?

Private Sub docPrintPage(sender As Object, e As PrintPageEventArgs)

    For Each filName As String In fileN
        Dim m_Image As Image = Image.FromFile(filName)
        Dim pc As Integer = 0
        Dim currPage As Integer = 0

        pc = m_Image.GetFrameCount(FrameDimension.Page)

        While (currPage < pc)
            m_Image.SelectActiveFrame(FrameDimension.Page, currPage)
            e.Graphics.DrawImage(m_Image, e.PageBounds) 'Most likely the problem lies here

            currPage = currPage + 1
            e.HasMorePages = true

        End While
    Next
    e.HasMorePages = false
 End Sub

Some explanation of the variables:

fileN: List of paths to my files
pc: pagecount/framecount of the current .tiff
currPage: index of the active frame in the current .tiff

My Ultimate goal: to show a series (list) of multi-framed .tiffs in a print-preview.

User999999
  • 2,500
  • 7
  • 37
  • 63
  • why set `e.HasMorePages = false` – Sathish Jul 18 '14 at 07:00
  • Other wise it keeps on adding pages using the method docPrintPage. e.HasMorePages has to be set to false in the end.One of the many references: Ref: http://www.codeproject.com/Tips/733680/Printing-and-Previewing-multiple-pages-in-Csharp – User999999 Jul 18 '14 at 07:02
  • what do you want exactly? print a single tiff page on every printed page? because PrintPage event prints a single page but you are trying to print multiple tiff page and then exit specifying that HasMorePages = False, which is not correct. – tezzo Jul 18 '14 at 07:30
  • Indeed my ultime goal is to print a single tiff per page. Each frame inside a tiff is A4-sized. The issue lies within the existance of multiple files and multiple frames and show it in a printpreview. Showing one multiple frame .tiff isn't the problem, its showing a list – User999999 Jul 18 '14 at 07:34
  • Remove that code and try – Sathish Jul 18 '14 at 07:41
  • Tried it, it goes to printing into infinity. e.HasMorePages = false is needed in order for the printdocument to understand that there aren't any pages after the current one. – User999999 Jul 18 '14 at 07:46

2 Answers2

3

I managed to solve the problem thanks to the following post:

Thank you very very much Tezzo

First create the following class-variables

Private fileCount As Integer = 0 // Index of the current file/Image
Private currPage As Integer = 0 // Current Page in the current file/Image
Private pCount As Integer = 0 // PageCount in the current file/Image
Private currImage As Image // My Current Image

Create a new PrintDocument (in some function) and assign it to PrintPreviewDialog

    Dim vPrintDoc As New PrintDocument
    vPrintDoc.DefaultPageSettings.Landscape = False

    AddHandler vPrintDoc.PrintPage, AddressOf docPrintPage
    AddHandler vPrintDoc.BeginPrint, AddressOf docBeginprint


    Dim i As PrintPreviewDialog = New PrintPreviewDialog
    i.Document = vPrintDoc
    i.ShowDialog()

Inside your method docBeginPrint you set your classvariables (declared before) to stats of the first (if present)

Private Sub docBeginprint(sender As Object, e As PrintEventArgs)

    If fileN.Count > 0 Then
        currPage = 0
        currImage = Image.FromFile(fileN.Item(0))
        pCount = currImage.GetFrameCount(FrameDimension.Page)
    End If

End Sub

'Then in your docPrintPage you start printing

Private Sub docPrintPage(sender As Object, e As PrintPageEventArgs)

    currImage.SelectActiveFrame(FrameDimension.Page, currPage) 'Set the Active Frame of your image to that of the following frame that needs to printed.

    Using st As MemoryStream = New MemoryStream() 'We use a memory stream to print Images in order to avoid a bug inside the e.graphics.

        currImage.Save(st, ImageFormat.Bmp)
        Dim bmp As Bitmap = CType(Image.FromStream(st), Bitmap)
        e.Graphics.DrawImage(bmp, 0, 0)
        bmp.Dispose()

    End Using

    currPage += 1 'Current page is printed, increase index with 1

    If currPage < pCount Then 'Are there any further pages in the current image?
        e.HasMorePages = True 'yes continue printing
    Else 'no 
        If fileCount = (fileN.Count - 1) Then 'Hase the list anymore files?
            e.HasMorePages = False 'No - stop printing all together
        Else 'yes
            currPage = 0 'Set your index for the current page back to first
            fileCount += 1 'Increase the index of the current file
            currImage = Image.FromFile(fileN.Item(fileCount)) 'Load the next image (Perhaps if-statements is desired to avoid Null-Reference)
            pCount = currImage.GetFrameCount(FrameDimension.Page) 'Load the pagecount of the current image

            e.HasMorePages = True 'continue printing
        End If
    End If
End Sub

I hope this may be of some use to other people.

Community
  • 1
  • 1
User999999
  • 2,500
  • 7
  • 37
  • 63
2

You can use this pseudo-code (sorry but I can't recreate your situation):

Dim intFileToPrint as Integer = 0
Dim intPageToPrint as Integer = 0
Dim intPagePrinted as Integer = 0

Private Sub PrintPreview()

    For intFileToPrint = 0 To fileN.Count - 1

        intPageToPrint = Image.FromFile(fileN(intFileToPrint)).GetFrameCount(FrameDimension.Page)
        intPagePrinted = 0

        AddHandler yourPrintDocument.PrintPage, AddressOf PrintFile
        yourPrintDocument.Print()

    End Sub

End Sub

Private PrintFile(ByVal sender As Object, ByVal e As PrintPageEventArgs)

    Dim imgToPrint as Image = Image.FromFile(fileN(intFileToPrint)).SelectActiveFrame(FrameDimension.Page, intPagePrinted)
    e.Graphics.DrawImage(imgToPrint, e.PageBounds)

    intPagePrinted = intPagePrinted + 1

    If intPagePrinted < intPageToPrint then e.HasMorePages = True

End Sub
tezzo
  • 10,858
  • 1
  • 25
  • 48