1

Tengo el siguiente problema, creo un formulario de un pdf con Adobe Acrobat, en el cual instanció las variables y las relacion

    private void FillForm()
    {
    string pdfTemplate = @"D:PlantillaFT.pdf";
    FormatoGenerado = @"D:FichaTecnicaGenerada.pdf";

        PdfReader pdfReader = new PdfReader(pdfTemplate);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                    FormatoGenerado, FileMode.Create));

        AcroFields pdfFormFields = pdfStamper.AcroFields;


        // Asigna los campos
        pdfStamper.AcroFields.ReplacePushbuttonField
        pdfFormFields.SetField("FOTO", Foto.Image; //!!!!Aqui es mi duda!!!!
        pdfFormFields.SetField("FICHA", txtFicha.Text);
        pdfFormFields.SetField("NOMBRES", txtNombres.Text);
        pdfFormFields.SetField("APELLIDOS", txtApellidos.Text);
        pdfFormFields.SetField("RFC", txtRFC.Text);
        pdfFormFields.SetField("CURP", txtCurp.Text);
        pdfFormFields.SetField("TELEFONO", txtTelefono.Text);
        pdfFormFields.SetField("EMAIL", txtEmail.Text);
        pdfFormFields.SetField("NACIMIENTO", FechaNacimiento.Text);
        pdfFormFields.SetField("DIRECCION", txtDireccion.Text);
        pdfFormFields.SetField("AVISARA", txtAvisarA.Text);
        pdfFormFields.SetField("TELAVISO", txtNumContacto.Text);
        pdfFormFields.SetField("TIPOSANGRE", cmbTipodeSangre.Text);
        pdfFormFields.SetField("SITCONTRACTUAL", cmbSitCotractual.Text);
        pdfFormFields.SetField("VACACIONES", Vacaciones.Text);
        pdfFormFields.SetField("NIVELACTUAL", txtNivelAct.Text);
        pdfFormFields.SetField("CATACTUAL", txtCategoriaActual.Text);


        string sTmp = "Datos asignados";
        MessageBox.Show(sTmp, "Terminado");

        // Cambia la propiedad para que no se pueda editar el PDF
        pdfStamper.FormFlattening = false;

        // Cierra el PDF
        pdfStamper.Close();
    }

Ahora mi problema es para la imagen como la instancio o la paso al pdf de una forma similar? introducir la descripción de la imagen aquí

3 respuestas 3

1

Yo tengo un proyecto donde lo hago de esta forma:

PdfPTable encabeval = new PdfPTable(3);
            encabeval.WidthPercentage = 100;

            encabeval.AddCell(imgizq);

PdfPCell imgizq = null;
                // Creamos la imagen y le ajustamos el tamaño
                using (var imageStream = new FileStream("ruta", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    iTextSharp.text.Image imagen = iTextSharp.text.Image.GetInstance(imageStream);
                    imagen.BorderWidth = 0;
                    imagen.Alignment = Element.ALIGN_LEFT;
                    float percentage = 0.0f;
                    percentage = 70 / imagen.Width;
                    imagen.ScalePercent(percentage * 100);

                    imgizq = new PdfPCell(imagen);
                }
                imgizq.BorderWidth = 0;
                //imgizq.Rowspan = 4; por si debe ocupar mas de una fila

encabeval.AddCell(imgizq);

PdfPCell codigo = new PdfPCell(new Phrase("CÓDIGO:", _standardFont7));
                codigo.BorderWidth = 0.75f;
                codigo.BorderWidthBottom = 0.75f;
                encabeval.AddCell(codigo);

PdfPCell version = new PdfPCell(new Phrase("VERSIÓN:", _standardFont7));
            version.BorderWidth = 0.75f;
            version.BorderWidthBottom = 0.75f;
            encabeval.AddCell(version);

doc.Add(encabeval);

Pero la imagen lo obtengo desde alguna ruta.

0

En esta entrada dan un par de posibles respuestas. Aunque hace ya tiempo que trabajé con itextsharp, me inclino por la siguiente (me suena más):

// GetFieldPositions returns an array of field positions if you are using 5.0 or greater
// This line does a lot and should really be broken up for null-checking
iTextSharp.text.Rectangle rect = 
              pdfStamper.AcroFields.GetFieldPositions("FOTO")[0].position;

// Create an image
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(TestImage);

// Scale it
img.ScaleAbsolute(rect.Width, rect.Height);

// Position it
img.SetAbsolutePosition(rect.Left, rect.Bottom);

// Add it to page 1 of the document
pdfStamper.GetOverContent(1).AddImage(img);

Prueba y nos comentas que tal ;)

1
0

Proba convirtiendo la imagen en base 64

byte[] imageArray = System.IO.File.ReadAllBytes("la url de la imagen");
string img = Convert.ToBase64String(imageArray);
pdfFormFields.SetField("FOTO", img);

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.