段落和图片对齐怎么实现

在word中怎么样实现两个段落文字中间对齐,图片也和这两个段落中间对齐?
下面是示例文档,日期右缩进4个字符,要求上面的公司名还有印章和日期居中对齐,这应该怎么实现
temp.zip (18.4 KB)

@sijile

若要获取段落的对齐方式,可以使用ParagraphFormat.Alignment属性。

您可以使用ShapeBase.BehindText属性获取形状是在文本下方还是上方。 ShapeBase.WrapType用于定义形状是内嵌形状还是浮动形状。 对于浮动形状,定义环绕形状的文本的环绕模式。

希望这能回答您的查询。 如果仍然遇到问题,请与您的查询共享更多详细信息以及预期的输出。 然后,我们将根据您的要求回答您的查询。

你说的这是简单的情况,请看我发你的示例文档,下面是示意图
3.png (6.6 KB)

@sijile

不幸的是,您的要求不够明确。 您能否与您的查询共享更多详细信息以及预期的输出? 然后,我们将相应地回答您的查询。 谢谢您的合作。

They are in temp.zip and 3.png, did you see them?
Come on man, let’s figure out how to achieve it.

@sijile

请注意,Aspose.Words模仿MS Word的行为。 在您的temp.docx中,您已经设置了一些段落格式属性。 您想在第二段中添加四个字符的缩进,它可能不会使两个段居中。 例如。 如果第一个段落中有更多文本,则需要在第二个段落中添加更多的缩进字符。

为了满足您在3.png中共享的要求,您需要在文档中添加文本框(形状节点),并在其中添加两个段落。 使用Layout API获取文本框的位置,将红色圆圈形状添加到文档中并设置其位置。

以下.NET代码示例演示如何在文档中添加形状节点和Cernterlized段落。 我们使用了您的temp.docx。

如果您使用的是Java,请告知我们,然后我们将分享Java代码示例。 希望这对您有帮助。

请检查随附的输出文档的第二页。

output.zip (14.7 KB)

Document doc = new Document(MyDir + "temp.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Writeln();
builder.Writeln();
builder.Writeln();
builder.Writeln();
builder.Writeln();

builder.MoveToDocumentEnd();

Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true).Clone(true);

Shape textbox = new Shape(doc, ShapeType.TextBox);
textbox.AppendChild(new Paragraph(doc));
textbox.TextPath.TextPathAlignment = TextPathAlignment.Center;
textbox.Width = 250;
textbox.Height = 100;
textbox.HorizontalAlignment = HorizontalAlignment.Right;
builder.MoveTo(textbox.FirstChild);

builder.Font.Name = "仿宋";
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Writeln("深圳市测试有限公司司");
builder.ParagraphFormat.TabStops.Add(50, TabAlignment.Left, TabLeader.None);
builder.Writeln(@"2019年12月26日");

doc.Sections[1].Body.FirstParagraph.AppendChild(textbox);
builder.MoveToDocumentEnd();
builder.InsertNode(shape);

shape.BehindText = false;
shape.WrapType = WrapType.Square;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.Top = 65;
shape.Left = 340;

doc.Save(MyDir + "output.docx");

谢谢你的解答,但是不是我需要的。第二段4个字符的右缩进是固定的,我的解决办法是算出第二段内容的中心位置,然后再算出第一段的左缩进。页面设置好的,每一行是28个中文符,下面是第一行左缩进的示意图calcwidth.png (20.1 KB)。
我想知道aspose.words里是否有计算文字宽度的方法,日期中有数字和汉字,不好精确计算宽度。

@sijile

请注意,MS Word文档是流文档,文本位置可能会根据字体大小和名称而有所不同。 Word文档中也没有页面概念。 MS Word即时创建页面。

Aspose.Words没有提供API来计算您在图像中共享的两个形像的中心点。但是,使用Aspose.Words.Layout命名空间,您可以计算段落的宽度,然后在计算后将缩进量添加到第二个段落。但这不是完美的情况。您可以尝试使用MS Word创建相同的内容(在图像中共享)。

对于您的情况,我们建议您两种解决方案。

解决方案1:

  1. 按照我之前的文章中的建议创建一个Shape节点
  2. 在其中添加段落并将段落饮食设置为中心。此选项将根据您的要求将第二段的正确性居中。
  3. 根据需要在“形状”上设置位置。

解决方案2:

  1. 创建一个包含两个单元格和一行的表。根据您的要求设置桌子的宽度。
  2. 根据需要设置单元的宽度。
  3. 在第二个单元格中添加两个段落。
  4. 将段落的对齐方式设置为cetner。
  5. 删除表格边框。

如果仍然遇到问题,请使用Microsoft Word手动创建所需的Word文档并将其附加在此处以供我们参考。 我们将调查您希望如何生成最终的Word输出。 然后,我们将为您提供更多信息以及代码。

Hi, in my project the word has to satisfy two conditions:

  1. The date paragraph must right indent 4 unicode character, and is setted in the template.
  2. The company paragraph is front the date paragraph, and aligned to the center of the date paragraph

Your solutions don’t meet the first condition.

The pagesetting is that every row can has 28 unicode character, and all paragraphs have the same font and fontsize setting. So I can calculate the center of the dare paragraph, and then the leftindent of the company paragraph. The problem is that the date paragraph has number and unicode character, the width can not be calculated accurate. I don’t know how aspose calculate the width of character, I get it by the date paragraph’s rightindent /8.

These are what I want and what I do.

Thanks

@sijile

You can achieve your requirement by different ways that’s why we shared two solutions. If you want code example for shared solution, please let us know. We will write the code and share it with you. We have manually created MS Word document for your case. Please check the attached temp.docx. temp.zip (22.8 KB)

If you want your own approach to get the center of both paragraphs and you can calculate the width of first paragraph and facing issue with the width calculation of second paragraph, we suggest you please use following code example to get the width of paragraph. In this case, you need to bookmark the second paragraph. Please check the attached temp.docx

Document doc = new Document(MyDir + "temp.docx");
Bookmark bm = doc.Range.Bookmarks["bookmark"];

LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

layoutEnumerator.Current = layoutCollector.GetEntity(bm.BookmarkStart);
double position1 = layoutEnumerator.Rectangle.Left;
layoutEnumerator.Current = layoutCollector.GetEntity(bm.BookmarkEnd);
double position2 = layoutEnumerator.Rectangle.Left;

Console.WriteLine(position2 - position1);

You can also calculate the width of first paragraph using the same approach.

As shared in my old posts, MS Word is flow document. Setting indent (e.g. 4 character with) will not center the paragraphs as shared in 3.png. However, by calculating the width of paragraphs you can achieve your requirement, you can use this approach.

I use your code, and it calculate the width accurate.
Thanks!