Foros del Web » Programación para mayores de 30 ;) » .NET »

Ayuda con Librería iTextSharp

Estas en el tema de Ayuda con Librería iTextSharp en el foro de .NET en Foros del Web. Mediante iTextSharp estoy generando un documento en pdf y creo una columna, asigno(addtext) y muestro el texto mediante el metodo .GO El detalle es que ...
  #1 (permalink)  
Antiguo 14/12/2010, 18:41
 
Fecha de Ingreso: diciembre-2010
Mensajes: 6
Antigüedad: 13 años, 4 meses
Puntos: 0
Pregunta Ayuda con Librería iTextSharp

Mediante iTextSharp estoy generando un documento en pdf y creo una columna, asigno(addtext) y muestro el texto mediante el metodo .GO

El detalle es que si mas adelante muestro mas texto mediante el metodo ShowTextAligned no lo muestra, no marca error de ningun tipo solo no lo muestra

Alguien habra pasado por lo mismo y me pudiera orientar? Voy iniciando en el uso de iTextSharp y por mas que busque no he encontrado la solucion espero averme sabido explicar correctamente ya que es la primera ves que uso un foro ;S

Saludos y Gracias de antemano =)
  #2 (permalink)  
Antiguo 14/12/2010, 19:21
 
Fecha de Ingreso: diciembre-2010
Mensajes: 32
Antigüedad: 13 años, 4 meses
Puntos: 0
Respuesta: Ayuda con Librería iTextSharp

Aqui hay un ejemplo en el cual usan esta libreria, la verdad que he usado itextsharp pero no me habia topado con el metodo mencionado (ShowTextAligned), lo he usado para hacer tablas en pdf y html mas que todo.

---------------------------------------------------

using System;

using System.IO;



using iTextSharp.text;

using iTextSharp.text.pdf;





namespace iTextSharp.tutorial.Chap01

{


public class Chap0112

{

public Chap0112()

{

Console.WriteLine("Chapter 1 example 12: reading an existing PDF file");



try

{

// we create a reader for a certain document

PdfReader reader = new PdfReader("Chap0703.pdf");

// we retrieve the total number of pages

int n = reader.NumberOfPages;

// we retrieve the size of the first page

Rectangle psize = reader.GetPageSize(1);

float width = psize.Width;

float height = psize.Height;



// step 1: creation of a document-object

Document document = new Document(psize, 50, 50, 50, 50);

// step 2: we create a writer that listens to the document

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0112.pdf", FileMode.Create));

// step 3: we open the document

try

{

Watermark watermark = new Watermark(Image.GetInstance("watermark.jpg"), 200, 320);

document.Add(watermark);

}

catch(Exception e)

{

Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");

}

document.Open();

// step 4: we add content

PdfContentByte cb = writer.DirectContent;

int i = 0;

int p = 0;

Console.WriteLine("There are " + n + " pages in the document.");

while (i < n)

{

document.NewPage();

p++;

i++;

PdfImportedPage page1 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);

Console.WriteLine("processed page " + i);

if (i < n)

{

i++;

PdfImportedPage page2 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2);

Console.WriteLine("processed page " + i);

}

if (i < n)

{

i++;

PdfImportedPage page3 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0);

Console.WriteLine("processed page " + i);

}

if (i < n)

{

i++;

PdfImportedPage page4 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0);

Console.WriteLine("processed page " + i);

}

cb.SetRGBColorStroke(255, 0, 0);

cb.MoveTo(0, height / 2);

cb.LineTo(width, height / 2);

cb.Stroke();

cb.MoveTo(width / 2, height);

cb.LineTo(width / 2, 0);

cb.Stroke();

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

cb.BeginText();

cb.SetFontAndSize(bf, 14);

cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0? 1 : 0)), width / 2, 40, 0);

cb.EndText();

}

// step 5: we close the document

document.Close();

}

catch (Exception de)

{

Console.Error.WriteLine(de.Message);

Console.Error.WriteLine(de.StackTrace);

}



}

}

}
  #3 (permalink)  
Antiguo 14/12/2010, 22:27
 
Fecha de Ingreso: noviembre-2002
Ubicación: DF
Mensajes: 1.056
Antigüedad: 21 años, 5 meses
Puntos: 37
Respuesta: Ayuda con Librería iTextSharp

Quiza si posteas parte de tu codigo se pueda apreciar el error.

En mi caso, genere la plantilla PDF con campos editables de esa manera es muy sencillo modificar contenidos.
La plantilla la genere con IBM Lotus Symphony, que es gratuito y bastante simple.

Luego, mediante:
Dim objReader As New PdfReader("D:\Plantillaprueba.pdf")
Dim nombreArchivoPDF As String = "miarchivoresultado.pdf"
Dim objStamper As New PdfStamper(objReader, New FileStream(nombreArchivoPDF, FileMode.Create))


Dim objFields As AcroFields = objStamper.AcroFields

objFields.SetField("campo1", miCampo1)
objFields.SetField("campo2", miCampo2)
objFields.SetField("campo3", miCampo3)

se pueden modificar los contenidos.
El resto que necesito lo agrego mediante tablas
  #4 (permalink)  
Antiguo 15/12/2010, 08:25
 
Fecha de Ingreso: diciembre-2010
Mensajes: 6
Antigüedad: 13 años, 4 meses
Puntos: 0
Respuesta: Ayuda con Librería iTextSharp

Tsugoi, Gracias por tu ejemplo lo revisare =)

Lo unico es que antes de que cierras el documento(step 5) estas usando ShowTextAligned, a lo mejor yo me estare equivocando y ese metodo no se usa para lo que yo creia ;S

cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0? 1 : 0)), width / 2, 40, 0);
  #5 (permalink)  
Antiguo 15/12/2010, 08:40
 
Fecha de Ingreso: diciembre-2010
Mensajes: 6
Antigüedad: 13 años, 4 meses
Puntos: 0
Respuesta: Ayuda con Librería iTextSharp

wwwmaster, gracias por el consejo =)


la parte del codigo es la siguiente:


If Len(descripcion) > 77 Then
'Agrego texto mediante ShowTextAligned
Dim columnConcepto As ColumnText = New ColumnText(cb)
columnConcepto.SetSimpleColumn(PosX + 174, PosSigY, 460, 0)
Dim pConcepto As Paragraph = New Paragraph(0, descripcion, myFontConcepto)
columnConcepto.AddText(pConcepto)
columnConcepto.Go()
Else
'Agrego texto mediante ShowTextAligned
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, descripcion, PosX + 174, PosSigY, 0)
End If


Valido si la descripcion cuenta con mas de 77 caracteres ya que si cuenta con mas de tendre que poner el texto en 2 renglones, para eso uso ColumnText en lugar de ShowTextAligned
Yo le paso x num. de descripciones el detalle esta en que el momento que entra a crear una columna y luego entra a ShowTextAligned el texto que agrege mediante ShowTextAligned ya no lo muestra ;S

Etiquetas: itextsharp
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:06.