How to set page margins during printing

BitSmithy 2,141 Reputation points
2020-04-10T17:35:23.403+00:00

I want to print from UWP, I prepare UIElement for printing and set it as PreviewPage

this.printDocument.SetPreviewPage(e.PageNumber, currentXAMLToPrint as UIElement);

Printing preview shows right printing preview but without margins. currentXAMLToPrint is placed at the top of the page. I want to place it at the top of the left/top margins (left upper corner of the margins).

How to set/get current margins of the page and set currentXAMLToPrint inside these margins

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,221 Reputation points
    2020-04-20T09:36:39.027+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I want to change the space between the content of my document and the edge of the paper.

    About change the space between the content of my document and the edge of the paper, you can refer to the Scenario3CustomOptions part of Printing official sample. You can use PrintCustomItemListOptionDetails property to create a list of Margins option. Then if you want to change the margin, in the printDetailedOptions_OptionChanged event, setting the ApplicationContentMarginTop and ApplicationContentMarginLeft as 0. For example:

    RandomAccessStreamReference narrowMarginsIconReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/narrowMargins.svg"));
    zeroMarginsIconTask = narrowMarginsIconReference.OpenReadAsync().AsTask();
    
    
    PrintCustomItemListOptionDetails margins = printDetailedOptions.CreateItemListOption("Margins", "Margins");
    margins.AddItem("ZeroMargins", "Zero", "Each margin is 0% of the paper size", await zeroMarginsIconTask);
    
    async void printDetailedOptions_OptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
    {
        ......
        if (optionId == "Margins")
        {
            PrintCustomItemListOptionDetails marginsOption = (PrintCustomItemListOptionDetails)sender.Options["Margins"];
            string marginsValue = marginsOption.Value.ToString();
    
            switch (marginsValue)
            {
                case "ZeroMargins":
                    ApplicationContentMarginTop = 0;
                    ApplicationContentMarginLeft = 0;
                break;
                ......
            }
        }
    }
    

    Get current margins.

    You can based on the ApplicationContentMarginTop property to get current margins.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.