<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>https://monobook.org/w/index.php?action=history&amp;feed=atom&amp;title=Xamarin.Mac%E3%81%A7WebKitView%E3%82%92%E5%8D%B0%E5%88%B7%E3%81%99%E3%82%8B</id>
	<title>Xamarin.MacでWebKitViewを印刷する - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="https://monobook.org/w/index.php?action=history&amp;feed=atom&amp;title=Xamarin.Mac%E3%81%A7WebKitView%E3%82%92%E5%8D%B0%E5%88%B7%E3%81%99%E3%82%8B"/>
	<link rel="alternate" type="text/html" href="https://monobook.org/w/index.php?title=Xamarin.Mac%E3%81%A7WebKitView%E3%82%92%E5%8D%B0%E5%88%B7%E3%81%99%E3%82%8B&amp;action=history"/>
	<updated>2026-06-04T07:26:23Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://monobook.org/w/index.php?title=Xamarin.Mac%E3%81%A7WebKitView%E3%82%92%E5%8D%B0%E5%88%B7%E3%81%99%E3%82%8B&amp;diff=12319&amp;oldid=prev</id>
		<title>Administrator: ページの作成:「Xamarin.Macに限らずCocoaではNSPrintOperationにNSViewをぶち込めば簡単に印刷できる。 NSViewはPDFのレンダリングエンジンその…」</title>
		<link rel="alternate" type="text/html" href="https://monobook.org/w/index.php?title=Xamarin.Mac%E3%81%A7WebKitView%E3%82%92%E5%8D%B0%E5%88%B7%E3%81%99%E3%82%8B&amp;diff=12319&amp;oldid=prev"/>
		<updated>2020-03-06T02:52:42Z</updated>

		<summary type="html">&lt;p&gt;ページの作成:「&lt;a href=&quot;/wiki/Xamarin.Mac&quot; class=&quot;mw-redirect&quot; title=&quot;Xamarin.Mac&quot;&gt;Xamarin.Mac&lt;/a&gt;に限らず&lt;a href=&quot;/wiki/Cocoa&quot; title=&quot;Cocoa&quot;&gt;Cocoa&lt;/a&gt;ではNSPrintOperationにNSViewをぶち込めば簡単に印刷できる。 NSViewは&lt;a href=&quot;/w/index.php?title=PDF&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;「PDF」 (存在しないページ)&quot;&gt;PDF&lt;/a&gt;のレンダリングエンジンその…」&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Xamarin.Mac]]に限らず[[Cocoa]]ではNSPrintOperationにNSViewをぶち込めば簡単に印刷できる。&lt;br /&gt;
NSViewは[[PDF]]のレンダリングエンジンそのものなので「改ページ」などの紙特有の処理部分だけを実装するだけでよい。&lt;br /&gt;
&lt;br /&gt;
ただWebKitViewに限っては普通にNSPrintOperationにぶち込んだだけでは全ページが白紙になる。&lt;br /&gt;
改ページなどは正常に動くのだが何も印刷されない。&lt;br /&gt;
&lt;br /&gt;
== 解決策 ==&lt;br /&gt;
[[stackoverflow]]で[[swift]]での解決策を発見した。&lt;br /&gt;
「_printOperationWithPrintInfo:」というセレクターでNSPrintOperationを生成するといいらしい。&lt;br /&gt;
* https://stackoverflow.com/questions/27127919/wkwebkit-mac-os-nsprintoperation&lt;br /&gt;
&lt;br /&gt;
さっそく拡張メソッドにしてみた。&lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
public static class WebKitViewExtensins&lt;br /&gt;
{&lt;br /&gt;
    public static NSPrintOperation GetPrintOperation(this WebKit.WKWebView webKitView, NSPrintInfo printInfo)&lt;br /&gt;
    {&lt;br /&gt;
        // WebKitViewからNSPrintOperationを引き出すセレクター&lt;br /&gt;
        var printSelector = new ObjCRuntime.Selector(&amp;quot;_printOperationWithPrintInfo:&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // セレクターがあるか確認する&lt;br /&gt;
        if (!webKitView.RespondsToSelector(printSelector))&lt;br /&gt;
        {&lt;br /&gt;
            return null;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        // セレクターを実行する&lt;br /&gt;
        return (NSPrintOperation)webKitView.PerformSelector(printSelector, printInfo);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
印刷してみる。&lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
public override void ViewDidLoad()&lt;br /&gt;
{&lt;br /&gt;
    base.ViewDidLoad();&lt;br /&gt;
&lt;br /&gt;
    // WebKitViewはUTF-8を含むURLを扱えないのでURLエンコードしてやる必要がある。&lt;br /&gt;
    var uri = new Uri(&amp;quot;https://monobook.org/wiki/クソゲー・オブ・ザ・イヤー&amp;quot;);&lt;br /&gt;
    var nsurl = new NSUrl(uri.GetComponents(UriComponents.HttpRequestUrl, UriFormat.UriEscaped));&lt;br /&gt;
    var request = new NSUrlRequest(nsurl);&lt;br /&gt;
    _webKitView.LoadRequest(request);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public void Print()&lt;br /&gt;
{&lt;br /&gt;
    //&lt;br /&gt;
    var pi = NSPrintInfo.SharedPrintInfo;&lt;br /&gt;
    pi.HorizontalPagination = NSPrintingPaginationMode.Auto;&lt;br /&gt;
    pi.VerticalPagination = NSPrintingPaginationMode.Auto;&lt;br /&gt;
    pi.VerticallyCentered = false;&lt;br /&gt;
&lt;br /&gt;
    // これ&lt;br /&gt;
    var po = _webKitView.GetPrintOperation(pi);&lt;br /&gt;
    po.ShowsPrintPanel = true;&lt;br /&gt;
&lt;br /&gt;
    // 印刷実行&lt;br /&gt;
    po.RunOperation();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 関連項目 ==&lt;br /&gt;
* [[WebKit]]&lt;br /&gt;
* [[GeckoFX]]&lt;br /&gt;
&lt;br /&gt;
[[category: Xamarin.Mac]]&lt;/div&gt;</summary>
		<author><name>Administrator</name></author>
	</entry>
</feed>