question

BitSmithy-4663 avatar image
0 Votes"
BitSmithy-4663 asked BitSmithy-4663 commented

How to set page margins during printing

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

windows-uwp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm not sure about "I want to place it at the top of the left/top margins" and "set current margins of the page", do you mean you want to put the content from the right to the left of the printing preview or just want to change the space between the content of your document and the edge of the paper? Can you show more details about your expected behavior?

0 Votes 0 ·

I want to do all of the above.
1. I want to get (and set too) current margins,
2. And next I want to change the space between the content of my document and the edge of the paper. But this space will be the same as margins space. My content will be place at the left-upper corner of the margins.

0 Votes 0 ·

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered BitSmithy-4663 commented

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.







· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


I have some questions to this answer:
1. What is ApplicationContentMarginLeft/Top it is user defined or it is any predefined property? Where is it from?
2. What from can I get the minimal available margin (it is dependant from printer type)
3. I dont see code which places content. I can see only code which sets margins

0 Votes 0 ·

The ApplicationContentMarginLeft/Top is defined in the PrintHelper.cs of the official sample. When set ApplicationContentMarginLeft/Top in the printDetailedOptions_OptionChanged event of Scenario3CustomOptions.xaml.cs and then call InvalidatePreview() method, it will trigger the AddOnePrintPreviewPage() method in PrintHelper.cs to reset the margin. In general, the minimal margin is Zero. In addition, what does "places content" mean? Can you show more details about the content you refer to and your expected behavior?

0 Votes 0 ·

OK. I understood that margins should be set programitically by user, they arent set by printer.
In docs it isnt clearly explained.
thanks It helped me very much.

0 Votes 0 ·