Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to write to PDF file in C#?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have recently learned about File.WriteAllText and File.AppendAllText methods in C#. As I understand it, File.WriteAllText creates txt files and writes text to them without any problem.
But I tried this code in a console app:



It generates a "MyTest.pdf" file, but any PDF reader/viewer cannot open this file, as if it is corrupted. Seems like System.IO can only generate txt files, and assigns them whatever the extension that we specify.
So my question is how can I generate valid pdf files directly from C# code? If I need any exterior library for that purpose could you suggest any free ones?
 
Bartender
Posts: 7645
178
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Welcome to the Ranch.

A text files is a text file, no matter what file extension you give it. I'm not really familiar with C#, but I think you need to use a PDF library, just like you'd need to do in Java. Searching for "crate pdf c# library" yields lots of results; maybe the list at https://csharp-source.net/open-source/pdf-libraries is helpful.
 
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
As far as C# libraries go, I'm aware of iText and PDFsharp. I don't have experience with them so I can't give a recommendation.

Personally I've always worked with Aspose Words and Aspose PDF, but they don't offer a free license.
 
Bartender
Posts: 1382
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Stephan van Hulst wrote:As far as C# libraries go, I'm aware of iText and PDFsharp. I don't have experience with them so I can't give a recommendation.



I used to use iText with Java, and, to a lesser extend, with C#. It's a very good library, well documented, honestly a bit low level - you have to directly deal with Chunks, Phrases, Paragraph and so on.
 
Bekzat Karayev
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for the replies, I will definitely check out the solutions that you suggested.

Right now, I am playing around with this PDFFlow library.  

I added it to my console app through NuGet Package Manager, it generates PDF files pretty easily, without much code.
I don't even need to use any System.IO methods. The library provides its own methods, that are named pretty straightforward, like this:



After running the console app, I get a legit PDF file inside Debug folder that can be opened in any viewer.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Wow, thank you to Bekzat. PDFFlow worked for me. I used you code and it generated PDF. You saved me)) I also found link to examples of real business documents there, downloaded one and compiled successfully. Examples are here: github.com/gehtsoft-usa/PDF.Flow.Examples
 
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Welcome to the Ranch
Are you promoting your own work or somebody else's?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic