We help IT Professionals succeed at work.

Using C# how to format a pdf file?

rwheeler23
rwheeler23 asked
on
Medium Priority
67 Views
Last Modified: 2019-04-29
Thanks to some expert advice obtained from this web site I can now successfully print a text file complete with a print dialog box. As was to be expected the users are now requesting the output to also be available in pdf format plus they want some values to be bold and others in a different font. Using C# how do I go about creating a pdf file and then formatting values on a record? Any tips and/or links would be greatly appreciated.
Comment
Watch Question

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
Now it is not as simple as printing a text file. You have a few options.

1. You can use a report engine: https://www.emoreau.com/Entries/Articles/2016/10/Crystal-Reports-for-Net-revisited-10-years-later.aspx
2. you can use a specialized library like https://products.aspose.com/pdf/net
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi rwheeler23,

You can use free and open source MigraDoc Foundation Library: http://www.pdfsharp.net/Overview.ashx

It is available on GitHub as well: https://github.com/empira/MigraDoc as on Nuget.

Formatting can be done with the code shown below:
Paragraph para = sec.AddParagraph();
para.Format.Alignment = ParagraphAlignment.Justify;
para.Format.Font.Name = "Times New Roman";
para.Format.Font.Size = 12;
para.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.DarkGray;
para.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.DarkGray;
para.AddText("Duisism odigna acipsum delesenisl ");
para.AddFormattedText("ullum in velenit", TextFormat.Bold);

Open in new window


Regards,
Chinmay.
rwheeler23President

Author

Commented:
I doubt the client wants to spend any more money on this. I can see where this can get lengthy trying to have every column in a different font.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
there are free solutions (Crystal Reports, PDFSharp, ...) but which ever solution you select, it will take quite some times to create the PDF your client wants.
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Can you share a sample file please? and some brief on expected result?
David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Personally I find the clients request to be ridiculous, but if they want to pay for it then it is on their dime.
What I would do is produce a web page with all of the adornments that they wand and they can view the page in their browser and then Print it themselves to PDF
rwheeler23President

Author

Commented:
That is an excellent point. From what I have seen they were dumping data out of MS Access as they would click the PDF button. I could do the same thing with SSRS as the data resides in SQL. I will ask their IT staff if they will allow me to install SSRS.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.