Hi
I looked to this blog where I can get an idea to convert a text file to a pdf file, but failed to create the instance of Document and PDF writer classes.
Any idea? I am using windows form application - C#
if you can help with any piece of code which convert a text file to pdf this would be great. Thank
Slavica MatijeviPosted Oct 13, 2014, 4:24 AM
Hi Eline here is something that I use:
var document = DocumentModel.Load("Sample.txt", LoadOptions.TxtDefault);
document.Save("Sample.pdf", SaveOptions.PdfDefault);
You need to add reference to this Word library written in C#. Also here is another sample how you can generate your PDF file in .NET.
Eline SamiPosted Aug 21, 2014, 5:55 AM
Hi,
Well, I used PdfSharp libraries and used the following codes.
But I have a problem. Although the code reads all the lines in the text file, but the pdf file which is created then contains only one page!
Which piece of code I need to change or might add another code to add pages as much as there is contents in the text file?
The Text file that I have might contains many rows and if you convert to pdf it might be around 5-10 pages or even more.
private static void ConvertToPDF()
{
try
{
string line = null;
//a Text Reader Object to read the text from Text file.
System.IO.TextReader readFile = new StreamReader(file);
int yPoint = 0;
//create a PDF Object and a Page Object.
PdfDocument pdf = new PdfDocument();
PdfPage pdfPage = pdf.AddPage();
//Also initialize the Graphics and Font
XGraphics graph = XGraphics.FromPdfPage(pdfPage);
XFont font = new XFont("Lucida Console", 10, XFontStyle.Regular);
//Now the PDF Object is ready to write the contents, so the next step is to read the content from the Text file and write to the PDF .
while (true)
{
line = readFile.ReadLine();
if (line == null)
{
break;
}
else
{
graph.DrawString(line, font, XBrushes.Black, new XRect(20, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
yPoint = yPoint + 20;
}
}
//After read all the line from Txt file, you can save the PDF file with your desired name with .pdf extension.
string pdfFilename = myPDF.pdf";
pdf.Save(pdfFilename);
//Then close the Reader Object.
readFile.Close();
readFile = null;
Process.Start(pdfFilename);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Alex SmithPosted Aug 21, 2014, 12:00 AM
Yes, of course.
This reference is availabe by this link :http://www.e-iceblue.com/Download/download-pdf-for-net-now.html You can download the package version 3.1.9, and have a test..
Eline SamiPosted Aug 20, 2014, 8:30 AM
Also, I cannot use
Do I need to add references? if yes, from where?
Ankur JainPosted Aug 20, 2014, 7:05 AM
try this...
please mark it as answer if its helpful..