Y-coordinate being reported differently on even numbered pages

When we converted a word doc to a PDF using Aspose, we found that the y-coordinate is calculated from bottom-left (the standard origin 0,0) on odd page numbers and top-left on even pages. For example, placing text on two pages at the exact same location will yield different y-coordinates for that text depending on the page sequence. Scouring the web revealed that this may just be a bug with Aspose. Even more troublesome is after inspecting the PDFs MediaBox, CropBox, TrimBox, BleedBox, and ArtBox of the PDF pages it appears that all boxes report their origin at bottom-left (0,0). So something deeper in the PDF has an affect on the y-coordinates being reported.

I constructed and example where the word “Hello” was added to the first line of each four page word doc then converted the doc to a four page PDF. Searching for the word “Hello” resulted in these x/y coordinates. As you can see, the even number pages have a y-coordinate different that the odd-numbered pages.
page=1, x=90.0, y=694.998, height=792.0
page=2, x=90.0, y=83.203, height=792.0
page=3, x=90.0, y=708.797, height=792.0
page=4, x=90.0, y=83.203, height=792.0

The first page y-coordinates is off because Aspose puts the “Evaluation Only. Created with Aspose.Words…” text on the first page which bumps the word down. The even number pages are obviously reporting the wrong y-coordinates.

The java code written for this test is as follows. I’ve also attached the resulting .doc and .pdf files.

ByteArrayOutputStream wordOut = new ByteArrayOutputStream();
// Create word document with four pages.
Document wordDoc = new Document();
DocumentBuilder b = new DocumentBuilder(wordDoc);
b.write("Hello 1");
b.insertBreak(BreakType.PAGE_BREAK);
b.write("Hello 2");
b.insertBreak(BreakType.PAGE_BREAK);
b.write("Hello 3");
b.insertBreak(BreakType.PAGE_BREAK);
b.write("Hello 4");
wordDoc.save(wordOut, SaveFormat.DOC);

// Convert word document to PDF
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
Document pdfDoc = new Document(new ByteArrayInputStream(wordOut.toByteArray()));
pdfDoc.save(pdfOut, SaveFormat.PDF);

Hopefully I provided enough information to figure this out.

P.S.

This may or may not be related to this issue I found here:
Difference between coordinates

Hi Issa,

Thanks for your inquiry.

Please note that Aspose.Words mimics the same behavior as MS Word does. If you convert the same Word document to Pdf by using MS Word, you will get the same output.

In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.1.0) from here and let us know how it goes on your side. I have tested the scenario using following code example and have not found the shared issue. Please check the attached image for detail.

// Create word document with four pages.
Document wordDoc = new Document();
DocumentBuilder b = new DocumentBuilder(wordDoc);
b.write("Hello 1");
b.insertBreak(BreakType.PAGE_BREAK);
b.write("Hello 2");
b.insertBreak(BreakType.PAGE_BREAK);
b.write("Hello 3");
b.insertBreak(BreakType.PAGE_BREAK);
b.write("Hello 4");
wordDoc.save(MyDir + "out.doc", SaveFormat.DOC);
wordDoc.save(MyDir + "out.pdf");
// -------------------------------------------------------------
// com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(MyDir + "out.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("Hello");
// Accept the absorber for all the pages
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (com.aspose.pdf.TextFragment textFragment: (Iterable) textFragmentCollection)
{
    System.out.println("Text :- " + textFragment.getText());
    System.out.println("Position :- " + textFragment.getPosition());
}

Thank you Tahir for your quick and thorough response. I ran the code you wrote and I too received the correct x/y coordinates. I was using a third-party tool for verification and it didn’t occur to me that the third-party tool could be faulty which I believe was the case. Sorry for the false alarm.

Hi Issa,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.