Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a task whereby I must export a windows form in VS 2019 (C++) to a pdf but I am unsure on how to go about doing this. What is the best library to use and the specific code needed?

Thanks
Daelin Parmanand

What I have tried:

I've tried using libharu but I did not have any success
Posted
Updated 15-Jun-21 16:36pm
Comments
[no name] 15-Jun-21 18:53pm    
It's usually a 2 step process: (1.) Capture window / screen to image (2.) Save / print image to pdf. If you search for those, you'll have better luck.
Member 15248812 15-Jun-21 19:24pm    
Thank you, will have a look at that

1 solution

Taken that you use C++/CLI with .NET, one quite simple way would be to print the form to a PDF file.

1. Windows 10 has PDF printer driver installed by default but if you're missing it, then have a look at How to Add or Reinstall the Microsoft PDF Printer - Microsoft Community[^]

2. Capture and print the form, have a look at How to: Print a Windows Form - Windows Forms .NET Framework | Microsoft Docs[^]

If the PDF printer is not the default printer, remember to set it as default or change the printer in your code, see PrintDocument.PrinterSettings Property (System.Drawing.Printing) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Member 15248812 16-Jun-21 3:35am    
Much appreciated, thank you.
Member 15248812 16-Jun-21 4:00am    
One more question, would the code in 2 be in a separate file or will that also go into the form.h file?
Wendelius 16-Jun-21 10:50am    
I guess that depends on how you want to organize your code. There's no single 'correct' place for the code.
Member 15248812 16-Jun-21 18:34pm    
I've pasted the CaptureScreen code below into an event handler for a button in my code. I've tried to convert it to c++, however I get an error for Size s (VS says that it expects ; after Size) and for Graphics->FromImage(memoryImage), Graphics is underlined and VS says that type name is not allowed. Any idea how I can fix this?

Graphics^ myGraphics = this->CreateGraphics();
Size s = this->Size;
memoryImage = gcnew Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics->FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this->Location.X, this->Location.Y, 0, 0, s);
Wendelius 16-Jun-21 22:31pm    
Since this is a bit different issue than the original question, I believe it's best if you post a whole new question with the relevant code parts and explanation.

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