3

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:

[enter image description here

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:

[enter image description here

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:

enter image description here

...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:

enter image description here

...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:

enter image description here

...maybe I need to add an empty cell or something...

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

3

The way to do it is basically the code in Update 5 (make a table with two cells/rows, and put the image in the second holding tank), but with the percentage changed from 55 to 100 (which is probably the default, and thus redundant).

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 displays pretty much as envisioned:

enter image description here

NOTE: The working code is based on an online example I found here.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    @BClayShannon... hi again... be aware about `PdfPTable` `WidthPercentage`, by default isn't 100% but 80%... if You need to define each column `width` You can do on this way (it's vb, sorry; for example with 2 columns) : `Dim cp() As Single = {50, 50} : tbl.SetWidths(cp)` ... no bothering You anymore. – nelek Jul 24 '15 at 17:39
  • 1
    Thanks, that saved me from commenting out the what-I-reckoned-was-redundant percentage assignment and the resulting autoyanking of follicles! – B. Clay Shannon-B. Crow Raven Jul 24 '15 at 17:40