How can I make this iTextSharp image "float to the right"?
I add image to PDF file like so:
string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImgSmaller.png"))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImg.png");
png.ScaleToFitLineWhenOverflow = true;
doc.Add(png);
}
It works, after mod - the image is indeed added to the PDF file:
[
However, I need an image ("Office Use Only" rectangle) to magnetize to the right, on the same "line" as the varicolored text shown on the left, for example:
[
How can i do this? Setting "ScaleToFitLineWhenOverflow" to true didn't help. I also reduced the size of the image itself, but that didn't matter - it just took the smaller file and pushed it back to the same (screen) size as before.
UPDATE
As soon as I remembered that I actually uploaded a smaller image (instead of just checking for its existence), the image was punished:
... but it still hides itself and not to the right of the blocks of text.
UPDATE 2
I tried to implement nelek's idea:
string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImgSmaller.png"))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImgSmaller.png");
png.ScaleToFitLineWhenOverflow = true; // this doesn't do what I hoped it would do (keep the img on the same line)
PdfPTable tblImg = new PdfPTable(1);
tblImg.WidthPercentage = 35;
tblImg.HorizontalAlignment = Element.ALIGN_RIGHT;
var parImg = new Paragraph();
parImg.Add(png);
PdfPCell imgCell = new PdfPCell();
imgCell.AddElement(parImg);
imgCell.BorderWidth = PdfPCell.NO_BORDER;
tblImg.AddCell(imgCell);
doc.Add(tblImg);
}
... but now the image is not showing at all. What hammer? What is the chain?
UPDATE 3
Ok, works like this (still need tweaking):
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImgSmaller.png"))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImgSmaller.png");
PdfPTable tblImg = new PdfPTable(1);
tblImg.WidthPercentage = 35;
tblImg.HorizontalAlignment = Element.ALIGN_RIGHT;
PdfPCell imgCell = new PdfPCell();
imgCell.AddElement(png); //parImg);
imgCell.BorderWidth = PdfPCell.NO_BORDER;
tblImg.AddCell(imgCell);
doc.Add(tblImg);
}
(we don't need stinkin paragraphs!)
for life not a paragraph
And death i think is no parenthesis
UPDATE 4
With a little tweaking of the code (going back to the original, full size image and changing the WidthPercentage of the table from 35 to 45), I can now see this in my generated PDF:
... so how can I pull the img up its boot boot tracks to better align text with text to the left? Do I need to wrap both tables in another table with two cells?
- I believe that when parsing, it might make sense to just use one table and make it a table with two columns / cells (instead of transferring two single cell amoebas, I mean tables, to another table).
UPDATE 5
One step closer to the last code:
PdfPTable tbl = new PdfPTable(2);
tbl.WidthPercentage = 55;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
par.SetLeading(0, 1.2f);
par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell();
chunky.AddElement(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);
string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImg.png"))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImg.png");
PdfPCell imgCell = new PdfPCell();
imgCell.AddElement(png);
imgCell.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(imgCell);
doc.Add(tbl);
}
... but now the image is fuzzy and breaks the text in the first cell of the table:
... maybe I need to add a blank cell or something ...
source to share
The way to do this is basically the code in update 5 (create a table with two cells / rows and put the image in a second storage tank), but with the percentage changed from 55 to 100 (which is probably the default and therefore overkill).
PdfPTable tbl = new PdfPTable(2);
tbl.WidthPercentage = 100;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
par.SetLeading(0, 1.2f);
par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell();
chunky.AddElement(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);
string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImg.png"))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImg.png");
PdfPCell imgCell = new PdfPCell();
imgCell.AddElement(png);
imgCell.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(imgCell);
doc.Add(tbl);
}
With this code, the image is rendered pretty much as intended:
NOTE. The working code is based on an online example I found here .
source to share