Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a windows app in VB Net 2019 that has a report that I wish to save as a PDF file. I setup the report in a group box for the ease of printing, that works will. But It is a must to Save the file to be able to use it without the program. A pdf file is the best way, but I cannot get the group box is convert to pdf. I used PDF Sharp to save the form, but the screen shot does not show the full group box and save the complete screen. I need to just save the Group box and all that is inside it, the page is 8 X 10. Can someone help.
The code below is the first that I could get a pdf to save but it displays this in the center of the page "System.Drawing.Graphics"

What I have tried:

Dim pdf As PdfDocument = New PdfDocument
pdf.Info.Title = "My First PDF"
Dim pdfPage As PdfPage = pdf.AddPage
Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
graph.DrawString(Convert.ToString(GroupBox1.CreateGraphics), font, XBrushes.Black,
New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center)
Dim pdfFilename As String = "firstpage.pdf"
pdf.Save(pdfFilename)
Process.Start(pdfFilename)
Posted
Updated 9-Nov-20 2:56am

1 solution

Look at your code:
VB
graph.DrawString(Convert.ToString(GroupBox1.CreateGraphics), font, ... , XStringFormats.Center)

The string you tell it to display is Convert.ToString(GroupBox1.CreateGraphics) which is just the default ToString implementation for the Graphics class. And since it doesn't override ToString, the object default ToString is used, which returns the full name of the object type: System.Drawing.Graphics
You want to print actual Text? You have to locate the actual text inside the controls inside the GroupBox - and print that.

It doesn't know what you want, so it does it's best!
 
Share this answer
 

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