I'm adding an image to a 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 is working, after a fashion - the image is indeed added to the PDF file:
[
However, I need the image (the "Office Use Only" rectangle) to magnetize to the right, on the same "row" as the varicolored text shown to the left, like so:
[
How can I do that? Setting "ScaleToFitLineWhenOverflow" to true didn't help. I also reduced the size of the image itself, but that made no difference - it just took the smaller file and ballooned it back out to the same (screen) size as it was previously.
UPDATE
Once I remembered to actually load the smaller image, too (rather than just checking for its existence), the image has been punified:
...but it still tucks itself under, instead of 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 does not display at all. What the hammer? What the chain?
UPDATE 3
Okay, this sort of works (still needs 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 no stinkin' paragraphs!)
for life's not a paragraph
And death i think is no parenthesis
UPDATE 4
With a little code-tweaking (reverted to the original, fullsize image, and changed the WidthPercentage of the table from 35 to 45), I now see this in my generated PDF:
...so how do I pull the img up by its pre-Twitter bootstraps to align better with the text to the left? Do I have to wrap both tables in yet another, two-celled table?
- I reckon it might make more sense, on reflection, to just use one table, and make it a two column/cell table (rather than wrap two single-celled amoebas, I mean tables, into yet another table).
UPDATE 5
One step closer with the latest 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 puny and squarshes the text in the table's first cell:
...maybe I need to add an empty cell or something...