如何在.NET中翻转/旋转PrintDocument?

3

我有一个文件需要在打印时翻转/旋转180度。(这是由于打印机标签纸的方向造成的)

有一个属性PrintDocument.PrinterSettings.LandscapeAngle,但它是只读的。

我认为这个属性受打印机驱动程序的影响,因此不能“设置”。

是否有一种简便的方法可以将打印旋转180度,而不必做任何过于复杂的操作?

4个回答

2

1
但是我想要它与横向相反的180度。 - Simon_Weaver
你不能只是把标签纸翻过来吗? - Joel Coehoorn
它是放在纸盘中还是被缓冲输出了? - Joel Coehoorn

2
我猜这取决于你对“过于恶劣的事情”所定义的内容 :-) PrintDocument 类有一个 Graphics 对象,您可以使用它来实现此功能,该对象又有一个 TranslateTransformRotateTransform 方法,这将使您能够将物品放置在需要的位置。
通常值得在操作图形对象之前复制一份,以便在完成后可以将其恢复到原始状态。

未经测试但听起来很有前途! - Simon_Weaver
1
我在上一家雇主的某些PDF处理代码中成功使用过它。可惜,自从我离开后,我就无法访问实际代码了,但它很容易实现。 - Rob Cowell

2

在VB.NET中打印表单并旋转/翻转PrintDocument,并将DefaultPageSettings设置为横向

Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
    mPrintBitMap.RotateFlip(RotateFlipType.Rotate90FlipNone)
    mPrintDocument.PrinterSettings.DefaultPageSettings.Landscape = True
    ' Draw the image centered.     
    Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
    Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2

    e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
    ' There's only one page.   
    e.HasMorePages = False
End Sub
Private Sub B_print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_print.Click
    ' Copy the form image into a bitmap.    
    mPrintBitMap = New Bitmap(Me.Width, Me.Height)
    Dim lRect As System.Drawing.Rectangle
    lRect.Width = Me.Width
    lRect.Height = Me.Height
    Me.DrawToBitmap(mPrintBitMap, lRect)
    ' Make a PrintDocument and print.    
    mPrintDocument = New PrintDocument

    mPrintDocument.Print()

End Sub

1

你有没有尝试过在将图像分配给打印机 GDI 之前先旋转图像本身?这就是我所做的:

                _currentPage = Image.FromStream((MemoryStream)_queue.Dequeue());
                pageHeight = _currentPage.Height;
                pageWidth = _currentPage.Width;

                if (pageHeight < pageWidth)
                {
                    _currentPage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    pageHeight = _currentPage.Height;
                    pageWidth = _currentPage.Width;                      

                }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接