1

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

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

请告诉我如何在 pdf 中插入 pageBreak 或创建多个页面。

- (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();

}
4

1 回答 1

3
- (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();

}
于 2012-10-03T13:38:44.860 回答