【问题标题】:Align Image inside cell to the right doesn't work将单元格内的图像向右对齐不起作用
【发布时间】:2015-10-23 15:07:23
【问题描述】:

我需要在标题上将图像向右对齐。这是我的代码:

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;

但它总是在左边,请帮忙?

【问题讨论】:

  • eRow 来自哪里?也许您甚至没有将图像添加到标题表中。在您的代码 sn-p 中,您不会将任何行添加到您在标题中创建的表中。

标签: pdfsharp migradoc


【解决方案1】:

我知道有点晚了 但是您必须将图像包装在一个段落中。 http://forum.pdfsharp.net/viewtopic.php?f=2&t=158

类似的东西(我使用你的部分代码)

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);

我在使用上面的代码/链接时遇到了同样的问题。

【讨论】:

  • 为什么投反对票?请解释。我确实提供了一个适合我的解决方案。
  • OP 不再对这个问题的答案感兴趣。他们已经写道,添加一个段落没有帮助(可能是他们的代码中的一个新错误)。恕我直言,这个问题应该被删除,但是关闭队列太长,这个问题没有得到足够的重视。
【解决方案2】:

线条

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

从逻辑上将图像从单元格中取出。这会创建一个可以出现在页面上任何位置的图像,而不仅仅是单元格内。

如果您只是删除这五行,也许它会起作用。如果您将Paragraph 添加到Cell 并将Image 添加到单元格,也许它会起作用。

如果您提供了 MCVE,如果我的建议有效,我会尝试的。但是你只显示一个代码sn-p。

image.LockAspectRatio = false; 这行是多余的,但没有坏处。

【讨论】:

  • 我删除了 5 行,但仍然无法正常工作,我也尝试添加一个段落,然后将其添加到单元格中,但我收到一个错误,提示必须克隆值... .
  • eRow 来自哪里?您没有显示导致“克隆”错误的代码(您的更改一定有问题)。另见:stackoverflow.com/help/mcve
  • 我最终使用了另一种策略,因为它对我不起作用。谢谢
【解决方案3】:

如果你这样做,它会起作用:

row.Cells[1].AddParagraph().AddImage(fileName);
row.Cells[1].Format.Alignment = ParagraphAlignment.Right;

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2012-11-12
    • 2014-09-27
    • 2012-04-06
    • 2023-03-16
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多