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

发布于 2024-11-04 22:45:28 字数 777 浏览 0 评论 0原文

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


    }

看看附加的图像

I need to print the content of my view using my mac app. I get the standard panel for print option. But while previewing my page setup is not proper.

I am using the following code for action on print button

- (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];


    }

have a look at the image attached

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失眠症患者 2024-11-11 22:45:28

经过大量研究后,我一切正常。我正在使用下面的代码&想要分享也许它可能会帮助将来的人

    [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];

After much research I got everything working fine.I am using below code & would like to share may be it might help someone in future

    [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];
且行且努力 2024-11-11 22:45:28

我自己也遇到过这个。

我的解决方案是在 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 = 横向),

尽管可能可以整理一下。

I ran into this myself.

My solution was to do this in 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 takes an integer value (0 = portrait, 1 = landscape)

Though can probably tidy things up a bit.

春风十里 2024-11-11 22:45:28

John Griffiths Answer 的 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();

C# Version of John Griffiths Answer just in case someone is on 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文