Lo primero es descargar las librerias para Visual Studio .NET de PDFsharp de la página www.pdfsharp.com/PDFsharp. En esta web existen ejemplos, FAQ, entre otros recursos.
Ahora, con Visual Studio .NET 2010 y usando Visual Basic crearemos una aplicación Windows Form.
Realizaremos una interfaz sencilla, por ejemplo:
Para poder usar las librerias primero se descomprime el archivo descargado, en nuestro caso se llama PDFsharp-MigraDocFoundation-Assemblies-1_32.zip. Después agregamos las dll a la solución de Visual Studio .NET.
Cómo agregar una referencia:
- Pulsamos sobre el icono Mostrar todos los iconos del explorador de soluciones situado por defecto en la parte derecha de la pantalla.
- Sobre References pulsamos botón derecho del ratón y Agregar referencia...
- En la pestaña Examinar buscamos PdfSharp.dll y la agregamos a la solución
Ahora ya podemos usar la libreria en nuestra solución.
La apliación realiza lo siguiente:
- Abre un archivo de texto de una ruta predefinida.
- Lee y almacena el texto en un array.
- Convierte el array en formato pdf, mediante la libreria PDFsharp.
- Abre el documento pdf y muestra en el campo Log los pasos y el archivo de texto.
Para realizar más operaciones con los pdf y esta librería, como poner contraseña, ir a la pagina web de PDFsharp.
El código completo es:
' Read file .txt
Imports System
Imports System.IO
' Convert To PDF
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Public Class frmMain
Private Sub frmMain_Load(sender As System.Object,
e As System.EventArgs)
Handles MyBase.Load
txtLog.Text = "Inicio." & vbCrLf
txtPathFile.Text = "C:\ElFosoDelSarlacc\ElFosoDelSarlacc.txt"
End Sub
Private Sub btnConvert_Click(sender As System.Object,
e As System.EventArgs)
Handles btnConvert.Click
'ReadFileTxt()
ConvertToPDF(ReadFileTxt())
End Sub
Private Function ReadFileTxt()
' Declaramos el objeto lector y pasamos la ruta del archivo
Dim objReader As New StreamReader(txtPathFile.Text)
txtLog.Text = (txtLog.Text & vbCrLf &
"Lectura de archivo " & _
txtPathFile.Text & vbCrLf & "Contenido: " & vbCrLf)
' Para la lectura del archivo txt se usa
' un array como muestra el ejemplo de Microsoft
' en la página http://support.microsoft.com/kb/302309/es
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
txtLog.Text = (txtLog.Text & sLine & vbCrLf)
End If
Loop Until sLine Is Nothing
objReader.Close()
txtLog.Text = (txtLog.Text & vbCrLf & "----- Fin de lectura -----")
' Devolvemos el contenido del archivo leído
Return arrText
End Function
Private Sub ConvertToPDF(ByVal arrTextoConvertir As ArrayList)
' Create a new PDF document
Dim document As PdfDocument = New PdfDocument
document.Info.Title = "Created with PDFsharp"
' Create an empty page
Dim page As PdfPage = document.AddPage
' Get an XGraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Create a font
Dim font As XFont = New XFont("Verdana", 10, XFontStyle.Italic)
' Draw the text
For Each sLine In arrTextoConvertir
Dim intEjeY As Integer
Dim intEjeX As Integer
gfx.DrawString(sLine, font, XBrushes.Black,
New XRect(intEjeX, intEjeY, page.Width.Point,
page.Height.Point),XStringFormats.TopLeft)
' Bajamos 11 puntos el eje Y
intEjeY = intEjeY + 11
Next
' Save the document...
Dim filename As String = "ElFosoDelSarlacc.pdf"
document.Save(filename)
' ...and start a viewer.
Process.Start(filename)
End Sub
End Class




No hay comentarios:
Publicar un comentario