Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vb.net - How to duplex and staple a print job

I want to print from Excel to Postscript then print to a printer. How can I specify duplex and staple before sending the print job to the printer.

I've been looking at the PrintTicket class. It looks promising. But I'm using WinForms project with .net 3.5.

I really don't want to set up multiple print queues if I don't have to. So how do I control the duplex and staple options?

Public Shared Function PrintSheetsToPS(ByVal wb As Excel.Workbook, _
                                       ByVal arr As Array, _
                                       ByVal PSFileName As String) As String

    Dim svInputPS As String = TempPath & PSFileName
    IO.File.Delete (svInputPS)
    wb.Worksheets(arr).PrintOut(PrintToFile:=True, _
                                  PrToFileName:=svInputPS, _
                                  ActivePrinter:=PSPrinterName)

    Return svInputPS

End Function
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use PrintQueue.AddJob Method to add a file to the print queue. But it only accepts XPS documents. Then you can use PrintTicket class to specify duplex and/or stapling. Instead of using Postscript, print to XPS and add a job with that.

Public Shared Function PrintSheetsToXPS(ByVal wb As Excel.Workbook, _
                                       ByVal arr As Array, _
                                       ByVal XPSFileName As String) As String

    Dim svInputPS As String = TempPath & XPSFileName
    IO.File.Delete (svInputPS)
    wb.Worksheets(arr).PrintOut(PrintToFile:=True, _
                      PrToFileName:=svInputPS, _
                      ActivePrinter:="Microsoft XPS Document Writer")

    Return svInputPS

End Function

Sub demoDuplexStaple(ByVal XPSFileName As String)
    'Imports SysPrint = System.Printing
    'need to reference ReachFramework
    Dim PrintServer As New SysPrint.PrintServer("" & My.Computer.Name)
    Dim PrintQ As New SysPrint.PrintQueue(PrintServer, "Ricoh Main")
    Dim Jobs As SysPrint.PrintJobInfoCollection = PrintQ.GetPrintJobInfoCollection
    Dim able As SysPrint.PrintCapabilities = PrintQ.GetPrintCapabilities()

    'get the current PrintTicket
    Dim CurrentTicket As SysPrint.PrintTicket _
            = PrintQ.CurrentJobSettings.CurrentPrintTicket

    'modify to staple and duplex
    CurrentTicket.Stapling = Printing.Stapling.StapleTopLeft
    CurrentTicket.Duplexing = Printing.Duplexing.TwoSidedLongEdge

    'add the XPS file to the print queue to print
    Dim TestJob As SysPrint.PrintSystemJobInfo _
            = PrintQ.AddJob("Test job", XPSFileName, False)

End Sub

Then if you need a PDF you can convert XPS to PDF using GhostXPS. Unfortunately there's no way to add bookmarks at the same time currently. But that may be addressed in the future.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...