Вы находитесь на странице: 1из 7

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb Imports Imports Imports Imports Imports Imports System iTextSharp.text iTextSharp.text.pdf iTextSharp.text.Image iTextSharp.text.FontFactory System.

IO

Public Class Form1 '******************************************************* '****** ESTRUCTURA RETURN CADENA MULTIPLES PARTES ****** '******************************************************* Public Structure Partes Public str_anterior As String Public str_letra_negrita As String Public str_siguiente As String End Structure Public Structure Linea Public str_anterior As Chunk Public str_letra_negrita As Chunk Public str_siguiente As Chunk End Structure Public bln_ejecutando As Boolean = False '************************************ '****** FUNCIONES DEL PROGRAMA ****** '************************************ '****** INSERTAR CABECERA CON Y SIN LOGO ***** '********************************************* Private Function InsertLogo(ByVal bln_logo As Boolean) Dim Parrafo As New Paragraph If bln_logo = True Then 'insertar logo y pasar lneas Dim logotip As iTextSharp.text.Image logotip = iTextSharp.text.Image.GetInstance("f:\logoapa.jpg") logotip.SetAbsolutePosition(65, 750) logotip.ScaleAbsoluteWidth(120) logotip.ScaleAbsoluteHeight(65) Parrafo.Add(logotip) End If 'pasar las lineas Dim int_cont As Integer = 0 Do While int_cont < 4 Parrafo.Add(New Paragraph("")) int_cont += 1 Loop Return (Parrafo) End Function '****** FUNCION TAMAO DE LETA ****** '************************************ Private Function Letra(ByVal tipo As Integer, ByVal size As Integer) Dim fuente As iTextSharp.text.Font Select Case tipo Case 0 fuente = New iTextSharp.text.Font(FontFactory.GetFont(FontFactory.COURIER, size, iTextSharp.text.Font.BOLD)) Case 1 fuente = New iTextSharp.text.Font(FontFactory.GetFont(FontFactory.COURIER, size, iTextSharp.text.Font.NORMAL)) Case 2 fuente = New iTextSharp.text.Font(FontFactory.GetFont(FontFactory.COURIER, size, iTextSharp.text.Font.ITALIC)) Case 3 fuente = New iTextSharp.text.Font(FontFactory.GetFont(FontFactory.COURIER, size, iTextSharp.text.Font.UNDERLINE)) End Select Return (fuente) End Function '****** FUNCION PARTIR CADENA ******

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb '*********************************** Private Function PartirLinea(ByVal str_linea As String, ByVal int_tipo As Integer) Dim int_pos As Integer = 0 Dim int_fin, int_pos1, int_fin1 As Integer Select Case int_tipo Case 0 'devolver cadena sin left int_pos = InStr(str_linea, "&left&") If int_pos = 0 Then int_pos += 7 Else int_pos += 6 End If Return (Microsoft.VisualBasic.Mid(str_linea, int_pos, str_linea.Length)) Case 1 'devolver cadena empieza negrita int_pos = InStr(str_linea, "&black&") int_pos1 = InStr(str_linea, "&ofblack&") int_fin1 = int_pos1 + 9 int_fin = 0 If int_pos = 0 Then int_fin = int_pos + 8 Else int_fin = int_pos + 7 End If Dim Partes As New Partes If int_pos = 0 Or int_pos = 1 Then Partes.str_anterior = "" Partes.str_letra_negrita = Microsoft.VisualBasic.Mid(str_linea, int_fin, int_pos1 - int_fin) Partes.str_siguiente = Microsoft.VisualBasic.Mid(str_linea, int_fin1, str_linea.Length) Else Partes.str_anterior = Microsoft.VisualBasic.Mid(str_linea, 1, int_pos - 1) Partes.str_letra_negrita = Microsoft.VisualBasic.Mid(str_linea, int_fin, int_pos1 - int_fin) Partes.str_siguiente = Microsoft.VisualBasic.Mid(str_linea, int_fin1, str_linea.Length) End If Return Partes Case 2 'devolver cadena empieza letra int_pos = InStr(str_linea, "&letra&") int_pos1 = InStr(str_linea, "&ofletra&") int_fin1 = int_pos1 + 9 int_fin = 0 If int_pos = 0 Then int_fin = int_pos + 8 Else int_fin = int_pos + 7 End If Dim Partes As New Partes If int_pos = 0 Or int_pos = 1 Then Partes.str_anterior = "" Partes.str_letra_negrita = Microsoft.VisualBasic.Mid(str_linea, int_fin, int_pos1 - int_fin) Partes.str_siguiente = Microsoft.VisualBasic.Mid(str_linea, int_fin1, str_linea.Length) Else Partes.str_anterior = Microsoft.VisualBasic.Mid(str_linea, 1, int_pos - 1) Partes.str_letra_negrita = Microsoft.VisualBasic.Mid(str_linea, int_fin, int_pos1 - int_fin) Partes.str_siguiente = Microsoft.VisualBasic.Mid(str_linea, int_fin1, str_linea.Length) End If Return Partes Case 3 'devolver cadena sin left int_pos = InStr(str_linea, "&topleft&") If int_pos = 0 Then int_pos += 10

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb Else int_pos += 9 End If Return (Microsoft.VisualBasic.Mid(str_linea, int_pos, str_linea.Length)) End Select End Function '************************************ '****** FIN FUNCIONES PROGRAMA ****** '************************************ Private Sub btn_explorador_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles btn_explorador.Click Try With Carpeta 'mi pc .RootFolder = Environment.SpecialFolder.MyComputer Dim result As DialogResult = .ShowDialog If result = Windows.Forms.DialogResult.OK Then 'ruta seleccionada Me.path.Text = .SelectedPath End If End With Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical, "Problemas con el explorador de windows") End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'salir de la aplicacin Me.Close() End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Try bln_ejecutando = True Me.txt_procesado.Text = "" 'contar archivos directorio Dim ContarArchivos As System.Collections.ObjectModel.ReadOnlyCollection(Of String) ContarArchivos = My.Computer.FileSystem.GetFiles(Me.path.Text, FileIO.SearchOption .SearchTopLevelOnly, "*.TXT") Me.progreso.Maximum = ContarArchivos.Count Me.progreso.Minimum = 0 Me.progreso.Value = 0 For Each file As String In My.Computer.FileSystem.GetFiles(path.Text, FileIO. SearchOption.SearchTopLevelOnly, "*.TXT") If Me.txt_pendientes.Text.Length <> 0 Then Me.txt_pendientes.Text = Me.txt_pendientes.Text & vbCrLf & My.Computer. FileSystem.GetName(file) Else Me.txt_pendientes.Text = My.Computer.FileSystem.GetName(file) End If Next Application.DoEvents() Dim str_nombre As String Dim bln_write As Boolean = False For Each Archivo As String In My.Computer.FileSystem.GetFiles(path.Text, FileIO. SearchOption.SearchTopLevelOnly, "*.TXT") Me.txt_pendientes.Text = "" For Each file As String In My.Computer.FileSystem.GetFiles(path.Text, FileIO. SearchOption.SearchTopLevelOnly, "*.TXT") If Me.txt_pendientes.Text.Length <> 0 Then Me.txt_pendientes.Text = Me.txt_pendientes.Text & vbCrLf & My.Computer .FileSystem.GetName(file) Else Me.txt_pendientes.Text = My.Computer.FileSystem.GetName(file) End If Next 'nombre arhivo pdf str_nombre = Microsoft.VisualBasic.Left(My.Computer.FileSystem.GetName (Archivo), (My.Computer.FileSystem.GetName(Archivo).Length - 4))

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb 'crear lector archivos Dim str_Lectura As StreamReader 'codificacin archivo Dim Encoding As System.Text.Encoding = System.Text.Encoding.Default 'abrir archivo str_Lectura = New StreamReader(Archivo, Encoding, False)

'fichero pdf Dim pdf As Document = New Document(iTextSharp.text.PageSize.A4_LANDSCAPE) 'escribir archivo Dim writer As PdfWriter = PdfWriter.GetInstance(pdf, New System.IO.FileStream (My.Computer.FileSystem.GetParentPath(Archivo) & "\" & str_nombre & ".PDF", System.IO. FileMode.Create)) writer.ViewerPreferences = PdfWriter.PageModeUseThumbs 'abrir documento pdf.Open() 'hasta el final del archivo Dim str_lin As String Dim parrafo As Paragraph Dim frase As Phrase Do While str_Lectura.EndOfStream <> True str_lin = str_Lectura.ReadLine() bln_write = False parrafo = New Paragraph parrafo.Font = Letra(1, 10) parrafo.Leading = 11 frase = New Phrase Application.DoEvents() 'la frase no tiene longitud If str_lin.Length = 0 Then frase = New Phrase frase.Add(vbCrLf) bln_write = True Application.DoEvents() End If 'la frase no contiene ningn parametro If InStr(str_lin, "&black&") = 0 And InStr(str_lin, "&letra&") = 0 Then frase = New Phrase frase.Add(str_lin) bln_write = True Application.DoEvents() End If 'inicio documento con logo If InStr(str_lin, "&start") <> 0 Then pdf.NewPage() pdf.Add(InsertLogo(True)) bln_write = False Application.DoEvents() End If 'inicio documento sin logo If InStr(str_lin, "&ofstart") <> 0 Then pdf.NewPage() pdf.Add(InsertLogo(False)) bln_write = False Application.DoEvents() End If 'nueva pgina con logo If InStr(str_lin, "&page") <> 0 Then pdf.NewPage() pdf.Add(InsertLogo(True)) bln_write = False Application.DoEvents() End If 'nueva pgina sin logo If InStr(str_lin, "&ofpage") <> 0 Then pdf.NewPage() pdf.Add(InsertLogo(False)) bln_write = False Application.DoEvents() End If 'direccin tcnica If InStr(str_lin, "&end") <> 0 Then

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb bln_write = False parrafo.IndentationLeft = 30 pdf.Add(parrafo) parrafo = New Paragraph parrafo.Font = Letra(1, 10) parrafo.Leading = 11 parrafo.IndentationLeft = 60 Do str_lin = str_Lectura.ReadLine If InStr(str_lin, "&ofend&") <> 0 Then Exit Do End If frase = New Phrase(str_lin & vbCrLf) frase.Font = New iTextSharp.text.Font(FontFactory.GetFont (FontFactory.COURIER, 10, iTextSharp.text.Font.NORMAL)) parrafo.Add(frase) Application.DoEvents() Loop pdf.Add(parrafo) End If 'pie de pgina If InStr(str_lin, "&feet") <> 0 Then bln_write = False parrafo = New Paragraph parrafo.Font = Letra(1, 6) parrafo.Leading = 6 parrafo.Alignment = Element.ALIGN_LEFT Do str_lin = str_Lectura.ReadLine If StrComp(str_lin, "&offeet") = 0 Then Exit Do End If frase = New Phrase(str_lin & vbCrLf) frase.Font = New iTextSharp.text.Font(FontFactory.GetFont (FontFactory.COURIER, 6, iTextSharp.text.Font.NORMAL)) parrafo.Add(frase) Application.DoEvents() Loop parrafo.Alignment = iTextSharp.text.Element.ALIGN_LEFT 'parrafo.IndentationLeft = 30 pdf.Add(parrafo) Application.DoEvents() End If 'procesar margen If InStr(str_lin, "&left&") <> 0 Then parrafo.IndentationLeft = 25 str_lin = PartirLinea(str_lin, 0) frase = New Phrase frase.Add(str_lin) Application.DoEvents() Else If InStr(str_lin, "&topleft&") <> 0 Then parrafo.IndentationLeft = 0 str_lin = PartirLinea(str_lin, 3) frase = New Phrase frase.Add(str_lin) Application.DoEvents() Else parrafo.IndentationLeft = 30 Application.DoEvents() End If End If 'procesar negritas If InStr(str_lin, "&black&") <> 0 And InStr(str_lin, "&letra&") = 0 Then 'solo hay negritas Dim str_temp As New Partes str_temp = PartirLinea(str_lin, 1) Dim pre_frase As New Linea frase = New Phrase pre_frase.str_anterior = New Chunk(str_temp.str_anterior, Letra(1, 10)) pre_frase.str_letra_negrita = New Chunk(str_temp.str_letra_negrita, Letra(0, 10)) pre_frase.str_siguiente = New Chunk(str_temp.str_siguiente, Letra(1,

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb 10)) frase.Add(pre_frase.str_anterior) frase.Add(pre_frase.str_letra_negrita) frase.Add(pre_frase.str_siguiente) bln_write = True Application.DoEvents() End If 'procesar cambio letra If InStr(str_lin, "&letra&") <> 0 And InStr(str_lin, "&black&") = 0 Then 'solo hay cambio de letra Dim str_temp As New Partes str_temp = PartirLinea(str_lin, 2) Dim pre_frase As New Linea frase = New Phrase pre_frase.str_anterior = New Chunk(str_temp.str_anterior, Letra(1, 10)) pre_frase.str_letra_negrita = New Chunk(str_temp.str_letra_negrita, Letra(1, 8)) pre_frase.str_siguiente = New Chunk(str_temp.str_siguiente, Letra(1, 10))

frase.Add(pre_frase.str_anterior) frase.Add(pre_frase.str_letra_negrita) frase.Add(pre_frase.str_siguiente) bln_write = True Application.DoEvents() End If If InStr(str_lin, "&black&") <> 0 And InStr(str_lin, "&letra&") <> 0 Then 'hay negrita y cambio de letra If InStr(str_lin, "&black") < InStr(str_lin, "&letra&") Then 'primero negrita Dim str_temp As New Partes str_temp = PartirLinea(str_lin, 1) Dim pre_frase As New Linea frase = New Phrase pre_frase.str_anterior = New Chunk(str_temp.str_anterior, Letra(1, 10)) pre_frase.str_letra_negrita = New Chunk(str_temp.str_letra_negrita , Letra(0, 10)) frase.Add(pre_frase.str_anterior) frase.Add(pre_frase.str_letra_negrita) 'luego letra str_temp = PartirLinea(str_temp.str_siguiente, 2) pre_frase.str_letra_negrita = New Chunk(str_temp.str_letra_negrita , Letra(1, 8)) pre_frase.str_siguiente = New Chunk(str_temp.str_siguiente, Letra (1, 10)) frase.Add(pre_frase.str_letra_negrita) frase.Add(pre_frase.str_siguiente) bln_write = True Application.DoEvents() Else 'primero letra Dim str_temp As New Partes str_temp = PartirLinea(str_lin, 2) Dim pre_frase As New Linea frase = New Phrase pre_frase.str_anterior = New Chunk(str_temp.str_anterior, Letra(1, 10)) pre_frase.str_letra_negrita = New Chunk(str_temp.str_letra_negrita , Letra(1, 8)) pre_frase.str_siguiente = New Chunk(str_temp.str_siguiente, Letra (1, 10)) frase.Add(pre_frase.str_anterior) frase.Add(pre_frase.str_letra_negrita) 'luego negrita str_temp = PartirLinea(str_lin, 1) pre_frase.str_letra_negrita = New Chunk(str_temp.str_letra_negrita , Letra(0, 10)) pre_frase.str_siguiente = New Chunk(str_temp.str_siguiente, Letra (1, 10)) frase.Add(pre_frase.str_letra_negrita) frase.Add(pre_frase.str_siguiente) bln_write = True Application.DoEvents()

F:\Newfonts\NewGenerarPdf\NewGenerarPdf\Form1.vb End If End If 'escribir linea o parrafo en el pdf If bln_write = True Then frase.Add(vbCrLf) parrafo.Add(frase) pdf.Add(parrafo) Application.DoEvents() End If Loop 'cerar Archivo pdf.Close() str_Lectura.Close() 'aadir archivos procesados If Me.txt_procesado.Text.Length <> 0 Then Me.txt_procesado.Text = Me.txt_procesado.Text & vbCrLf & My.Computer. FileSystem.GetName(Archivo) Else Me.txt_procesado.Text = My.Computer.FileSystem.GetName(Archivo) End If

'eliminar archivo My.Computer.FileSystem.DeleteFile(Archivo) Me.progreso.Value += 1 Me.lbl_por.Text = CInt((Me.progreso.Value * 100) / ContarArchivos.Count) & "%" Application.DoEvents() Next bln_ejecutando = False Me.txt_pendientes.Text = "" Me.lbl_por.Text = "0%" System.Threading.Thread.Sleep(1000) Button1_Click(sender, e) 'Catch ex As Exception 'MsgBox(ex.ToString, MsgBoxStyle.Critical, "Avisa a TXEMA!!!!!") 'End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.lbl_por.Text = "0%" End Sub Private Sub form1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus Button1_Click(sender, e) End Sub End Class

Вам также может понравиться