PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 6:51 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 
Offline

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?

Thanks

Quote:
var fileTxt =
System.IO.File.ReadAllText(@"K:\SOMEFILE.TXT",
Encoding.UTF8);



// 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);


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 11, 2018 5:00 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
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.
http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

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.
http://pdfsharp.net/wiki/TextLayout-sample.ashx
viewtopic.php?p=9372#p9372

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


Top
 Profile  
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: Bing [Bot], Google [Bot] and 55 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