如何在 Objective-C 中从图像生成 PDF? (在 Mac 操作系统上)

发布于 2024-09-30 06:07:20 字数 189 浏览 0 评论 0 原文

我正在尝试构建从 Objective-C 中的一堆图像生成单个 PDF 的代码。到目前为止,我看到的大多数示例都不允许我构建 PDF,而只是生成 Cocoa 视图并将它们转换为 PDF。

我正在寻找一些工具包(如 Java 的 iText),它允许我从一堆图像生成 PDF(每个图像都是 PDF 文档中的一个页面)。

有什么提示吗?

I'm trying to build the code that will generate a single PDF from a bunch of images in Objective-C. Most of examples I've seen so far don't really allow me to build the PDF, but only generating Cocoa views and turning them into PDFs.

What I'm looking for is some toolkit (like Java's iText) that would allow me to generate a PDF from a bunch of images (with each image being a page in the PDF document).

Any hints?

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

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

发布评论

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

评论(2

多孤肩上扛 2024-10-07 06:07:20

使用 PDFKit 更简单。

PDFDocument *pdf = [[PDFDocument alloc] init];
for (NSString *fileName in fileNames)
{
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileName]; 
    PDFPage *page = [[PDFPage alloc] initWithImage:image];
    [pdf insertPage:page atIndex: [pdf pageCount]];
    [image release];
    [page release];
}

[pdf writeToFile: @"output.pdf"];

It's simpler with PDFKit.

PDFDocument *pdf = [[PDFDocument alloc] init];
for (NSString *fileName in fileNames)
{
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileName]; 
    PDFPage *page = [[PDFPage alloc] initWithImage:image];
    [pdf insertPage:page atIndex: [pdf pageCount]];
    [image release];
    [page release];
}

[pdf writeToFile: @"output.pdf"];
番薯 2024-10-07 06:07:20

请参阅创建 PDF 文件 部分。

有一个创建 pdf 文档的示例代码。您需要做的就是实现您的自定义 myDrawContent 函数,您可以在其中将图像绘制到 pdf 上下文,就像绘制任何其他 CGContextRef 一样。

See Creating a PDF File section in Quartz 2D programming guide.

There's a sample code to create a pdf document. All you'll need to do there is implement your custom myDrawContent function, where you can draw your images to pdf context same way as to any other CGContextRef.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文