1

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

}
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 に答える