How to place an image in an existing pdf with Itexsharp?

1

I have the following problem, I create a pdf form with Adobe Acrobat, in which you specified the variables and the relations

    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();
    }

Now my problem is for the image as the instancio or the step to the pdf in a similar way?

    
asked by Israel Ramirez Sanchez 26.08.2017 в 07:44
source

3 answers

1

I have a project where I do it this way:

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);

But the image I get from some route.

    
answered by 12.09.2018 в 21:21
0

In this entry give a couple of possible answers. Although I have worked with itextsharp for a long time, I prefer the following one (sounds more to me):

// 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);

Try and tell us how;)

    
answered by 26.08.2017 в 10:10
0

Probably converting the image to base 64

byte[] imageArray = System.IO.File.ReadAllBytes("la url de la imagen");
string img = Convert.ToBase64String(imageArray);
pdfFormFields.SetField("FOTO", img);
    
answered by 15.01.2019 в 01:14