【问题标题】:Create PDF of A4 size paper with images of original sizes使用原始尺寸的图像创建 A4 尺寸纸张的 PDF
【发布时间】:2017-02-22 12:57:25
【问题描述】:

我正在将每张 PPT 幻灯片转换为图像并从中创建 PDF。图像以其原始尺寸正确创建。现在要创建 PDF,我使用下面的代码。

    CGSize paperSize = CGSizeMake(595.2,841.8);

    NSString *strPath = [[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"slide%i.png", i]];
     NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:strPath]];

    UIImage *image = [[UIImage alloc] initWithData:imgData];
    CGRect rect = CGRectMake(0, 0,paperSize.width ,paperSize.height);
UIGraphicsBeginPDFPageWithInfo(rect, nil);
                [image drawInRect:rect];

UIGraphicsEndPDFContext();

它的确切作用是用纸张大小而不是原始大小绘制图像。我想在 A4 尺寸的纸上绘制原始尺寸的图像。任何帮助将不胜感激。

【问题讨论】:

  • 你在绘制图像时传递了纸张尺寸检查这一行 [image drawInRect:rect] 所以在绘制 pdf 时传递原始尺寸
  • 所以它创建了具有图像大小的 PDF。
  • @kirtimali:你是对的。将 A4 尺寸传递给 rect 有效。非常感谢。加一。在答案中发布我的工作代码。

标签: ios objective-c pdf uigraphicscontext


【解决方案1】:

Kriti 的建议对我有用。以下是工作代码:

UIGraphicsBeginPDFContextToData(pdfFile, CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height), nil);

NSString *strPath = [[[appDelegate getDocumentDirectory] stringByAppendingPathComponent:@"EPC"] stringByAppendingPathComponent:[NSString stringWithFormat:@"slide%i.png", i]];        

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:strPath]];

image = [[UIImage alloc] initWithData:imgData];
    rect = CGRectMake((paperSize.width - image.size.width)/2, (paperSize.height - image.size.height)/2 ,image.size.width ,image.size.height);

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height), nil);

[image drawInRect:rect];


UIGraphicsEndPDFContext();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2013-04-17
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    相关资源
    最近更新 更多