Как сделать многостраничный PDF из textView? - PullRequest
3 голосов
/ 01 марта 2012

Я сгенерировал файл PDF из своего приложения для iPhone, и хотя большинство документов составляют только одну страницу, я хочу иметь возможность определить, будет ли текст выходить за пределы «полей», и если это так, добавить его вследущая страница.Я новичок в этом, так что не совсем уверен, как это сделать.

Ниже приведен код.Какие-либо предложения?

- (void) drawBorder
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    UIColor *borderColor = [UIColor grayColor];

    CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2);

    CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor);
    CGContextSetLineWidth(currentContext, kBorderWidth);
    CGContextStrokeRect(currentContext, rectFrame);
}

- (void)drawPageNumber:(NSInteger)pageNumber
{
    NSString* pageNumberString = [NSString stringWithFormat:@"Page %d", pageNumber];
    UIFont* theFont = [UIFont systemFontOfSize:12];

    CGSize pageNumberStringSize = [pageNumberString sizeWithFont:theFont
                                               constrainedToSize:pageSize
                                                   lineBreakMode:UILineBreakModeWordWrap];

    CGRect stringRenderingRect = CGRectMake(kBorderInset,
                                            pageSize.height - 40.0,
                                            pageSize.width - 2*kBorderInset,
                                            pageNumberStringSize.height);

    [pageNumberString drawInRect:stringRenderingRect withFont:theFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
}

- (void) drawHeader:(NSString *)header
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

    NSString *textToDraw = header;

    UIFont *font = [UIFont systemFontOfSize:24.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, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

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

- (void) drawText:(NSString *)bodyText
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

    NSString *textToDraw = bodyText;

    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) drawLine
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(currentContext, kLineWidth);

    CGContextSetStrokeColorWithColor(currentContext, [UIColor blackColor].CGColor);

    CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 40.0);
    CGPoint endPoint = CGPointMake(pageSize.width - 2*kMarginInset -2*kBorderInset, kMarginInset + kBorderInset + 40.0);

    CGContextBeginPath(currentContext);
    CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);

    CGContextClosePath(currentContext);    
    CGContextDrawPath(currentContext, kCGPathFillStroke);
}

- (void) drawImage:(UIImage *)image
{
    //UIImage * demoImage = [UIImage imageNamed:@"demo.png"];
    [image drawInRect:CGRectMake( (pageSize.width - image.size.width/2)/2, 350, image.size.width/2, image.size.height/2)];
}


- (void) generatePdf: (NSString *)thefilePath :(NSString *) theHeader :(NSString *) theText :(UIImage *) theImage
{

    pageSize = CGSizeMake(612, 792);
    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.
        currentPage++;
        [self drawPageNumber:currentPage];

        //Draw a border for each page.
        [self drawBorder];

        //Draw text fo our header.
        [self drawHeader:theHeader];

        //Draw a line below the header.
        [self drawLine];

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

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

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

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)generatePdfWithFileName:(id)sender
{
    pageSize = CGSizeMake(612, 792);
    NSString *fileName = @"test.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    NSString *header = @"This is the bad ass header section!";
    NSString *text = @"There is so much to say here I don't know where to begin....";

    [self generatePdf : pdfFileName : header:text:nil];
}

@end

Ответы [ 2 ]

9 голосов
/ 02 марта 2012

Концепцию можно увидеть здесь

[из http://spitzkoff.com/craig/?p=160]

При размещении предметов, получите Высота:

CGSize size = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
[name drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
currentPageY += size.height;

Всоответствующие пункты, проверьте текущий Y и решите, нужно ли вам перейти на новую страницу:

if (size.height + currentPageY > maxHeight) {
// create a new page and reset the current page's Y value
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
currentPageY = kMargin;
}

Надеюсь, это поможет.

1 голос
/ 19 сентября 2015

Я новичок в stackoverflow, но я хотел бы предложить краткий ответ на проблему "разбить на части длинное текстовое представление".

Метод, который я использую, безусловно, не самый эффективный способ разбить текст на части, но это единственный способ, который я нашел, и он работает. Взяв часть текста, я просто проверяю в цикле, перед рисованием, слово за словом, если вложенный текст помещается на моей странице, то есть высота разрешена. Если весь текст не помещается, тогда мой код будет рисовать только найденную часть. Например, я мог бы разбить длинный текст на четыре страницы ... по крайней мере. Извините за недостаток опыта, и если мой ответ, это просто проблеск, идея "как это сделать" не достаточно ясна.

Чтобы уточнить мой ответ. Я добавил метод, который я использую, чтобы проверить необходимую (найденную) высоту для «подтекста» (проверяя каждый раз с добавлением слова в «подтекст»). Поскольку sizeWithFont: устарело с IOS 7, я использую метод boundingRectWithSize:

// Process, that can take long though, to find the next start of the part of a text that hasn't been drawned yet.

float widthRect;
widthRect = pageWidth - 2 * leftMargin;

    - (unsigned int) findNextStartingPos:(NSString *)text withHeight:(float) height
    {
        NSString *character;
        unsigned int pos = 0;
        unsigned int posFound = 0;
        unsigned int lastPosFound = 0;
        BOOL found = NO;
        NSString *textTest;
        float heightFound;

        while (pos <= [text length]-1)
        {
            character = [text substringWithRange:NSMakeRange(pos, 1)];
            if ([character isEqualToString:@" "] || [character isEqualToString:@"\n"])
            {
                posFound = pos;
                found = YES;
            }

            if (found)
            {
                textTest = [text substringWithRange:NSMakeRange(0, posFound)];
                heightFound = lroundf([self heightFound:textTest withWidth:widthRect fontSize:fontSizeBody center:NO]);

                if (heightFound > height)
                {
                    if (lastPosFound == 0)
                    {
                        posFound = lastPosFound;
                    } else
                    {
                        posFound = lastPosFound + 1;
                    }
                    if (posFound > [text length]-1) posFound = lastPosFound;
                    break;
                }
                lastPosFound = posFound;
            }
            pos++;
        }

        return posFound;

    }  return posFound;

    }


// Returns the height needed to draw a text (and to check for space in a page)
-(float) heightFound:(NSString *)text withWidth:(float)width fontSize:(CGFloat)fontSize center:(BOOL)center
{
    if ([text length] != 0)
    {
        UIFont *font = [UIFont fontWithName:@"Helvetica" size:fontSize];

        NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        if (center)
        {
            paragraphStyle.alignment = NSTextAlignmentCenter;
        } else {
            paragraphStyle.alignment = NSTextAlignmentLeft;
        }
        NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraphStyle};

        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:attributes];

        CGRect newRect = [attributedString boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];

        return newRect.size.height;

    } else {
        return 0;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...