Get PDF Page size from HDC C++

I want to draw string in 3 location(Top, Center, bottom) of my printing page. I use Gdiplus::RectF and drawstring() to Draw string in the page. for setting string Location I need the Page dimentions. I have to Hook EndPage (Inject DLL) and I have only the HDC. how can I Get the dimentions of printing page? here is my code :

int  StopPrint::hookFunction(HDC hdc)
{    
    sendinfo WaterMarkParams;
    rpcclient(WaterMarkParams);
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
    Graphics grpc(hdc);
    Gdiplus::FontFamily nameFontFamily(L"Arial");
    Gdiplus::Font         font(&nameFontFamily, WaterMarkParams.FontSize, 
    Gdiplus::FontStyleBold, Gdiplus::UnitPoint);
    Gdiplus::RectF        rectF(200.0f, 200.0f, 200.0f, 200.0f);
    Gdiplus::SolidBrush   solidBrush(Gdiplus::Color(100, 0, 0, 250)); 
    grpc.DrawString(WaterMarkParams.WaterMarkTxtstr, -1, &font, rectF, NULL, &solidBrush);
    return getOriginalFunction()(hdc);
}

Upvotes: 1

Views: 422

Answers (1)

I find The method that is somehow close to the correct answer, it returns dimensions close to correct size, but it's not exact. I get the printable area of the page Width and Height in pixel by GetDeviceCaps(hdc, HORZ|VERT RES) and get Number of pixels per logical inch along the screen Width and Height by GetDeviceCaps(hdc, LOGPIXELS X|Y). Then I divide them and get page size in inch. After that I get pixel per inch by(FLOAT)GetDpiForWindow(GetDesktopWindow()) and convert page size to pixel. It return dimensions but 20 - 30 pixel is smaller than correct page size. Anyway it work for A3-5 page size. The code is:

   double WidthsPixels = GetDeviceCaps(hdc, HORZRES);
   double HeightsPixels = GetDeviceCaps(hdc, VERTRES);
   double WidthsPixelsPerInch = GetDeviceCaps(hdc, LOGPIXELSX);
   double HeightsPixelsPeInch = GetDeviceCaps(hdc, LOGPIXELSY);
   float  Screendpi = (FLOAT)GetDpiForWindow(GetDesktopWindow());
   rectF.Width = (WidthsPixels/ WidthsPixelsPerInch)*Screendpi ;
   rectF.Height = (HeightsPixels/ HeightsPixelsPeInch)*Screendpi ;

Upvotes: 1

Related Questions