Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3027

expandToFit & centering page content on printed page XPS API

$
0
0
Hello,

The following shows the calculation and application of shrink to fit without centering the page content on the printed page. I use a 3rd party library to create the matrix which is then applied to the XPS_MATRIX.

I understand that working out where the page should sit on the page requires knowing the media size and the desired size of the printed page, then calculating what the margin will be; BUT I do not understand how to do this calculation for the desired margin so I am asking for clarification on how to apply centering through a calculated margin in both rotations of the page.

As of right now, this applies shrink to fit, but I also need to figure out expand to fit so I was wondering if anyone can provide insight how to do a calculation for expand to fit as well.

Code:

HDC hdc = CreateDC(L"WINSPOOL", printerName, NULL, NULL);

    const int mediaWidth =      GetDeviceCaps(hDC, PHYSICALWIDTH);
    const int mediaHeight =    GetDeviceCaps(hDC, PHYSICALHEIGHT);
    const int printableWidth =  GetDeviceCaps(hDC, HORZRES);
    const int printableHeight = GetDeviceCaps(hDC, VERTRES);
    const int printerDpiX =    GetDeviceCaps(hDC, LOGPIXELSX);
    const int printerDpiY =    GetDeviceCaps(hDC, LOGPIXELSY);
    const int leftMargin =      GetDeviceCaps(hDC, PHYSICALOFFSETX);
    const int topMargin =      GetDeviceCaps(hDC, PHYSICALOFFSETY);

    const auto scaleToFitX = static_cast<double>(printableWidth) / static_cast<double>(mediaWidth);
    const auto scaleToFitY = static_cast<double>(printableHeight) / static_cast<double>(mediaHeight);
    const auto scaleToFit = scaleToFitX <= scaleToFitY ? scaleToFitX : scaleToFitY;
    const auto leftMarginXps = static_cast<double>(leftMargin) / static_cast<double>(printerDpiX) * 96.0;
    const auto topMarginXps = static_cast<double>(topMargin) / static_cast<double>(printerDpiY) * 96.0;
    const auto printableWidthXps = static_cast<double>(printableWidth) / static_cast<double>(printerDpiX) * 96.0;
    const auto printableHeightXps = static_cast<double>(printableHeight) / static_cast<double>(printerDpiY) * 96.0;

IXpsOMCanvas* scaleCanvas;
            result = xpsObjectFactory->CreateCanvas(&scaleCanvas);
            throwOnFailure(result, "Failed to create XPS canvas.");

            auto* pageSize = new XPS_SIZE {0.0f, 0.0f};
            FMatrix fMatrix;
            switch (documentAssembly->getDocument()->getPage(pageIndex)->getRotate())
            {
            default:
            case 0:
            case 180:
                fMatrix.scale(scaleToFit, scaleToFit);
                fMatrix.translate(leftMarginXps, topMarginXps);
                pageSize->width = pageDimensions.width;
                pageSize->height = pageDimensions.height;
                page->SetPageDimensions(pageSize);
                break;
            case 90:
            case 270:
                fMatrix.scale(scaleToFit, scaleToFit);
                fMatrix.translate(topMarginXps, leftMarginXps);
                pageSize->width = pageDimensions.height;
                pageSize->height = pageDimensions.width;
                page->SetPageDimensions(pageSize);
                break;
            }

            XPS_MATRIX combinedMatrix = {
                static_cast<float>(fMatrix.xx()),
                static_cast<float>(fMatrix.xy()),
                static_cast<float>(fMatrix.yx()),
                static_cast<float>(fMatrix.yy()),
                static_cast<float>(fMatrix.dx()),
                static_cast<float>(fMatrix.dy())
            };

            IXpsOMMatrixTransform* rawG;
            result = xpsObjectFactory->CreateMatrixTransform(&combinedMatrix, &rawG);

Thanks for any help, much is appreciated.

Viewing all articles
Browse latest Browse all 3027