Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu May 05, 2022 12:27 pm

Hello,

I'm trying to set the paper size before printing but without sucess

The user may be able to select the paper size manually from a combobox, is that possible?

With the PrintDocument it works, but when i try to do the same thing with pdf printing it isn't working

Here's the PrintDocument Code
"PrintDocument1.PrinterSettings.PrinterName = cmbPrinters.Text
Dim psize As Printing.PaperSize
psize = PrintDocument1.PrinterSettings.PaperSizes.Item(papertype.SelectedIndex)

PrintDocument1.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
PrintDocument1.DefaultPageSettings.PaperSize = psize
PrintDocument1.DefaultPageSettings.Landscape = True
PrintDocument1.Print()

Code from printing pdf
Dim doc As New PdfDocument()
'Load a PDF file
doc.LoadFromFile(newfilepdf.Text)
'Specify printer name
doc.PrintSettings.PrinterName = cmbPrinters.Text
'Select a page range to print
doc.PrintSettings.SelectPageRange(1, 1)
'Set PaperSize
Dim psize As New PaperSize With {.RawKind = papertype.SelectedIndex}
doc.PrintSettings.PaperSize = psize
'silent printing
doc.PrintSettings.PrintController = New StandardPrintController()
'Print document
doc.Print()

I've search on some forums and weren't able to find an answer

I'm using the free version o Spire.Office

Thank you for the help, and i'm sorry if there's something i'm not doing correctly here on the forum

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Fri May 06, 2022 7:17 am

Hello,

Thanks for your inquiry.
After investigation according to the code you provided, to resolve your issue, you need to add one line code into the original code: doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize, False, 100f).
Please refer to the following code and have a test. If you have any issue, just feel free to contact us.

Code: Select all
Dim doc As New PdfDocument()
'Load a PDF file
doc.LoadFromFile(newfilepdf.Text)
'Specify printer name
doc.PrintSettings.PrinterName = cmbPrinters.Text
'Select a page range to print
doc.PrintSettings.SelectPageRange(1, 1)
'Set PaperSize
Dim psize As New PaperSize With {.RawKind = papertype.SelectedIndex}
doc.PrintSettings.PaperSize = psize
'Setting up the print layout for the page
doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize, False, 100f)
'silent printing
doc.PrintSettings.PrintController = New StandardPrintController()
'Print document
doc.Print()


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Fri May 06, 2022 11:19 am

Hello,

Thank very much for the reply

So i've modified the code as you've mentionned, it is indeed working but not as intended

The printer doesn't chose the correct paper based on the combobox item number, here's as screenshoot you the paper i've chosed and the paper that the printer selected

Maybe the code for the selected item is not correct, the combobox is not sorted so the paper is added in the correct order

Thank you

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Sat May 07, 2022 11:28 am

Hello,

Thanks for your feedback.
I directly set paper size in the code and printed pdf file using the virtual printer- "Microsoft XPS Document Writer" printer and the physical printer "HP Color LaserJet MFP M281fdw". The print result is expected. You can refer to the following code and have a try on your side to see if you can print as expected by directly setting paper size in the code.

Code: Select all
 Dim doc As New PdfDocument()
      doc.LoadFromFile("test.pdf")

      'Specify printer name
      doc.PrintSettings.PrinterName = "Microsoft XPS Document Writer"
      doc.PrintSettings.SelectPageRange(1, 1)
      Dim size As New PaperSize()
      size.RawKind = CInt(PaperKind.A4)
      doc.PrintSettings.PaperSize = size
      'Setting up the print layout for the page
      doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize, False, 100f)
      doc.PrintSettings.PrintController = New StandardPrintController()
      doc.Print()


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Sat May 07, 2022 1:48 pm

Hello,

Thank your for the help now it's working :D

But still not as i would prefer, the user must be able to select the desired paper from the combobox in order to select the various paper sizes available

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Mon May 09, 2022 10:17 am

Hello,

Thanks for your feedback.
To help us reproduce your issue and work out a solution for you, could you please offer complete project. Thanks for your assistance in advance.

Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Mon May 09, 2022 3:14 pm

Hello,

I've found a solution a work around to make it work as i intended

I've added a extra combobox that adds the Enum for the paper type, when select the paper type the combobox will select the right paper type and there for the printing will be done correctly

"Dim doc As New PdfDocument() 'Create a PdfDocument object
doc.LoadFromFile(FinalFilePath) '(newfilepdf.Text) 'Load a PDF file
doc.PrintSettings.PrinterName = CmbPrinters.Text 'Specify printer name
doc.PrintSettings.SelectPageRange(1, 1) 'Select a page range to print
If Orientationcmb.SelectedIndex = 0 Then
doc.PrintSettings.Landscape = True
ElseIf Orientationcmb.SelectedIndex = 1 Then
doc.PrintSettings.Landscape = False
End If
Dim size As New PaperSize With {.RawKind = CInt(Papertypenumber.Text)}
doc.PrintSettings.PaperSize = size
doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.ActualSize, False, 100.0F)
doc.PrintSettings.PrintController = New StandardPrintController() 'silent printing
doc.Print() 'Print document
doc.Dispose()

For this problem it's working, but i have another issue if i chose to print in A3 format for example the pdf will not print as it would

doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, True, 100.0F)

I've tryed to modify this line as bellow but this error (see annexed file) appears

I would share my complete project if there weren't some sensitive data inside :-(

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Tue May 10, 2022 11:11 am

Hello,

Thanks for your feedback.
Please refer to the following code snippet to print with FitSize mode. If there is any other issue, please share us with your PDF file for further investigation.
Code: Select all
.......
Dim printDocument As New PrintDocument()
Dim paperSizes As System.Drawing.Printing.PrinterSettings.PaperSizeCollection = printDocument.PrinterSettings.PaperSizes
For Each ps As PaperSize In paperSizes
   If ps.RawKind = CInt(Papertypenumber.Text) Then
      doc.PrintSettings.PaperSize = ps
   End If
Next ps
doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, True)
.......

Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Tue May 17, 2022 9:57 am

Hello,

Hope you are doing well.
Could you please let me know how is the issue going? Thanks for your feedback and time in advance.

Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Thu May 19, 2022 5:59 pm

Hello,

I'm sorry to answer this late, I'm doing fine how about you?

So yes it's now working perfectly as i wanted, thank you so much for the help :-)

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Mon May 30, 2022 10:21 am

Hello,

You 're welcome, if you have any issue in the furture, just feel free to contact us.

Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Thu Jun 02, 2022 8:37 pm

Hello,

I was wondering if you can help me to improve the printing time

The form seems to freeze when printing and it takes some time to print files

Thank you
"

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Fri Jun 03, 2022 2:11 am

Hello,

Thanks for your feedback.
To help us better investigate the printing time issue, please share the following more information with us. Thanks in advance.
1. Your PDF file, you can attach it here or send it to us via email (support@e-iceblue.com).
2. Your printer name.
3. The time you cost, how about the printing time if you print with Microsoft XPS Document Writer?
4. Your system information (E.g. Win10, 64 bit) and region setting (E.g. China, Chinese).

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon Jun 13, 2022 5:29 pm

Hello,

Sorry for the delayed answer, the printing is quite fast now, but the program seems to freeze when i launch the printing preventing me to launch a message or something else to show that the printing is pending

Here's the complete code i use:

Dim numberofcopies As Integer = 1

'Do While number < qtycolis.Text + 1
Dim PrintingFolder As String = "PrintPending\AFFICHE\" & Form1.sapuserinfo.Text & "\" & Form1.sessionnum.Text & "\"
If (My.Computer.FileSystem.DirectoryExists(PrintingFolder) = False) Then
My.Computer.FileSystem.CreateDirectory(PrintingFolder)
End If

Dim pdfTemplate As String = "formulaires/A0-A4.pdf"
Dim strFolder As String = PrintingFolder
Dim newFile As String = strFolder & Formattxtbox.Text & ".pdf"
Dim pdfReader As New PdfReader(pdfTemplate)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

' ------ SET YOUR FORM FIELDS ------

pdfFormFields.SetField("articletitle", Arttitle.Text)
pdfFormFields.SetField("articlespec", Artdes.Text)
pdfFormFields.SetField("articlespec2", Artdes1.Text)
pdfFormFields.SetField("prixfranc ", Price.Text)

pdfFormFields.SetField("prixcent", Pricecents.Text)
pdfFormFields.SetField("articlenb", Artnb.Text)
pdfFormFields.SetField("uq", Uq.Text)
pdfFormFields.SetField("qtycolis", numberofcopies)


Dim qrcode As String = Artnb.Text
Dim Generator As New MessagingToolkit.Barcode.BarcodeEncoder
'Generator.IncludeLabel = blmnumrec.Text
'Generator.CustomLabel = eancode

qrpic.Image = New Bitmap(Generator.Encode(MessagingToolkit.Barcode.BarcodeFormat.QRCode, qrcode))
'Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, SaveFileDialog1.FileName)
qrpic.Image.Save("PrintPending\AFFICHE\" & Form1.sapuserinfo.Text & "\" & Form1.sessionnum.Text & "\qrcode.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Dim inputImageStream As Stream = New FileStream("PrintPending\AFFICHE\" & Form1.sapuserinfo.Text & "\" & Form1.sessionnum.Text & "\qrcode.jpg", FileMode.Open, FileAccess.Read, FileShare.Read)
Dim pdfContentByte = pdfStamper.GetOverContent(1)
Dim Img = iTextSharp.text.Image.GetInstance(inputImageStream)
'signature lors de la demande
Img.SetAbsolutePosition(730, 30)
Img.ScaleAbsolute(100, 50)

pdfContentByte.AddImage(Img) 'End If
pdfStamper.FormFlattening = True
pdfStamper.Close()
inputImageStream.Close()
Dim randomnum, copiesnb As String
Dim rn As New Random
randomnum = rn.Next(1, 10000)
My.Computer.FileSystem.RenameFile(newFile, randomnum & ".pdf")
Dim FinalFilePath As String = PrintingFolder & randomnum & ".pdf"

Dim doc As New PdfDocument() 'Create a PdfDocument object
doc.LoadFromFile(FinalFilePath) '(newfilepdf.Text) 'Load a PDF file
doc.PrintSettings.PrinterName = CmbPrinters.Text 'Specify printer name
doc.PrintSettings.SelectPageRange(1, 1) 'Select a page range to print
If Orientation1bt.CheckState = True Then
doc.PrintSettings.Landscape = True
ElseIf Orientation2bt.CheckState = True Then
doc.PrintSettings.Landscape = False
End If
Dim size As New PaperSize With {.RawKind = CInt(Papertypenumber.Text)}

Dim printDocument As New PrintDocument()
Dim paperSizes As System.Drawing.Printing.PrinterSettings.PaperSizeCollection = printDocument.PrinterSettings.PaperSizes
For Each ps As PaperSize In paperSizes
If ps.RawKind = CInt(Papertypenumber.Text) Then
doc.PrintSettings.PaperSize = ps
End If
Next ps
doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, True, 100.0F)

doc.PrintSettings.PrintController = New StandardPrintController() 'silent printing
doc.Print() 'Print document
doc.Dispose()

numberofcopies += 1
copiesnb = numberofcopies

End If

Now i also have another problem, we've aquired another printer at work but i'm not able to send the correct printing information to the printer

As you can see on the file i sent the A3 format to the printer that i must change trough the printer quicksettings, but when i go to the pending printings and go to the file properties it always shows me the A4 format

The printer is on the network and it's an HP DesignJet T730

dark_prince69
 
Posts: 10
Joined: Sat Oct 23, 2021 10:29 am

Tue Jun 14, 2022 7:01 am

Hello,

Thanks for your message.
Sorry we do not have the same model printer as yours, I simulated a PDF file, printed it to Microsoft XPS Document Writer and our physical printer(HP Color LaserJet MFP M281fdw),the pages could be printed one after the other, there is no freeze issue. How about the performance when you print files to Microsoft XPS Document Writer? If your issues still occurs, please share your PDF file with us for further testing.

Besides, as for the A3 format issue, we also noticed this issue in recent tests, sorry for the inconvenience caused. However, this issue has been fixed in our latest Spire.PDF Pack(Hot Fix) Version:8.6.1, please download the latest version and refer to the following sample code to print the A3 format.
Code: Select all
 Dim doc As New PdfDocument()
 doc.LoadFromFile("test.pdf")
 doc.PrintSettings.PrinterName = "Microsoft XPS Document Writer"
 doc.PrintSettings.SelectPageRange(1, 1)
 doc.PrintSettings.PaperSize = PdfPaperSizes.A3
 doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, True)
 doc.Print()

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF