Cocoa 如何为mac应用程序以编程方式设置打印选项的页面布局

Cocoa 如何为mac应用程序以编程方式设置打印选项的页面布局,cocoa,macos,printing,Cocoa,Macos,Printing,我需要使用mac应用程序打印视图的内容。我得到了打印选项的标准面板。但是在预览我的页面时,设置不正确 我使用下面的代码对打印按钮执行操作 - (void)print:(id)sender { [[NSPrintOperation printOperationWithView:staticText] runOperation]; float horizontalMargin, verticalMargin; NSSize bounds = [p

我需要使用mac应用程序打印视图的内容。我得到了打印选项的标准面板。但是在预览我的页面时,设置不正确

我使用下面的代码对打印按钮执行操作

- (void)print:(id)sender {


        [[NSPrintOperation printOperationWithView:staticText] runOperation];
        float horizontalMargin, verticalMargin;

        NSSize bounds = [printInfo imageablePageBounds].size;
        NSSize size = [printInfo paperSize];

        horizontalMargin = 0;
        verticalMargin = 0;
        [self setPrintInfo:[NSPrintInfo sharedPrintInfo]];

        [printInfo setLeftMargin:horizontalMargin];
        [printInfo setRightMargin:horizontalMargin];
        [printInfo setTopMargin:verticalMargin];
        [printInfo setBottomMargin:verticalMargin];


    }

经过大量研究,我发现一切都很好。我正在使用下面的代码&希望与大家分享,也许这对将来的人会有所帮助

    [self setPrintInfo:[NSPrintInfo sharedPrintInfo]];
    [printInfo setVerticalPagination:NSAutoPagination];
    float horizontalMargin, verticalMargin;

    horizontalMargin = 0;
    verticalMargin = -100;

    [printInfo setLeftMargin:horizontalMargin];
    [printInfo setRightMargin:horizontalMargin];
    [printInfo setHorizontallyCentered:YES];
    [printInfo setTopMargin:-600];

    [printInfo setBottomMargin:verticalMargin];
    [[NSPrintOperation printOperationWithView:sampleText] runOperation];

我自己也遇到了这个

我的解决方案是在Swift中这样做:

let printInfo = NSPrintInfo.sharedPrintInfo()
let printOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOperation.printInfo.orientation = NSPaperOrientation(rawValue: 1)! // switch to landscape
printOperation.printInfo.leftMargin = 10
printOperation.printInfo.rightMargin = 10
printOperation.printInfo.topMargin = 10
printOperation.printInfo.bottomMargin = 10
printOperation.runOperation()
NSPaperOrientation采用整数值(0=纵向,1=横向)

不过可能会把事情整理一下。

C版本的约翰·格里菲斯会回答,以防有人在Xamarin.Mac上

  var printInfo = NSPrintInfo.SharedPrintInfo;
  var printOperation = NSPrintOperation.FromView(webContainer.MainFrame.FrameView.DocumentView, printInfo);

  printOperation.PrintInfo.Orientation = NSPrintingOrientation.Portrait;
  printOperation.PrintInfo.LeftMargin = 20;
  printOperation.PrintInfo.RightMargin = 20;
  printOperation.PrintInfo.TopMargin = 50;
                printOperation.PrintInfo.BottomMargin = 50;
                printOperation.RunOperation();

请看一下图片,好吗?如果你想要一个答案,也许你应该更好地解释一下你想要达到什么目的……我不清楚为什么文本总是出现在屏幕的中心,尽管我已经减少了页首marginI,但我想从页面的顶部显示文本谢谢你费心贴出这篇文章。一年后,由于你的努力,我偶然发现了同样的问题,很快就解决了。谢谢你的帖子。这减轻了我的巨大痛苦。目前唯一的问题是左边距。我知道这是一个老帖子。但是你能帮我解决这个问题吗。虽然我用不同的值对其进行编码,但左边距没有改变。它似乎是静止的。