Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/08/2010, 16:03
deltacr
 
Fecha de Ingreso: agosto-2010
Mensajes: 1
Antigüedad: 13 años, 8 meses
Puntos: 0
ItextSharp: Agregar Texto al principio de un archivo ya existente!!! :-(

Buenas amigos;
Llevo ya varios días peleándome con la librería ‘itextsharp’ con Visual Basic 2005 y no he podido encontrar la solución a lo siguiente:

Necesito abrir un documento pdf (ya existente y con contenido), agregarle unas cuantas líneas de texto al principio del mismo, para luego imprimirlo.

Al no haber encontrado una forma de modificar de forma directa el documento; he optado por crear un pdf nuevo, agregarle unas líneas de texto y posteriormente pegarle las páginas del documento que quería modificar originalmente. El problema es que al abrir el documento final, quedan las líneas de texto traspapeladas con las páginas que copie del documento original. Y no en encuentro una forma para que la información copiada del documento original empiece después de las líneas agregadas.

Por favor; alguien que me colabore? 

Muchas gracias por la ayuda que me puedan brindar!!!

Ejemplo del código con el que estoy probando:

Public Function PegarArchivoaPdfsYAV3(ByVal Resolucion As String, ByVal Cedula As String) As Boolean
Dim n As Integer = 0
Dim reader As New iTextSharp.text.pdf.PdfReader(Resolucion)

Dim document As New iTextSharp.text.Document
Dim Contador As Integer = 1
Dim page As PdfImportedPage = Nothing
Dim writer As PdfWriter = Nothing
'Dim writer As New PdfWriter '.GetInstance(document, New FileStream(Cedula, FileMode.Open))
'dim writer as new PdfWriter = PdfWriter.getInstance(document, new FileStream("archivo_destino.pdf", FileMode.Create))
Dim cb As PdfContentByte = Nothing
Dim rotation As Integer = 0


n = reader.NumberOfPages
document = New iTextSharp.text.Document(reader.GetPageSizeWithRot ation(1), 18, 18, 18, 18)
writer = PdfWriter.GetInstance(document, New IO.FileStream(Cedula, IO.FileMode.Create))
document.Open()
cb = writer.DirectContent

'Dim parrafo As iTextSharp.text.Paragraph
Dim Parrafo As String
Dim vNombre As String = "Carlos"
Dim vDespacho As String = "Penal"
Dim vPartes As String = "Pedro de Juan"
Dim vFecResLetras As String = "10/10/2010 :10:10"
Dim vNunico As String = "10-000001-0170-CA"
Dim pdestino As String = "0100"
Dim VMedio As String = "CASILLERO"

Do While Contador <= n

document.SetPageSize(PageSize.LETTER)
document.NewPage() ' // documento destino

Parrafo = Chr(13) & Chr(10) + "CÉDULA DE NOTIFICACIÓN" & Chr(13) & Chr(10) & Chr(13) & Chr(10) + _
"Notificando: " & vNombre + Chr(13) & Chr(10) + Chr(13) & Chr(10) & _
"Notifiqué mediante cédula, la resolución del " & vFecResLetras & " del " & vDespacho + Chr(13) & Chr(10) + Chr(13) & Chr(10) & _
"Expediente: " & vNunico + Chr(13) & Chr(10) + Chr(13) & Chr(10) & _
"Forma de Notificación: " + VMedio + Chr(13) & Chr(10) + Chr(13) & Chr(10) & _
"Dirección: " & pdestino & Chr(13) & Chr(10) + Chr(13) & Chr(10) & _
"Partes: " + Chr(13) & Chr(10) & vPartes + Chr(13) & Chr(10) + Chr(13) & Chr(10)
document.Add(New Paragraph(Parrafo))
document.Add(New Paragraph("1" + Chr(13) & Chr(10)))
document.Add(New Paragraph("2" + Chr(13) & Chr(10)))
document.Add(New Paragraph("3" + Chr(13) & Chr(10)))
document.Add(New Paragraph("4" + Chr(13) & Chr(10)))
document.Add(New Paragraph("5" + Chr(13) & Chr(10)))

Dim p As New Paragraph(New Chunk("Some Text here", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.RED)))
document.Add(p)

page = writer.GetImportedPage(reader, Contador)

'Read the imported page's rotation
rotation = reader.GetPageRotation(Contador)
'Then add the imported page to the PdfContentByte object as a template based on the page's rotation
If rotation = 90 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(Contador).Height)
ElseIf rotation = 270 Then
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(Contador).Width + 60, -30)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
End If

Contador += 1
Loop

document.Close()
' abrir documento final
System.Diagnostics.Process.Start("AcroRd32.exe", Cedula)

Return Nothing

End Function