在 iPhone 中使用静态内容和图像生成 PDF

发布于 2025-01-06 02:05:35 字数 2746 浏览 0 评论 0原文

在这里,我需要通过其中一个页面中的一些动态内容生成pdf文件,下一页包含图像,但我的代码生成为单个页面,你能帮助我吗?

这个绘制文本方法用于生成动态内容

 - (void) drawText
    {
        CGContextRef    currentContext = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

        NSString *textToDraw=[[NSString alloc] initWithString:@""];
        for (int i=0; i<[fieldsArr count]; i++) 
        {
            textToDraw=[textToDraw stringByAppendingFormat:[NSString stringWithFormat:@"%@\t%@\n",[fieldsArr objectAtIndex:i],[valuesArr objectAtIndex:i]]];
        }


        textToDraw=[textToDraw stringByAppendingFormat:@"\n\n\n\n\n\n"];
        UIFont *font = [UIFont systemFontOfSize:14.0];

        CGSize stringSize = [textToDraw sizeWithFont:font
                                   constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
                                       lineBreakMode:UILineBreakModeWordWrap];

        CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

        [textToDraw drawInRect:renderingRect 
                      withFont:font
                 lineBreakMode:UILineBreakModeWordWrap
                     alignment:UITextAlignmentLeft];
    }

这个方法用于绘制图像

 - (void) drawImage
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];    
        NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"displayImage.jpg"];
        UIImage * demoImage = [UIImage imageWithContentsOfFile:getImagePath];
        [demoImage drawInRect:CGRectMake( (pageSize.width - demoImage.size.width/2)/2, 500, demoImage.size.width/2, demoImage.size.height/2)];
    }

此方法用于生成pdf文件

 - (void) generatePdfWithFilePath: (NSString *)thefilePath
    {
        UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

        NSInteger currentPage = 0;
        BOOL done = NO;
        do 
        {
            //Start a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

            //Draw a page number at the bottom of each page.

            //Draw some text for the page.
            [self drawText];

            //Draw an image
            [self drawImage];
            done = YES;
        } 
        while (!done);

        // Close the PDF context and write the contents out.
        UIGraphicsEndPDFContext();
    }

请帮助我将不同页面中的文本和图像分开

Here i need to generate the pdf file by some dynamic content in one of the page and the next page contains the image but my code is generating as a single page could you help me

This draw text method is used for generating the dynamic content

 - (void) drawText
    {
        CGContextRef    currentContext = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

        NSString *textToDraw=[[NSString alloc] initWithString:@""];
        for (int i=0; i<[fieldsArr count]; i++) 
        {
            textToDraw=[textToDraw stringByAppendingFormat:[NSString stringWithFormat:@"%@\t%@\n",[fieldsArr objectAtIndex:i],[valuesArr objectAtIndex:i]]];
        }


        textToDraw=[textToDraw stringByAppendingFormat:@"\n\n\n\n\n\n"];
        UIFont *font = [UIFont systemFontOfSize:14.0];

        CGSize stringSize = [textToDraw sizeWithFont:font
                                   constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
                                       lineBreakMode:UILineBreakModeWordWrap];

        CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

        [textToDraw drawInRect:renderingRect 
                      withFont:font
                 lineBreakMode:UILineBreakModeWordWrap
                     alignment:UITextAlignmentLeft];
    }

This Method is used for drawn the image

 - (void) drawImage
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];    
        NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"displayImage.jpg"];
        UIImage * demoImage = [UIImage imageWithContentsOfFile:getImagePath];
        [demoImage drawInRect:CGRectMake( (pageSize.width - demoImage.size.width/2)/2, 500, demoImage.size.width/2, demoImage.size.height/2)];
    }

This Method is used for generating the pdf file

 - (void) generatePdfWithFilePath: (NSString *)thefilePath
    {
        UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

        NSInteger currentPage = 0;
        BOOL done = NO;
        do 
        {
            //Start a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

            //Draw a page number at the bottom of each page.

            //Draw some text for the page.
            [self drawText];

            //Draw an image
            [self drawImage];
            done = YES;
        } 
        while (!done);

        // Close the PDF context and write the contents out.
        UIGraphicsEndPDFContext();
    }

pls help me to separate the text and image in different pages

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

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

发布评论

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

评论(2

十年不长 2025-01-13 02:05:35

检查您正在编写的内容类型,如果属于 imageView 则启动一个新的 pdf 页面。

Check the type of content you are writing , if it belongs to imageView then initiate a new pdf page.

瞳孔里扚悲伤 2025-01-13 02:05:35

当你想绘制图像时添加一个新页面。这里是更改代码,在此我在绘制图像之前添加了新页面

- (void) generatePdfWithFilePath: (NSString *)thefilePath
    {
        UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

        NSInteger currentPage = 0;
        BOOL done = NO;
        do 
        {
            //Start a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

            //Draw a page number at the bottom of each page.

            //Draw some text for the page.
            [self drawText];

            //Again start a new page to Draw an image

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


            [self drawImage];
            done = YES;
        } 
        while (!done);

        // Close the PDF context and write the contents out.
        UIGraphicsEndPDFContext();
    }

Add a new page when you want to draw image.Here is change the code, in this i have added new page before image is drawn

- (void) generatePdfWithFilePath: (NSString *)thefilePath
    {
        UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

        NSInteger currentPage = 0;
        BOOL done = NO;
        do 
        {
            //Start a new page.
            UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

            //Draw a page number at the bottom of each page.

            //Draw some text for the page.
            [self drawText];

            //Again start a new page to Draw an image

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


            [self drawImage];
            done = YES;
        } 
        while (!done);

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