PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Convert a Text File to a PDF File
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2042
Page 1 of 1

Author:  mamamia [ Wed Jun 06, 2012 8:03 am ]
Post subject:  Convert a Text File to a PDF File

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!

Author:  Thomas Hoevel [ Wed Jun 06, 2012 8:08 am ]
Post subject:  Re: Convert a Text File to a PDF File

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

Author:  mamamia [ Wed Jun 06, 2012 12:56 pm ]
Post subject:  Re: Convert a Text File to a PDF File

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

Author:  Thomas Hoevel [ Wed Jun 06, 2012 3:01 pm ]
Post subject:  Re: Convert a Text File to a PDF File

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.

Author:  Janardhan [ Tue Jul 09, 2013 1:15 am ]
Post subject:  Re: Convert a Text File to a PDF File

Does migradoc can hold the format as similar to the text ? or do the code has to handle it?

Author:  Thomas Hoevel [ Tue Jul 09, 2013 7:57 am ]
Post subject:  Re: Convert a Text File to a PDF File

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/