Nathan Cosbie-Ross
Nathan Cosbie-Ross

Reputation: 27

UWP C# Convert png to a pdf

Not sure if there is an easy thing out there for this. Is there a way so convert a png to a pdf file in a UWP program? I've got a program that reads in a lot of images and I'd like to edit them, then output as a pdf. I just need to convert a 1280 x 720 png file into a landscape pdf in the same location with the same file name so hopefully this could be done without the user needing to input too much.

I'm not really sure where to start on this.

Any pointers to anything relevant would be appreciated.

Upvotes: 1

Views: 916

Answers (4)

Yannick Turbang
Yannick Turbang

Reputation: 388

You can use the nuget package PdfSharp, it's the best library for C#

http://www.pdfsharp.net

enter image description here

Upvotes: 1

Nico Zhu
Nico Zhu

Reputation: 32785

UWP C# Convert png to a pdf

For this scenario, the better way is call Print api to print png to pdf with Microsoft Print to PDF printer . You could refer to document here. And we also provide printer helper for printing quickly. For more detail please refer printhelper.

Upvotes: 1

K J
K J

Reputation: 11827

Windows has native conversion of image to PDF (but you need to make the connections via the shell) and control is limited

So here 1280 x 720 image.png will by default need A3 but I could change that in windows forms.

enter image description here

using command

rundll32 "C:\WINDOWS\system32\shimgvw.dll" ImageView_PrintTo "c:\mydata\1280 x 720.png" "My Print to PDF"

I cheated by having "My Print to PDF" (A3 - headless) duplicate "Microsoft Print to PDF" (A4 - prompting) but with scripting could have set a custom 1280 x 720 page size.

IF (big if) you want 1 page PDF with imbedded image that's in a %PDF-... %EOF wrapper you could do that with the simplest of text prefix and suffix strings, possibly even string pages together. However its much simpler to use any cli PDF manipulator to do that for you such as https://www.win2pdf.com/doc/command-line-png-to-pdf.html and UWP is a big overhead thus usually includes more functions than you need.

Disclaimer I support SumatraPDF which is one portable exe that can headless command line print PNG folder.zip to Multi page Images.pdf However for a PNG quality approach would need manual "save as" PDF scripting not hidden printer commands.

enter image description here

Upvotes: 1

Margus
Margus

Reputation: 20058

First google result: https://www.c-sharpcorner.com/UploadFile/26b237/insert-an-image-to-pdf-in-C-Sharp/

Not promoting the component, but it shows how it is done. You create a .pdf document and load the image into it with desired styles and save the document.

enter image description here

We usually do not propose solutions (there are multiple ways). Rather we help you along the way - you should try something yourself, and when you get stuck then show what you tried.

Upvotes: 1

Related Questions