You have to play with the positions of the image, based on the Cartesian plane:
Starting at position x=0, y=0
(bottom left corner of page)
I enclose the lines of code in the example:
//OBTENGO LA IMAGEN desde archivo
public static iTextSharp.text.Image img = Image.GetInstance("Logo.png");
private void CrearPDFConImagen()
{
try
{
Document Doc = new Document(PageSize.LETTER, 10f, 10f, 10f, 0f);//Horizontal
//Document Doc = new Document(PageSize.LETTER, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(Doc
, new System.IO.FileStream(
System.IO.Directory.GetCurrentDirectory()
+ "\EjemploImagen" + Guid.NewGuid() + ".pdf",
System.IO.FileMode.Create));
Doc.Open();
// Le colocamos el título y el autor
// **Nota: Esto no será visible en el documento
Doc.AddTitle("Reporte de ejemplo Con imagen");
Doc.AddCreator("Cristina Carrasco - [email protected]");
//var logo = iTextSharp.text.Image.GetInstance("Logo.png");
var parrafo2 = new Paragraph(" Titulo del PDF");
parrafo2.SpacingBefore = 200;
parrafo2.SpacingAfter = 0;
parrafo2.Alignment = 1; //0-Left, 1 middle,2 Right
Doc.Add(parrafo2);
Doc.Add(Chunk.NEWLINE);
img.ScaleToFit(125f, 60F);
//Imagen - Esquina inferior izquierda
img.SetAbsolutePosition(0, 0);
Doc.Add(img);
//Imagen - Movio en el eje de las X
img.SetAbsolutePosition(200, 0);
Doc.Add(img);
//Imagen - Movio en el eje de las Y
img.SetAbsolutePosition(0, 200);
Doc.Add(img);
//Imagen - Movio en el eje de las Y
img.SetAbsolutePosition(0, 750);
Doc.Add(img);
//Imagen - Movio en el eje de las Y
//Esta imagen es la que esta centrada a un lado del titulo
img.SetAbsolutePosition(150, 750);
Doc.Add(img);
Doc.Close();
}
catch (Exception ex)
{
throw new Exception(ex.ToString(), ex);
}
}
In this line:
var parrafo2 = new Paragraph(" Titulo del PDF");
Add some spaces at the beginning so that the image looks more or less centered.
I hope you find it useful, greetings.
Updates:
Annex source code: Click to download
2016-Jan-10
I'll explain more in detail.
The version of itextsharp
I use is 5.5.10.0, you should validate that.
I add the references in the file as shown in the green box.
And when creating the variable img
, what I do is create an instance of iTextSharp.text.mage
and I send the name of the file Logo.png
to the constructor, which must be added to the project, as shown in the box red from the previous image.
Another important thing about the image is that it must exist in the application folder so I did the following:
Select the option Copy to Output Directory = Copy always
in the properties box (right click on the file Logo.png
and then click on the option propiedades
of the contextual menu in the section Explorador de soluciones
of Visual Studio
), you can do this or copy the file directly to the folder where the application runs.
This is the image that I have added to the project:
When executing the application, it is copied to the folder of .exe
(in my case it is a Windows application):
Maybe that's the detail you need. When the image does not exist, the system looks for it and marks error ( Can not find the file ):