How to access last page of Word document?

Hi,

I was searching the forums for clues how to access the very last page of the Word document in order to add something there.
First iteration was trying to work with headers and footers, but Word seems to have some limitations, nor supports directly accessing last page of the document.

I found that old posting, but can’t see if there was actual solution. Footer in the LAST PAGE of a Dynamic Document alone

First I’m creating a textbox shape with a helper method:
Aspose.Words.Drawing.Shape textboxShape = CreateStampTextBox( document, alignment, stamp );

I’ve tried to add it to last page using various methods, but I don’t seem to get it right.
For example, document.LastSection.Body.LastParagraph.AppendChild( textboxShape ); doesn’t seem to get it right.

Any help would be greatly appreciated.

@Matti_Ketonen,

Thanks for your inquiry. Please move the cursor to the end of document using DocumentBuilder.MoveToDocumentEnd method and insert the shape node using DocumentBuilder.InsertNode method.

You can also insert the shape at the end of document using Document.LastSection.Body.LastParagraph.AppendChild( textboxShape ).

Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

Best Regards,
Tahir Manzoor

Thanks @tahir.manzoor for the reply!

I have tried using Document.LastSection.Body.LastParagraph.AppendChild( textboxShape ), but when opening the word document it doesn’t show up in there.

Here is attached Word file that I use for testing, and what happens after it is stamped.

Word Documents.zip (31.4 KB)

Document.docx is the file before stamping.
Document.Stamped.docx is the file after adding textbox shapes with:
document.FirstSection.Body.FirstParagraph.AppendChild( textboxShape );
document.LastSection.Body.FirstParagraph.AppendChild( textboxShape );
document.LastSection.Body.LastParagraph.AppendChild( textboxShape );

Please let me know if you need any other details, I’m more than happy to provide!

@Matti_Ketonen,

Thanks for your inquiry. We have tested the scenario using following code example and have not found the shared issue. Please share the code example that you are using here for testing. We will investigate the issue and provide you more information on this.

Document doc = new Document(MyDir + "Document.docx");

Shape textboxShape = new Shape(doc, ShapeType.TextBox);
textboxShape.WrapType = WrapType.Inline;
Paragraph para = new Paragraph(doc);
para.AppendChild(new Run(doc, "353-00000011v1.0  LastSection.LastParagraph"));

textboxShape.AppendChild(para);
textboxShape.Width = 500;
textboxShape.Height = 50;
doc.LastSection.Body.LastParagraph.AppendChild(textboxShape);

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

Best Regards,
Tahir Manzoor

Thanks again for a quick reply,

Here are the metods that I use to draw the shape and add it to multiple locations.

private static void AddStamp( string documentFile, string saveAsFile, string stampText )
{
// Open document.
Aspose.Words.Document document = new Aspose.Words.Document( documentFile );

// Stamp the first page.
//document.StampOnlyFirstPage( ParagraphAlignment.Center, MyStampText );
document.StampOnlyFirstPageUsingTextBox( ParagraphAlignment.Center, MyStampText );

document.StampOnlyLastPageUsingTextBox( ParagraphAlignment.Center, MyStampText );

// Save the file.
document.Save( saveAsFile );

}

private static void StampOnlyFirstPageUsingTextBox( this Aspose.Words.Document document, ParagraphAlignment alignment, string stamp )
{
// Create textbox
Aspose.Words.Drawing.Shape textboxShape = CreateStampTextBox( document, alignment, stamp );

// Add textbox to the document.
document.FirstSection.Body.FirstParagraph.AppendChild( textboxShape );

}

private static void StampOnlyLastPageUsingTextBox(this Aspose.Words.Document document, ParagraphAlignment alignment, string stamp)
{
// Create textbox.
Aspose.Words.Drawing.Shape textboxShape = CreateStampTextBox( document, alignment, stamp + " LastSection.FirstParagraph" );

// Add textbox to the document.
document.LastSection.Body.FirstParagraph.AppendChild( textboxShape );

Aspose.Words.Drawing.Shape textboxShape2 = CreateStampTextBox(document, alignment, stamp + "  LastSection.LastParagraph");
document.LastSection.Body.LastParagraph.AppendChild( textboxShape2 );

}

private static Aspose.Words.Drawing.Shape CreateStampTextBox( Aspose.Words.Document document, ParagraphAlignment alignment, string stamp )
{
// Create textbox shape.
Aspose.Words.Drawing.Shape textboxShape = new Aspose.Words.Drawing.Shape( document, Aspose.Words.Drawing.ShapeType.TextBox );

// Set size.
textboxShape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
textboxShape.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
//textboxShape.Width = 610.4;
textboxShape.Width = 604;
textboxShape.Height = 24;
//textboxShape.Top = 694;
textboxShape.Top = 690;
textboxShape.Fill.Opacity = 0;
textboxShape.Stroke.On = false;

// Create stamp as paragraph of content.
Aspose.Words.Paragraph paragraph = new Aspose.Words.Paragraph( document );

// Set the alignment for the paragraph text.
paragraph.ParagraphFormat.Alignment = alignment;

// Add the content.
paragraph.AppendChild( new Aspose.Words.Run( document, stamp ) );

// Insert paragraph into the textbox.
textboxShape.AppendChild( paragraph );

return textboxShape;

}

///


/// Stamps the first page only.
///

///
///
///
private static void StampOnlyFirstPage( this Aspose.Words.Document document, ParagraphAlignment alignment, string stamp )
{
Aspose.Words.Layout.LayoutCollector layoutCollector = new Aspose.Words.Layout.LayoutCollector(document);

// Get the list of header footers, method will generate footers if needed, and will copy the content from any
// existing one to the new ones.
List< Aspose.Words.HeaderFooter > headerFooters = document.FirstSection.GetFooters( StampFrequency.FirstPageOnly );
int count = 0;
foreach (Aspose.Words.HeaderFooter headerFooter in headerFooters)
{
	count++;
	headerFooter.StampFooter( alignment, $"{stamp}    STAMP #{count}" );
}

}

@Matti_Ketonen,

Thanks for sharing the detail. We are investigating this issue and will get back to you soon.

Best Regards,
Tahir Manzoor

@Matti_Ketonen,

Please use Shape.RelativeHorizontalPosition and Shape.RelativeVerticalPosition properties to specify relative to what the shape is positioned horizontally and vertically. Please add following lines of code in CreateStampTextBox method to get the desired output. Hope this helps you.

textboxShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
textboxShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;

Best Regards,
Tahir Manzoor

Thanks @tahir.manzoor !!

This solved the case:

// Set location anchor.
textboxShape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
textboxShape.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Bottom;
textboxShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
textboxShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;