I am using .net and I want to insert a shape in all pages that have an orientation of landscape but my shape should have special formatting as attached in the zip file
Thanks a lot for helpingAspose.zip (93.7 KB)
To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:
- Your simplified input Word document
- Your expected DOCX file showing the desired output. You can create this document by using MS Word.
As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.
@awais.hafeez Please find attached the input document as well as the output doc that has a Textbox inserted in all landscape pages with special size and position same as attached in the original post
Thanks a lot for your help
Documents.zip (36.2 KB)
Any update about how we can achieve this
Thanks for your efforts
You can build logic on the following code to get the desired output:
Document doc = new Document("E:\\Temp\\Documents\\Input.docx");
Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
Run run = (Run)runs[i];
int length = run.Text.Length;
Run currentNode = run;
for (int x = 1; x < length; x++)
{
currentNode = SplitRun(currentNode, 1);
}
}
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 1;
foreach (Run run in smallRuns)
{
if (collector.GetStartPageIndex(run) == pageIndex)
{
if (run.ParentParagraph.ParentSection.PageSetup.Orientation == Aspose.Words.Orientation.Landscape)
{
Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextBox);
watermark.IsLayoutInCell = false;
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Margin;
watermark.Left = 10.87 * 72;
watermark.Top = 0;
watermark.Width = 0.37 * 72;
watermark.Height = 6.69 * 72;
watermark.TextPath.Text = "w";
watermark.TextPath.FontFamily = "Arial";
watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
watermark.WrapType = WrapType.None;
builder.MoveTo(run);
builder.InsertNode(watermark);
}
pageIndex++;
}
}
doc.JoinRunsWithSameFormatting();
doc.Save("E:\\Temp\\Documents\\20.5.docx");
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
Hope, this helps.
A post was split to a new topic: Insert Shape in Header Footer of Landscape Pages in Word Document C# .NET