PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Wed Nov 05, 2025 5:40 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Jun 06, 2012 8:03 am 
Offline

Joined: Wed Jun 06, 2012 7:43 am
Posts: 2
Hi,
I have a windows form application that create a txt file with a basic string.
But I want to create the file with the pdf format.
Does anybody know whether it is possible to convert a text file to pdf using PDFSharp?

Thank you!


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 06, 2012 8:08 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Hi!

You have to read the text file yourself (or better use the string directly).

If the text will always fit a single page, PDFsharp can easily be used:
http://www.pdfsharp.net/wiki/TextLayout-sample.ashx

I'd use MigraDoc (better formatting options, automatic page breaks):
http://www.pdfsharp.net/wiki/MigraDocHe ... ample.ashx
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 06, 2012 12:56 pm 
Offline

Joined: Wed Jun 06, 2012 7:43 am
Posts: 2
Thank you for your reply.
Actullay my string is dynamic so it may fit more than one page.
I can use the string directly as you say.
I looked at MigraDoc using in the link u sent , but my code coundt find the referance of "PdfDocumentRenderer"

What can simply I do for writing more pages.
The sample ise below.It just writes only one page!

Code:
string filename ="abc.pdf";
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Lucida Console", 6, XFontStyle.Regular);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(20, 20, 575, 870);
//gfx.DrawRectangle(XBrushes.SeaShell, rect);
tf.Alignment = XParagraphAlignment.Left;
tf.DrawString(str , font, XBrushes.Black, rect, XStringFormats.TopLeft);
document.Save(filename);


Thanks


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 06, 2012 3:01 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
mamamia wrote:
I looked at MigraDoc using in the link u sent , but my code coundt find the referance of "PdfDocumentRenderer"
To use MigraDoc, you have to add references to the MigraDoc assemblies or MigraDoc projects.
Your project already has references to either the PDFsharp assemblies or the PDFsharp projects. Just add MigraDoc references of the same type.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 09, 2013 1:15 am 
Offline

Joined: Tue Jul 09, 2013 1:12 am
Posts: 2
Does migradoc can hold the format as similar to the text ? or do the code has to handle it?


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 09, 2013 7:57 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
In the most simple way, you read the text file as a string and add this string to a MigraDoc paragraph - linebreaks in the text file will be linebreaks in MigraDoc.

Or read the text file line by line and add each line as a MigraDoc paragraph. Only a little bit of extra code, but more flexibility to format the resulting MigraDoc document.

If the text files use simple formatting, your code can support that, too.
Code:
This is a Headline
==================

Here comes a bulletlist:
* one
* two
* three
To support this, you have to your own code to parse the text file and format the MigraDoc document as needed.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 102 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