public static void Print(DockPanel dockPanel)
{
LocalPrintServer localPrintServer = new();
PrintQueue printQueue = localPrintServer.DefaultPrintQueue;
// PDF
// printQueue = new(localPrintServer, "Microsoft Print to PDF");
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printQueue);
PrintTicket printTicket = printQueue.DefaultPrintTicket;
printTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA4);
// 縦
//printTicket.PageOrientation = PageOrientation.Portrait;
// 横
printTicket.PageOrientation = PageOrientation.Landscape;
// 印刷データ
var canvas = new Canvas();
Canvas.SetTop(dockPanel, 50);
Canvas.SetLeft(dockPanel, 50);
canvas.Children.Add(dockPanel);
FixedPage? fixedPage = new();
fixedPage.Children.Add(canvas);
writer.Write(fixedPage, printTicket);
canvas.Children.Remove(dockPanel);
}
最初にDockPanelの親から切り離す
gridMain.Children.Remove(dockPanelPrint);
Print(dockPanelPrint);
gridMain.Children.Add(dockPanelPrint);