-1

I need to align on the headers an image to the right. This is my code:

Section section = document.AddSection();
table = section.Headers.Primary.AddTable();

var column = table.AddColumn("5cm");
column.Format.Alignment = ParagraphAlignment.Left;

column = table.AddColumn("8cm");
column.Format.Alignment = ParagraphAlignment.Left;

column = table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Right;

eRow.Cells[0].AddParagraph("Some text");
eRow.Cells[1].AddParagraph("Some text");

eRow.Cells[2].Format.Alignment = ParagraphAlignment.Right;

image = eRow.Cells[2].Elements.AddImage(imagePath);

image.LockAspectRatio = false;
image.Width = Unit.FromInch(0.16);
image.Height = Unit.FromInch(0.09);

image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;

But it's always to the left, any help please?

cmonti
  • 187
  • 1
  • 12

3 Answers3

10

I know it is a bit late But you have to wrap your image in a paragraph. http://forum.pdfsharp.net/viewtopic.php?f=2&t=158

Something like (I use parts of your code)

Section section = document.AddSection();
table = section.Headers.Primary.AddTable();

var column = table.AddColumn("5cm");

//I assume eRow is just a row added to your table
var eRow = table.AddRow();
var paragraph = eRow.Cells[0].AddParagraph();

//set the alignment on the paragraph object
paragraph.Format.Alignment = ParagraphAlignment.Right;
paragraph.AddImage(imagePath);

I had the same problem at it worked with the code/link from above.

Arikael
  • 1,989
  • 1
  • 23
  • 60
  • 1
    Why the downvote? Please explain. I did provide a solution which worked in my case. – Arikael Nov 22 '15 at 21:07
  • The OP is no longer interested in answers to this question. They already wrote that adding a paragraph didn't help (probably a new error in their code). IMHO the question should be deleted, but the close queue is too long and this question does not get enough attention. – I liked the old Stack Overflow Dec 08 '15 at 19:05
0

The lines

image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;

logically take the image out of the cell. This creates an image that can appear anywhere on the page, not just inside the cell.

Maybe it works if you simply remove those five lines. Maybe it works if you add a Paragraph to the Cell and add the Image to the cell.

If you would have supplied an MCVE I would have tried if my suggestions work. But you only show a code snippet.

The line image.LockAspectRatio = false; is superfluous, but does no harm.

  • I remove the 5 lines, but still is not working, I tried as well adding a paragraph and then add it to the cell but I get an error that says that value has to be cloned.... – cmonti Oct 23 '15 at 16:30
  • Where is eRow coming from? You don't show the code that leads to the "cloned" error (there must be something wrong with your changes). See also: http://stackoverflow.com/help/mcve – I liked the old Stack Overflow Oct 23 '15 at 16:56
  • I end up using another strategy because it didn't work for me. thanks – cmonti Oct 23 '15 at 18:17
-1

It will work if you do like that:

row.Cells[1].AddParagraph().AddImage(fileName);
row.Cells[1].Format.Alignment = ParagraphAlignment.Right;
Fahad
  • 27
  • 2
  • 8