This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Pipeline as Code and have Mohamed Labouardy on-line!
See this thread for details.
Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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:
  • Quote
  • 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?
 
Saloon Keeper
Posts: 6890
163
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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.
 
Saloon Keeper
Posts: 12816
278
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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: 1303
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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:
  • Quote
  • 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.
 
Yup, yup, yup. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic