Hi,
I’m currently evaluating the Aspose.Words
library, and I believe I may have encountered a bug.
Steps to Reproduce
-
Download the attached
layout_calc_bug.docx
file and save it without making any changes.
layout_calc_bug.docx -
Copy the code below into a Visual Studio test project with a reference to the
Aspose.Words
library. -
Set the constant fields at the top of the code (license path and document path).
-
Run the tests.
Expected Result
Section.Bottom >= LastLine.Bottom
- The
GetStartPageIndex
of paragraph labeled “Page 2 Header” is 2
Actual Result
Section.Bottom < LastLine.Bottom
- The
GetStartPageIndex
of paragraph labeled “Page 2 Header” is 1
Additional Notes
After reproducing the issue, open the document in Microsoft Word and save it again (without making any changes).
After doing so, the issue no longer occurs and the test passes.
So, I don’t know that is wrong with the original (unchanged document).
using Aspose.Words.Layout;
namespace Aspose.Words.Tests;
[TestClass]
public sealed class LayoutTests
{
private const string License_Path = "YOUR ASPOSE.WORDS LICENCE PATH";
private const string Document_Path = "PATH TO THE TEST DOCUMENT";
[TestInitialize]
public void TestInitialize()
{
var lic = new License();
lic.SetLicense(License_Path);
}
private static Document GetDocument()
{
var doc = new Document(Document_Path);
doc.UpdatePageLayout();
return doc;
}
[TestMethod]
public void TestPageLayout_UsingEnumerator()
{
// Arrange
var doc = GetDocument();
LayoutEnumerator enumerator = new(doc);
// Act
// measure column
enumerator.MoveFirstChild(); // move to first column
var columnType = enumerator.Type; // column
var columnRect = enumerator.Rectangle;
// measure last line
enumerator.MoveLastChild();
var lastLineType = enumerator.Type; // column
var lastLineRect = enumerator.Rectangle;
// Assert
Assert.AreEqual(LayoutEntityType.Column, columnType);
Assert.AreEqual(LayoutEntityType.Line, lastLineType);
Assert.IsTrue(columnRect.Bottom > lastLineRect.Bottom);
}
[TestMethod]
public void TestPageLayout_UsingPageNumber()
{
// Arrange
var doc = GetDocument();
LayoutCollector collector = new(doc);
var paragraphInPge2 = doc.GetChildNodes(NodeType.Paragraph, true)
.Where(p => p.Range.Text.StartsWith("Page 2 Header"))
.First();
// Act
var pageStart = collector.GetStartPageIndex(paragraphInPge2);
// Assert
Assert.AreEqual(2, pageStart);
}
}