Q: I am swapping out PDFSharp for PDFTron in lots of our code and
would like to use the same coordinate system that PDFTron used for now
so we do not have to rewrite all of the element positioning code
(which is a LOT of code).
PDFSharp uses a different Y origin to PDFTron.
To do this I need to reverse the Y axis of the page when I create it.
I can see the GetDefaultMatrix method of the Page object which looks
like it might allow me to do this, but I cannot see a way to apply
that matrix back to the page.
I'd expect something like PageObject.SetTransform(MyMatrix) but I
can't see it and I've searched the docs, samples and Google for usage
of this method.
Is there a way to apply this or am I barking up the wrong tree?
A: To flip the coordinate system when generating PDF document, you can
subtract the Y coordinate from the current page height.
Something along the following lines:
Assuming you have
Modify the code as follows:
double page_height = page.GetPageHeight();
builder.MoveTo(306, page_height - 396);
builder.LineTo(681, page_height - 771);
And so forth for all Y (vertical) coordinates.
Another option is to set the 'Mirror' trasfromation matrix:
// Start a new content group.
Element element = bld.CreateGroupBegin();
// Compensate for effects of page rotation and crop box.
element.GetGState().SetTransform(1, 0, 0, -1, 0, page.GetPageHeight
// Add content to PDF page.
// Because of the mirror matrix on the outer group the content will
// be added relative to the upper-left corner of the page.
writer.WriteElement(bld.CreateRect(0, 0, 10, 10));