Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Printing only the first page from a PDF in VB.NET

$
0
0
Hey guys, I'm in need of some help.
So I have to select a folder, and then all pdf files that are in that folder have to be printed. Not that hard but here's the catch: I only have to print the first page of every pdf.

I hope you can help me and thank you in advance!

This is the code I have right now, but it doesn't seem to find any pdf's:

Imports System.Drawing
    Imports System.Drawing.Printing
    
    Public Class Form1
    
        Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
            ' declaring variables
            Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog
            Dim intAantal As Integer
    
            ' text with instruction
            MyFolderBrowser.Description = "Select the folder"
    
            ' no new folders should be able to be made
            MyFolderBrowser.ShowNewFolderButton = False
    
            ' checking if a folder is selected
            Dim dlgResult As DialogResult
            dlgResult = MyFolderBrowser.ShowDialog()
    
            If dlgResult = Windows.Forms.DialogResult.OK Then
                txtUitvoer.Text = MyFolderBrowser.SelectedPath
    
                ' checking the extension (pdf) 
                Const cstrExtension As String = "pdf"
    
                Dim strFoundFile As String
    
                For Each strFoundFile In System.IO.Directory.GetFiles(MyFolderBrowser.SelectedPath)
    
                    Dim strFile As String = System.IO.Path.GetExtension(strFoundFile)
    
                    If strFile = "." & cstrExtension Or strFile = cstrExtension Then
                        intAantal += 1
    
                        ' searching for a pdf 
                        Dim Proc As New System.Diagnostics.Process
                        Dim ProcPrint As New System.Diagnostics.Process
    
                        Proc.StartInfo.WorkingDirectory = MyFolderBrowser.SelectedPath
                        ProcPrint.StartInfo.WorkingDirectory = MyFolderBrowser.SelectedPath
    
                        ' printing
                        Dim PrintDoc As New Printing.PrintDocument
                        Dim SetPage As New System.Drawing.Printing.PageSettings
    
                        With SetPage
    
                            .PrinterSettings.FromPage = 0
                            .PrinterSettings.ToPage = 1
    
                        End With
    
                        PrintDoc.DefaultPageSettings = SetPage
    
                        PrintDoc.Print()
                          
                    End If
    
                Next
    
    
            End If
    
        End Sub

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>