Skocz do zawartości

Witaj!

Zaloguj lub Zarejestruj się aby uzyskać pełny dostęp do forum.

Zdjęcie
- - - - -

Zmiana orientacji strony PDF


  • Zaloguj się, aby dodać odpowiedź
3 odpowiedzi w tym temacie

#1 uho

uho
  • 207 postów
  • SkądŁuków, PL

Napisano 30 sierpnia 2012 - 09:23

Witam, generuje sobie PDF wg poniższej metody:

- (NSData *)generatePDF
{
    vcNewForm = [FSingleton sharedInstance].vcNewForm;
    
    // PDF NSData
    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, vcNewForm.printPaper.bounds, nil);
    UIGraphicsBeginPDFPage();
    
    // print area background
    if (vcNewForm.printBackground) {
        NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/tmpFormImage.jpg"];
        UIImage *iBg = [UIImage imageWithContentsOfFile:jpgPath];
        [iBg drawInRect:CGRectMake(vcNewForm.printArea.frame.origin.x, vcNewForm.printArea.frame.origin.y, vcNewForm.printArea.frame.size.width, vcNewForm.printArea.frame.size.height)];
    }
    
    for (FormField *field in vcNewForm.printArea.subviews) {
        CGRect fieldFrameInPrintPaper = [vcNewForm.printPaper convertRect:field.frame fromView:vcNewForm.printArea];
        if ([field.fieldType isEqualToString:@"textField"]) {
            if (field.defaultText && ![field.defaultText isEqualToString:@""]) {
                [field.defaultText drawInRect:fieldFrameInPrintPaper withFont:field.font lineBreakMode:UILineBreakModeWordWrap alignment:field.textAlign];
            } else {
                [field.keyboardDictType drawInRect:fieldFrameInPrintPaper withFont:field.font lineBreakMode:UILineBreakModeWordWrap alignment:field.textAlign];
            }
        }
        if ([field.fieldType isEqualToString:@"checkbox"]) {
            [field.iconImg drawInRect:CGRectMake(fieldFrameInPrintPaper.origin.x+3, fieldFrameInPrintPaper.origin.y+3, fieldFrameInPrintPaper.size.width-6, fieldFrameInPrintPaper.size.height-6)];
        }
        if ([field.fieldType isEqualToString:@"fieldFromDB"]) {
            [field.dbField drawInRect:fieldFrameInPrintPaper withFont:field.font lineBreakMode:UILineBreakModeWordWrap alignment:field.textAlign];
        }
    }

    UIGraphicsEndPDFContext();

    return pdfData;
}

Wszystko jest ok, tylko gdy rozmiar wygenerowanego PDF jest w orientacji poziomej, to Print Controller dopasowuje go do szerokości kartki w orientacji pionowej.
Więc potrzebuje wykonać transformacje wygenerowanego PDFa zapisanego w NSData, do orientacji Pionowej, czyli obrócić go o 90 stopni.

#2 uho

uho
  • 207 postów
  • SkądŁuków, PL

Napisano 30 sierpnia 2012 - 13:03

Dodałem poniższy kod przed UIGraphicsEndPDFContext(); ale obraz się nie przekręca.

if (vcNewForm.paperOrientation = 1) {
CGContextRef context = UIGraphicsGetCurrentContext();

CFMutableDataRef myPDFData = (__bridge CFMutableDataRef)pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);

CGContextSaveGState(context);

// test
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 100, 100);
CGContextDrawPath(context, kCGPathStroke);

// wariant 1
CGAffineTransform rotate = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, vcNewForm.printPaper.bounds, 90, true);
CGContextConcatCTM(context, rotate);

// wariant 2
//CGContextRotateCTM(context, M_PI / 2);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);
}



#3 uho

uho
  • 207 postów
  • SkądŁuków, PL

Napisano 03 września 2012 - 14:12

witam, po małym olśnieniu udało mi się uzyskać zamierzony efekt:

- (NSData *)generatePDF
{
    vcNewForm = [FSingleton sharedInstance].vcNewForm;
    
    // PDF NSData
    NSMutableData *pdfData = [NSMutableData data];
    CGContextRef context;
    if (vcNewForm.paperOrientation == 0) {
        UIGraphicsBeginPDFContextToData(pdfData, vcNewForm.printPaper.bounds, nil);
    } else {
        UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, vcNewForm.printPaper.bounds.size.height, vcNewForm.printPaper.bounds.size.width), nil);
    }
    context = UIGraphicsGetCurrentContext();
    UIGraphicsBeginPDFPage();
    
    CGContextSaveGState(context);
    
    if (vcNewForm.paperOrientation == 1) {
        // rotate
        CGContextScaleCTM(context, 1, -1);
        CGContextRotateCTM(context, -M_PI / 2);
        // flip
        CGContextTranslateCTM(context, 0.0f, vcNewForm.printPaper.bounds.size.height);
        CGContextScaleCTM(context, 1.0f, -1.0f);
    }
    
    // print area background
    if (vcNewForm.printBackground) {
        NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/tmpFormImage.jpg"];
        UIImage *iBg = [UIImage imageWithContentsOfFile:jpgPath];
        [iBg drawInRect:CGRectMake(vcNewForm.printArea.frame.origin.x, vcNewForm.printArea.frame.origin.y, vcNewForm.printArea.frame.size.width, vcNewForm.printArea.frame.size.height)];
    }
    
    for (FormField *field in vcNewForm.printArea.subviews) {
        CGRect fieldFrameInPrintPaper = [vcNewForm.printPaper convertRect:field.frame fromView:vcNewForm.printArea];
        if ([field.fieldType isEqualToString:@"textField"]) {
            if (field.defaultText && ![field.defaultText isEqualToString:@""]) {
                [field.defaultText drawInRect:fieldFrameInPrintPaper withFont:field.font lineBreakMode:UILineBreakModeWordWrap alignment:field.textAlign];
            } else {
                [field.keyboardDictType drawInRect:fieldFrameInPrintPaper withFont:field.font lineBreakMode:UILineBreakModeWordWrap alignment:field.textAlign];
            }
        }
        if ([field.fieldType isEqualToString:@"checkbox"]) {
            [field.iconImg drawInRect:CGRectMake(fieldFrameInPrintPaper.origin.x+3, fieldFrameInPrintPaper.origin.y+3, fieldFrameInPrintPaper.size.width-6, fieldFrameInPrintPaper.size.height-6)];
        }
        if ([field.fieldType isEqualToString:@"fieldFromDB"]) {
            [field.dbField drawInRect:fieldFrameInPrintPaper withFont:field.font lineBreakMode:UILineBreakModeWordWrap alignment:field.textAlign];
        }
    }
    
    CGContextRestoreGState(context);
        
    UIGraphicsEndPDFContext();

    return pdfData;
}


#4 danteusz

danteusz
  • 527 postów
  • SkądWrocław-Psie Pole, Wroclaw, Poland, Poland

Napisano 03 września 2012 - 15:33

Wygląda sensownie ;)




Użytkownicy przeglądający ten temat: 1

0 użytkowników, 1 gości, 0 anonimowych