PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 20, 2018 2:44 am

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Convert txt file to pdf
PostPosted: Sun Mar 11, 2018 4:20 pm 

Joined: Sun Mar 11, 2018 3:58 pm
Posts: 1
I have a project that requires reading in a number of txt files of various lengths and converting them to pdf. What is the most straight forward way to do that? I have varying page sizes I have to deal so I want to just read in my text and create a PdfDocument. Is there a way to just have the DrawString auto size and add pages automatically or must I do all those calculations to keep adding pages and rectagles? Are there more samples our there somewhere that can show how to handle multiple unknown pages?


var fileTxt =

// Set font encoding to unicode
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode);
XFont font = new XFont("Courier New", 6, XFontStyle.Regular, options);

// Create new document
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();

XGraphics gfx = XGraphics.FromPdfPage(page);

XTextFormatter tf = new XTextFormatter(gfx);
tf.Alignment = XParagraphAlignment.Left;

tf.DrawString(fileTxt, font, XBrushes.Black,
new XRect(20, 20, page.Width-20, 300), XStringFormats.TopLeft);

Reply with quote  
PostPosted: Sun Mar 11, 2018 5:00 pm 
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 381
Location: CCAA
I would most likely use MigraDoc's Document class and add each line of the TXT file using AddParagraph.
You get the page breaks for free.

Another option is the XTextFormatter class or the XTextFormatterEx family of classes - but you still have to add the page breaks and take care of page margins.

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 4 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group