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

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2874 Views - Last Post: 06 February 2013 - 10:20 AM Rate Topic: ***** 1 Votes

#1 Essenti4l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 13

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

Posted 05 February 2013 - 05:45 AM

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


Is This A Good Question/Topic? 0
  • +

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

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

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

Posted 05 February 2013 - 06:05 AM

If strFile = "." & cstrExtension Or strFile = cstrExtension Then


Be aware that it is sometimes .PDF rather than .pdf.
Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 05 February 2013 - 06:24 AM

There's an HTMLish thing in your code that shouldn't be there.

Is: If strFile = "." & cstrExtension Or strFile = cstrExtension Then

Should be: If strFile = "." & cstrExtension Or strFile = cstrExtension Then

You also need to specify a handler for the Printdocument.PrintPage event. Microsoft's docs say:

"During the printing process, in the Printdocument.PrintPage event, view the PrintRange to determine what should be printed. If PrintRange is PrintRange.SomePages , use the FromPage and ToPage properties to determine what pages should be printed. If PrintRange is PrintRange.Selection , then specify output only for the selected pages."

Another thing that may or may not matter to your particular application is that it will only find PDF files with a lower case extension, ie. it will not find files ending with ".PDF"
Was This Post Helpful? 0
  • +
  • -

#4 Essenti4l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 13

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

Posted 05 February 2013 - 06:33 AM

Alright thank you, I've already made sure it reads all extensions. It still doesn't seem to find any PDF's though.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

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

Posted 05 February 2013 - 06:47 AM

View PostEssenti4l, on 05 February 2013 - 06:33 AM, said:

Alright thank you, I've already made sure it reads all extensions. It still doesn't seem to find any PDF's though.

It worked for me. Try a different printer if possible.
Was This Post Helpful? 0
  • +
  • -

#6 Essenti4l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 13

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

Posted 05 February 2013 - 06:51 AM

View Postandrewsw, on 05 February 2013 - 06:47 AM, said:

View PostEssenti4l, on 05 February 2013 - 06:33 AM, said:

Alright thank you, I've already made sure it reads all extensions. It still doesn't seem to find any PDF's though.

It worked for me. Try a different printer if possible.


It prints 2 empty pages if there are 2 PDF's in the folder. So it knows the amount of PDF's.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

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

Posted 05 February 2013 - 07:15 AM

View PostEssenti4l, on 05 February 2013 - 06:51 AM, said:

View Postandrewsw, on 05 February 2013 - 06:47 AM, said:

View PostEssenti4l, on 05 February 2013 - 06:33 AM, said:

Alright thank you, I've already made sure it reads all extensions. It still doesn't seem to find any PDF's though.

It worked for me. Try a different printer if possible.


It prints 2 empty pages if there are 2 PDF's in the folder. So it knows the amount of PDF's.

Well, when I say it worked.. it sent stuff to my printer but I didn't actually print it :). Try printing the first few pages.

Refer back to Lar3y's post #3 as well.

This post has been edited by andrewsw: 05 February 2013 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 05 February 2013 - 07:32 AM

View PostEssenti4l, on 05 February 2013 - 07:51 AM, said:

It prints 2 empty pages if there are 2 PDF's in the folder. So it knows the amount of PDF's.

Right. It needs a Printdocument.PrintPage event handler. Have you added one yet?

You can find pointers to documentation on that at the MSDN Library
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 05 February 2013 - 10:44 AM

Actually, , Though that event handler is necessary in most cases, the main problem with printing a PDF is that it must first be rendered into some sort of bitmap, and that's no easy task. Adobe likes to keep their API proprietary, and charge for it.

Your best bet might be to find an older version of their reader; one that allows starting it with a command line that allows specifying ptintin and printing options. Another possibility is PDFSharp or iText, both of which are available through SourceForge. I haven't looked into them in depth, but both appear to have libraries available that may be called from VB.Net. GhostScript is another possibility. It will likely render PDFs, though I am not sure.

Good Luck.
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 05 February 2013 - 05:03 PM

Well, I'm totally surprised, having found out that there is actually a control from Adobe that will allow printing of a PDF file, including being able to specify a range of pages without the need for a PrintPage event.

Have a look at how easy this is...
       For Each strFoundFile In System.IO.Directory.GetFiles(MyFolderBrowser.SelectedPath)
           If strFoundFile.Substring(strFoundFile.LastIndexOf(".")) = ".pdf" _
               Or strFoundFile.Substring(strFoundFile.LastIndexOf(".")) = ".PDF" Then
               AxAcroPDF1.src = strFoundFile
                'The above line may be replaced with the following one:
                ' AxAcroPDF1.LoadFile(strFoundFile)
               AxAcroPDF1.Refresh()
               AxAcroPDF1.printPages(1, 1)
           End If
       Next


However (you knew there was going to be a however, right?), this will find every PDF file in the directory, but will only print the last file it finds. I believe it is because the rendering doesn't progress far enough before it loads the next file and tries to print that one.

The answer is probably to monitor the print queue status, perhaps using a timer, or maybe start a thread that does the print task and doesn't set a bit until the job is done (finishing the render is probably sufficient). I am sort of hoping someone else might jump in with a solid methodology.

As for the control, I am using Visual Studio 2010, and to get the control, I simply right-click on any control on the ToolBox, Select "Choose Items...", then click on the "COM Cmponents" tab, and check the checkbox next to the "Adobe PDF Reader". The control will then show up in the ToolBox.

BTW, the Ax in front of the control name indicates that the control is an "Active-X" control, with a special hosing program (AxHost) to allow it to be used in Windows 7.

Edit: Have a look at MSDN and the Acrobat Users Forum

You will need to have Adobe Acrobat Reader on your system.

This post has been edited by lar3ry: 05 February 2013 - 05:08 PM

Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

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

Posted 05 February 2013 - 05:47 PM

A small change would be to convert the extension to upper-case when checking:

If strFoundFile.Substring(strFoundFile.LastIndexOf(".")).toUpper() = ".PDF" Then


but I don't have the answer for the only last one print question. Does it work if we use LoadFile rather than src = ?

This post has been edited by andrewsw: 05 February 2013 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

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

Posted 05 February 2013 - 05:56 PM

I believe if we add a Sleep after both LoadFile and PrintPages it might work, as it allows time for the document to be loaded and added to the print queue.

This post has been edited by andrewsw: 05 February 2013 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#13 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 05 February 2013 - 07:57 PM

View Postandrewsw, on 05 February 2013 - 06:56 PM, said:

but I don't have the answer for the only last one print question. Does it work if we use LoadFile rather than src = ?

I believe if we add a Sleep after both LoadFile and PrintPages it might work, as it allows time for the document to be loaded and added to the print queue.

No, LoadFile doesn't change anything.

I was thinking about Sleep(), but I'm not very familiar with it, and have no idea if it would Sleep() the printing operation. It's easy enough to check, I suppose, so I'll give it a try.
Was This Post Helpful? 0
  • +
  • -

#14 Essenti4l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 13

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

Posted 06 February 2013 - 06:55 AM

I was thinking about using PDFSharp or iText# to split the PDF documents in the folder, then merge them all together and print the compilation.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3243
  • View blog
  • Posts: 10,882
  • Joined: 12-December 12

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

Posted 06 February 2013 - 07:03 AM

View Postlar3ry, on 05 February 2013 - 07:57 PM, said:

I was thinking about Sleep(), but I'm not very familiar with it, and have no idea if it would Sleep() the printing operation. It's easy enough to check, I suppose, so I'll give it a try.


Imports System.Threading

Thread.Sleep(4) ' seconds

Give it a long stretch initially!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2