PDFRasterizer - How to fix a page offset?

Q:
I use PDFRasterize to draw PDF pages to GDI+ Bitmap. It seems that
pages are a bit shifted. This error does not occure when using the
Draw.Save api.

private void PagesAreOffTest()
{
PDFDoc Doc = null;
int PageCount = 0;

try
{
Doc = new PDFDoc(“PagesAreOffTest.pdf”);
PageCount = Doc.GetPagesCount();
}
catch (PDFNetException e)
{
Console.WriteLine("Exception opening PDF: " +
e.Message);
Doc = null;
}

if (null != Doc)
{
PDFRasterizer Rasterizer = new
PDFRasterizer(PDFRasterizer.Type.e_BuiltIn);
Rasterizer.SetCaching(false);
Matrix2D matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0);

for (int pageIndex = 1; pageIndex <= PageCount;
pageIndex++)
{
Page page = Doc.PageFind(pageIndex).Current();

int imageHeight =
(int)Math.Ceiling(page.GetPageHeight());
int imageWidth =
(int)Math.Ceiling(page.GetPageWidth());
int imageStride = imageWidth * 4;
int imageSize = imageHeight * imageWidth;
int[] image = new int[imageSize];

IntPtr ip = Marshal.AllocCoTaskMem(imageSize * 4);
Rasterizer.Rasterize(page, ip, imageWidth,
imageHeight,
-imageStride, matrix2D);
Marshal.Copy(ip, image, 0, imageSize);
Marshal.FreeCoTaskMem(ip);

using (Bitmap bmp = this.BytesToBmp(image,
imageWidth,
imageHeight))
{
bmp.Save(“PagesAreOffTest” +
pageIndex.ToString() +
“.bmp”);
}

page = null;
image = null;
}

matrix2D = null;
Rasterizer = null;

if (null != Doc)
{
Doc.Close();
Doc.Dispose();
Doc = null;
}
}
}



A:
The problem is that the file has a crop box that is not aligned with
the origin/media box.
To go around this you need to take into account page transform matrix.
For example,

// C#
Matrix2D mtx(drawing_scale,0,0,drawing_scale, 0, 0);
Page page = page_itr.Current();
mtx *= page.GetDefaultMatrix(false?); // true or false depending on
your needs

// C++
Common::Matrix2D mtx(drawing_scale,0,0,drawing_scale, 0, 0);
Page page = *page_itr;
mtx *= page.GetDefaultMatrix(false?); // true or false depending on
your needs