Click here to Skip to main content
15,790,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning
Please could you tell me how to print a PDF file directly without opening the print dialog box and setting the A4 paper size? As for the first part of my question, actually, I found many examples, I did a class that prints files, but often prints them not in A4 format. So what I would like is to be able to set the A4 format before printing and also test if there are more than one page in the PDF file and possibly print the second page. I tried to see if it can be done with ITextSharp, but I didn't understand if it can do with an existing file.
Thanks a lot for any help you want to give me.

What I have tried:

Imports Microsoft.VisualBasic
Imports System.Drawing.Printing
Imports System.IO

Public Class StampaPDF


    Public Sub New(ByVal args As String)

        Dim PathDisegni As String = args

        Dim mioPdf As New Pdf
        Dim files() As String = Directory.GetFiles(PathDisegni)

        'Variabile predicate con lambda func con parametri che passa una stringa e ritorna un boolean
        Dim predicate As Func(Of String, Boolean) = Function(file) file.Contains(".pdf")

        For Each file As String In files.Where(predicate)

    End Sub

End Class

Public Class Pdf

    'Dichiara i PrinterSetting
    Public Shared defaultPrinterSetting As PrinterSettings = Nothing

    Public Function PrintPDFs(ByVal pdfFileName As String) As Boolean

            Dim psi As New System.Diagnostics.ProcessStartInfo()
            psi.UseShellExecute = True
            psi.Verb = "print"
            psi.WindowStyle = ProcessWindowStyle.Hidden
            psi.FileName = pdfFileName
            Dim myProcess As System.Diagnostics.Process
            myProcess = System.Diagnostics.Process.Start(psi)
            Dim localProces As Diagnostics.Process = Diagnostics.Process.GetProcessById(myProcess.Id)
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Shared Async Function PrintPDFs2(ByVal pdfFileName As String) As Task(Of Boolean)

            Dim DocPrinter As New DocumentPrinter
            'Ottiene la stampante di default del sistema
            defaultPrinterSetting = Await DocPrinter.GetDefaultPrinterSetting

            'Togliere il commento se si vuole cambiare la stampante di default prima di stampare

            'Stampa il file 
            If (Await DocPrinter.PrintFile(pdfFileName, defaultPrinterSetting)) Then
                MsgBox("Il Disegno è stato stampato !!")
                MsgBox("Il Disegno NON è stato stampato !!")
            End If

        Catch ex As Exception
        End Try

    End Function

End Class

Public NotInheritable Class DocumentPrinter

    Shared Sub New()

    End Sub

    Public Shared Async Function PrintFile(ByVal fileName As String, printerSetting As PrinterSettings) As Task(Of Boolean)

        Dim printProcess As Process = Nothing
        Dim printed As Boolean = False

            If printerSetting IsNot Nothing Then
                Dim startInfo As New ProcessStartInfo()
                startInfo.Verb = "Print"
                startInfo.Arguments = Pdf.defaultPrinterSetting.PrinterName ' <----Stampante da usare---- 
                startInfo.FileName = fileName
                startInfo.UseShellExecute = True
                startInfo.CreateNoWindow = True
                startInfo.WindowStyle = ProcessWindowStyle.Hidden

                Using print As System.Diagnostics.Process = Process.Start(startInfo)
                    'Chiude l'applicazione dopo x millisecondi con  WaitForExit(X)
                    '  print.WaitForExit(10000)
                    If print.HasExited = False Then
                        If print.CloseMainWindow() Then
                            printed = True
                            printed = True
                        End If
                        printed = True
                    End If
                End Using
                Throw New Exception("La Stampante NON è stata trovata nel sistema...")
            End If

        Catch ex As Exception
        End Try
        Return printed
    End Function

    ''' <summary>
    ''' Ottieni le impostazioni predefinite della stampante nel sistema
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Async Function GetDefaultPrinterSetting() As Task(Of PrinterSettings)

        Dim defaultPrinterSetting As PrinterSettings = Nothing

        For Each printer As String In PrinterSettings.InstalledPrinters

            defaultPrinterSetting = New System.Drawing.Printing.PrinterSettings
            defaultPrinterSetting.PrinterName = printer

            If defaultPrinterSetting.IsDefaultPrinter Then
                Return defaultPrinterSetting
            End If


        Return defaultPrinterSetting

    End Function

End Class
Updated 23-Mar-20 4:13am
EricERankin 25-Mar-20 13:27pm    
You can refer to this Print PDF files in C# and VB.NET.
Note, you can find examples for Console, Windows Forms and WPF applications, they all use the same C# and VB.NET library for PDF.

1 solution

defaultPrinterSetting.PrinterName = printer

defaultPrinterSetting.DefaultPageSettings.PaperSize.RawKind = System.Drawing.Printing.PaperKind.A4
Share this answer
Fabrizio Leoncini 26-Mar-20 9:48am    
Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900