We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose Word Image Position change dynamically in C# code

Hi Team, Greetings !!!
We have a requirement where a signature which is always at the last page of the document needs to change it’s position dynamically through C# code. We would kindly need your technical assistance on how to achieve this programmatically. We will be attaching a sample template, kindly share us your technical guidance on how to achieve this code.
Positions that we need would be

  • Bottom Left
  • Bottom Center
  • Bottom Right

Signature - SampleFile.docx (17.4 KB)

To be noted. Template will always have the Signature image in it by default. we just need to change it’s position dynamically.

@Karthik_Test_account The signature image in your sample document is Inline, so to check it’s position you can simply change alignment of the paragraph with this image. For example see the following code:

Document doc = new Document(@"C:\Temp\Signature - SampleFile.docx");

// Get the signature shape. In the sample document it is the only shape so get it as firs shape.
Shape signature = (Shape)doc.GetChild(NodeType.Shape, 0, true);

// Get the paragraph with the shape.
Paragraph para = (Paragraph)signature.GetAncestor(NodeType.Paragraph);

// Change alignment.
para.ParagraphFormat.Alignment = ParagraphAlignment.Left;
doc.Save(@"C:\Temp\out_left.docx");
para.ParagraphFormat.Alignment = ParagraphAlignment.Right;
doc.Save(@"C:\Temp\out_right.docx");
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
doc.Save(@"C:\Temp\out_center.docx");

@alexey.noskov

This code works for a plain template. But for our actual template it doesn’t work I think we are inserting page number using shape, doubt that is conflicting. Can you please use this template and give a logic that works for this one.

Sample Tempalte with Content and Signature.docx (48.0 KB)

Here No of pages will differ every time but the Signature will always be at the last page of the template.

@Karthik_Test_account You can modify the code like the following:

Document doc = new Document(@"C:\Temp\Sample Tempalte with Content and Signature.docx");
            
// Get the signature shape. In the sample document it is the only shape so get it as firs shape.
NodeCollection shapes = doc.LastSection.Body.GetChildNodes(NodeType.Shape, true);
Node signature = shapes[shapes.Count - 1];

// Get the paragraph with the shape.
Paragraph para = (Paragraph)signature.GetAncestor(NodeType.Paragraph);

// Change alignment.
para.ParagraphFormat.Alignment = ParagraphAlignment.Left;
doc.Save(@"C:\Temp\out_left.docx");
para.ParagraphFormat.Alignment = ParagraphAlignment.Right;
doc.Save(@"C:\Temp\out_right.docx");
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
doc.Save(@"C:\Temp\out_center.docx");

But if you have a control over the template, you can mark the signature shape with a bookmark, this will avoid possible mistakes when get the signature shape.