如何使用 Cocoa 在 PDF 中生成网格?

发布于 2024-11-02 15:51:57 字数 355 浏览 3 评论 0 原文

我是 Cocoa 和 Objective-C 的初学者。

我想制作一个 Cocoa 应用程序,它将生成一个网格网格(用于练习中国书法)并导出为 PDF,类似于此在线生成器: http://incompetech.com/graphpaper/chinesequarter/

我应该如何生成网格?我尝试将 Quartz 与 CustomView 一起使用,但没有取得很大进展。另外,一旦在 CustomView 中绘制了网格,将其“打印”到 PDF 的方法是什么?

感谢您的帮助。

I'm a beginner to Cocoa and Objective-C.

I want to make a Cocoa application that will generate a grid of boxes (used for practicing Chinese calligraphy) to export as a PDF, similar to this online generator: http://incompetech.com/graphpaper/chinesequarter/.

How should I generate the grid? I've tried to use Quartz with a CustomView, but didn't manage to get very far. Also, once the grid is drawn in the CustomView, what is the method for "printing" that to a PDF?

Thanks for the help.

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

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

发布评论

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

评论(2

千纸鹤 2024-11-09 15:51:57

我应该如何生成网格?

实现一个自定义视图来绘制它。

我尝试将 Quartz 与 CustomView 一起使用,...

这是一种方法; AppKit绘图是另一个。不过,它们的大部分部分非常相似。 AppKit直接基于PostScript,而Quartz则间接基于PostScript。

…但没能走得太远。

您应该针对您的问题提出更具体的问题。

此外,在 CustomView 中绘制网格后,将其“打印”到 PDF 的方法是什么?

发送一条 dataWithPDFInsideRect: 消息,传递其 边界

请注意,没有“一旦在 CustomView 中绘制网格”。尽管可能存在一些内部缓存,但从概念上讲,视图不会绘制一次并保留它;而是绘制一次。它会在需要的时候、每次需要的时候、到需要的地方。当窗口需要重新绘制时,Cocoa 将告诉脏区域中的任何视图进行(重新)绘制,并且它们最终将绘制到屏幕上。当您请求 PDF 数据时,这也会告诉视图进行绘制,并且它将绘制到记录 PDF 数据的上下文中。这使得视图既可以是惰性的(仅在需要时绘制),又可以在不同的上下文中以不同的方式绘制(例如,在打印时)。

How should I generate the grid?

Implement a custom view that draws it.

I've tried to use Quartz with a CustomView, …

That's one way; AppKit drawing is the other. Most parts of them are very similar, though; AppKit is directly based on PostScript, while Quartz is indirectly based on PostScript.

… but didn't manage to get very far.

You should ask a more specific question about your problem.

Also, once the grid is drawn in the CustomView, what is the method for "printing" that to a PDF?

Send it a dataWithPDFInsideRect: message, passing its bounds.

Note that there is no “once the grid is drawn in the CustomView”. Though there may be some internal caching, conceptually, a view does not draw once and hold onto it; it draws when needed, every time it's needed, into where it's needed. When the window needs to be redrawn, Cocoa will tell any views that are in the dirty area to (re)draw, and they will draw ultimately to the screen. When you ask for PDF data, that will also tell the view to draw, and it will draw into a context that records PDF data. This allows the view both to be lazy (draw only when needed) and to draw differently in different contexts (e.g., when printing).

世界和平 2024-11-09 15:51:57

哎呀,你问的是 Cocoa,这是 Cocoa Touch,但我将把它留在这里,因为它可能会有一些用处(至少对于后来发现它的其他人来说)。

您可以在视图中绘制内容,然后将其中的内容放入 pdf 中。

此代码将获取 UIView(此处称为sheetView)中绘制的内容,将其放入 pdf 中,然后将其作为附件放入电子邮件中(以便您现在可以看到它)。您需要在标头中引用协议 MFMailComposeViewControllerDelegate。

if ([MFMailComposeViewController canSendMail]) {
    //set up PDF rendering context
    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, sheetView.bounds, nil);
    UIGraphicsBeginPDFPage();

    //tell our view to draw (would normally use setNeedsDisplay, but need drawn now).
    [sheetView drawRect:sheetView.bounds];

    //remove PDF rendering context
    UIGraphicsEndPDFContext();

    //send PDF data in mail message as an attachment
    MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
    mailComposer.mailComposeDelegate = self;If
    [mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SheetView.pdf"];
    [self presentModalViewController:mailComposer animated:YES];
}
else {
    if (WARNINGS) NSLog(@"Device is unable to send email in its current state.");
}

你还需要这个方法...

#pragma mark -
#pragma mark MFMailComposeViewControllerDelegate protocol method
//also need to implement the following method, so that the email composer can let
//us know that the user has clicked either Send or Cancel in the window.
//It's our duty to end the modal session here.
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self dismissModalViewControllerAnimated:YES];
}

Oops, you were asking about Cocoa and this is Cocoa Touch, but I'll leave it here as it may be some use (at least to others who find this later).

You can draw things in the view and then put what's there into a pdf.

This code will take what's drawn in a UIView (called sheetView here), put it into a pdf, then put that as an attachment in an email (so you can see it for now). You'll need to reference the protocol MFMailComposeViewControllerDelegate in your header.

if ([MFMailComposeViewController canSendMail]) {
    //set up PDF rendering context
    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, sheetView.bounds, nil);
    UIGraphicsBeginPDFPage();

    //tell our view to draw (would normally use setNeedsDisplay, but need drawn now).
    [sheetView drawRect:sheetView.bounds];

    //remove PDF rendering context
    UIGraphicsEndPDFContext();

    //send PDF data in mail message as an attachment
    MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
    mailComposer.mailComposeDelegate = self;If
    [mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SheetView.pdf"];
    [self presentModalViewController:mailComposer animated:YES];
}
else {
    if (WARNINGS) NSLog(@"Device is unable to send email in its current state.");
}

You'll also need this method...

#pragma mark -
#pragma mark MFMailComposeViewControllerDelegate protocol method
//also need to implement the following method, so that the email composer can let
//us know that the user has clicked either Send or Cancel in the window.
//It's our duty to end the modal session here.
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self dismissModalViewControllerAnimated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文