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

Swa*_*tik 2 printing macos cocoa

我需要使用我的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];


    }
Run Code Online (Sandbox Code Playgroud)

看看附上的图片

Swa*_*tik 10

经过大量的研究,我得到了一切正常.我正在使用下面的代码,并希望分享可能会有助于将来的某个人

    [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];
Run Code Online (Sandbox Code Playgroud)