I created a PDF document with images, I am trying to add text under each image keep in mind that the template for the page is different depending on how many images the user wants on the page. My problem is I am having problems adding and position the text.
Code for adding images:
int count = 0;
imageStartX = (docSize.Width / 100) * marginSizeProcent;
float imageMaxHeight = 0;
float imageMaxWidth = 0;
iTextSharp.text.Image image = null;
switch (pageLayout)
{
case PageLayoutEnum.SingleImage:
imageMaxWidth = docSize.Width - ((docSize.Width/100) * (2 * (float)marginSizeProcent));
imageMaxHeight = imageStartY - ((docSize.Width/100) * (float)marginSizeProcent);
foreach (PDFObject o in pdfObjects)
{
if (count > 0)
AddPageWithHeader(false);
image = iTextSharp.text.Image.GetInstance(o.File);
image.ScaleToFit(imageMaxWidth, imageMaxHeight);
image.SetAbsolutePosition(imageStartX + (imageMaxWidth - image.ScaledWidth) / 2, imageStartY - image.ScaledHeight - (imageMaxHeight - image.ScaledHeight) / 2);
image.Border = Rectangle.BOX;
image.BorderWidth = 2f;
image.BorderColor = BaseColor.DARK_GRAY;
document.Add(image);
count++;
}
break;
case PageLayoutEnum.TwoImages:
code for adding text:
MemoryStream memoryStream = new MemoryStream();
PdfReader pdfReader = new PdfReader(documentStream.ToArray());
PdfStamper stamper = new PdfStamper(pdfReader, memoryStream);
PdfContentByte contentbyte = stamper.GetUnderContent(1);
ColumnText dispalyIdText = new ColumnText(contentbyte);
Paragraph idText;
int counter = 0;
switch (pageLayout)
{
case PageLayoutEnum.SingleImage:
foreach (PDFObject item in pdfObjects)
{
dispalyIdText.SetSimpleColumn(200, 200, 200, 200, 200, Element.ALIGN_LEFT);
idText = new Paragraph(new Chunk(item.DisplayId, FontFactory.GetFont("Arial", 20, Font.BOLD, BaseColor.RED)));
dispalyIdText.AddElement(idText);
}
break;
case PageLayoutEnum.TwoImages:
You don’t say what your actual problems are, only that you’re having them.
If I were to guess, one of your problems is that text isn’t actually being displayed on your PDFs. There are three distinct reasons for this. The first is this line:
The first four parameters to this method are the coordinates of the rectangle that you want to constrain your drawing to. The first parameter is the lower left
x, the second is the lower lefty, the third is the upper rightxand the fourth is the upper righty. In your code you are saying to bind your text to a rectangle with lower left coordinates of200,200and upper right coordinates of200,200. This means your rectangle has zero width and height. To fix this you need to give a rectangle that actually works. In a PDF, the lower left corner is0,0so to draw text in a rectangle in the lower left corner that’s 20 pixels (not actually pixels but that’s another story) high and 200 wide you’d do:Your second problem is that you are setting the
leading(line-height) to 200. Depending on the object you are creating this may or may not blow text way out. You should set this to something more sane, possibly the font’s height. This doesn’t affectAddElementbut it does affectSetText.The last problem is that when using
ColumnTextyou are now in “text” mode and have to tell the system when you are ready to start processing. You do this by issuing theGo()command: