Friday, May 12, 2006

Pasando un archivo de texto a PDF


  • Esta vez mostrarĂ© como pasar un archivo de texto a formato pdf, es decir de un .txt a un .pdf, para esto hay que utilizar itextsharp.dll que lo puedes buscar en este enlace: http://itextsharp.sourceforge.net/ , encontraras informacion para poder hacer PDFs desde tu plataforma C#, es muy sencillo de usar y OpenSource, es decir, gratis... Recuerda que debes cargar la dll a traves de las referencias:
  • agregar referencia
  • Proyectos - examinar
  • Aceptar

Como vez es muy sencillo, obviamente debe existir un archivo .txt en el debug.


using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using System.Security.Cryptography;
namespace ArchivoaPDF
{
class Class1
{
static string LeerArchivo(string path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] abyt = new byte[Convert.ToInt32(fs.Length)];
fs.Read(abyt, 0, abyt.Length);
fs.Close();
return Encoding.UTF8.GetString(abyt);
}
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Ingrese nombre archivo : ");
string nombre = Console.ReadLine();
nombre += ".txt";
/****************************************************************/
string texto = LeerArchivo(nombre);
Console.WriteLine("");
Console.WriteLine(texto);
/****************************************************************/
Console.WriteLine("Ingrese nombre archivo PDF : ");
string pdfNombre = Console.ReadLine();
pdfNombre += ".pdf";;
/****************************************************************/
Document pdf = new Document();
PdfWriter.GetInstance(pdf,new FileStream(pdfNombre, FileMode.Create));
pdf.Open();
pdf.Add(new Paragraph(texto));
pdf.Close();
Console.ReadLine();
}
}
}

Eso serĂ­a por ahora...

0 Comentarios :

Post a Comment

<< Home