gpt4 book ai didi

objective-c - 如何使用保存的 .png 文件在创建 PDF 文件时插入分页符?

转载 作者:行者123 更新时间:2023-11-29 11:08:27 25 4
gpt4 key购买 nike

我正在使用以编程方式从 webView 获取的屏幕截图创建 pdf 文件。

但主要问题是下面的代码不会插入分页符,它只会创建整个页面。

请告诉我如何在 pdf 中插入分页符或创建多页。

- (void) drawPdf
{
CGSize pageSize = CGSizeMake(612, webViewHeight);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

// Mark the beginning of a new page.
//UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

double currentHeight = 0.0;

for (int index = 1; index <= imageName ; index++)
{

NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]];
UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];

[pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
currentHeight += pngImage.size.height;

}
UIGraphicsEndPDFContext();

}

最佳答案

- (void) drawPdf
{
CGSize pageSize = CGSizeMake(612, webViewHeight);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);


// Mark the beginning of a new page.
//UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

double currentHeight = 0.0;

for (int index = 1; index <= imageName ; index++)
{
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
//Above line would work to make page break

NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]];
UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];

[pngImage drawInRect:CGRectMake(0, 0, pageSize.width, pngImage.size.height)];
currentHeight += pngImage.size.height;

}
UIGraphicsEndPDFContext();

}

关于objective-c - 如何使用保存的 .png 文件在创建 PDF 文件时插入分页符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12709175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com