BDGapps
BDGapps

Reputation: 3356

iOS Printing with image and text

I am trying to print an image and some text above it. When I print the image it takes up the entire page I only want it in the center and about 320 x 480 pix. Someone told me I needed to use rects but I do not know how to do this. The code below is what I have so far, it just prints the image but it has no size constraints. Thank You.

-(void)sendToPrinter{
UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

if(!print){

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Print Unavailable!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    return;
}


pageSize = print.printPaper.paperSize;
pageSize = CGSizeMake(print.printFormatter.printPageRenderer.printableRect.size.width, print.printFormatter.printPageRenderer.printableRect.size.height);



NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *projectTitle = [data stringForKey:@"pName1"];
NSString *fileName = [NSString stringWithFormat:@"%@.pdf", projectTitle];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];


NSData *finalImageData = [NSData dataWithContentsOfFile:pdfFileName];


print.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [[(PageView *)[self view] representedPage] title];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = finalImageData;
UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
viewFormatter.startPage = 0;

print.printFormatter = viewFormatter;

UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
    if(!completed && error){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Print Unavailable!" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
};

[print presentAnimated:YES completionHandler:completionHandler];

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

NSString *textToDraw = @"Lorem ipsum ";


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 + 350.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

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

- (void) drawImage{

//paint chart

UIImage* chartImage = [self getUIImageScreenShot];;

[chartImage drawInRect:CGRectMake( (pageSize.width - chartImage.size.width/2)/2, 70, chartImage.size.width/2, chartImage.size.height/2)];
//[chartImage drawInRect:CGRectMake( (pageSize.width - chartImage.size.width/2)/2, 70, chartImage.size.width/2, chartImage.size.height/2)];}

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

enter image description here

Upvotes: 2

Views: 4202

Answers (2)

Dan
Dan

Reputation: 398

I use the following to insert a simple image into and text into PDF, might point you in the right direction

- (IBAction)generatePdfButtonPressed:(id)sender{
pageSize = CGSizeMake(612, 792);

NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *projectTitle = [data stringForKey:@"pName1"];
NSString *fileName = [NSString stringWithFormat:@"%@.pdf", projectTitle];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

[self generatePdfWithFilePath:pdfFileName];}

and then...

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

NSString *textToDraw = @"Lorem ipsum ";


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 + 350.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

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

- (void) drawImage{

//paint chart
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"chartImageP1"];
UIImage* chartImage = [UIImage imageWithData:imageData];

[chartImageBack drawInRect:CGRectMake( (pageSize.width - chartImageBack.size.width/2)/2, 70, chartImageBack.size.width/2, chartImageBack.size.height/2)];
[chartImage drawInRect:CGRectMake( (pageSize.width - chartImage.size.width/2)/2, 70, chartImage.size.width/2, chartImage.size.height/2)];}

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

Upvotes: 1

czechboy
czechboy

Reputation: 944

seems like you are only adding the picture to the printing item

print.printingItem = imageData;

have you tried creating the whole "picture" of what you want printed out (including the text and stuff) and sending that to the printer instead?

Upvotes: 1

Related Questions