【问题标题】:Print multiple multipaged files with one Printdocument使用一个 Printdocument 打印多个多页文件
【发布时间】:2014-07-18 06:53:52
【问题描述】:

我想打印multipage .tiff files 的列表。我遇到的问题是我没有得到多页,只有第一页被“打印”。有人可以指出我到底做错了什么吗?

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

变量的一些解释:

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

我的终极目标:在打印预览中显示一系列(列表)多帧 .tiff。

【问题讨论】:

  • 为什么设置e.HasMorePages = false
  • 否则它会继续使用 docPrintPage 方法添加页面。 e.HasMorePages 最后必须设置为 false。众多参考之一:参考:codeproject.com/Tips/733680/…
  • 你到底想要什么?在每个打印页面上打印一个 tiff 页?因为 PrintPage 事件打印单个页面,但您尝试打印多个 tiff 页面然后退出指定 HasMorePages = False,这是不正确的。
  • 确实,我的终极目标是每页打印一个 tiff。 tiff 中的每个帧都是 A4 大小的。问题在于存在多个文件和多个框架,并在打印预览中显示。显示一个多帧 .tiff 不是问题,它显示一个列表
  • 删除该代码并尝试

标签: vb.net printdocument


【解决方案1】:

感谢以下帖子,我设法解决了这个问题:

Thank you very very much Tezzo

首先创建以下类变量

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

创建一个新的 PrintDocument(在某些函数中)并将其分配给 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()

在您的方法docBeginPrint 中,您将类变量(之前声明的)设置为第一个(如果存在)的统计信息

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

'然后在您的 docPrintPage 中开始打印

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

我希望这对其他人有用。

【讨论】:

    【解决方案2】:

    您可以使用此伪代码(抱歉,我无法重现您的情况):

    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
    

    【讨论】:

    • 几乎就是我要找的东西! :) 你的代码给了我一个很好的尝试!
    • 它做到了!将立即发布我调整后的代码:)
    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多