Question

I have three nsmutable array in ios. I have to print data as below example using printer , How should i do it?

example:--

 0.5    1    10
 1      10   11
 2      5    22
 3      4    6
Was it helpful?

Solution 2

You can use the UIPrintInteractionController for doing this.

Please check the below code:

NSMutableString *stringToPrint = [[NSMutableString alloc] initWithString:@""];
for(int i = 0;i<[array count];i++)
{
    [stringToPrint appendFormat:@"%@",[array objectAtIndex:i]];
    if(i%2 == 0)
    {
        [stringToPrint appendFormat:@"\n"];
    }
    else
    {
         [stringToPrint appendFormat:@"\t"];
    }
}
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Midhun";
pic.printInfo = printInfo;

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]
                                             initWithText:stringToPrint];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
textFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = textFormatter;
pic.showsPageRange = YES;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
{
    if (!completed && error)
    {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
}
else
{
    [pic presentAnimated:YES completionHandler:completionHandler];
}

Please read Printing and Drawing in iOS for more details.

OTHER TIPS

For example these are arrays :

 NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
NSArray *arr1 = [NSArray arrayWithObjects:@"17",@"27",@"37",@"47",@"57",@"67", nil];
NSArray *arr2 = [NSArray arrayWithObjects:@"171",@"271",@"371",@"471",@"571",@"671", nil];

Now get all data in NSString

NSMutableString *str = [NSMutableString string];
for (int i=0; i<[arr count]; i++)
{
    str = [str stringByAppendingFormat:[NSString stringWithFormat:@"%@  %@  %@\n",[arr objectAtIndex:i],[arr1 objectAtIndex:i],[arr2 objectAtIndex:i]]];
}
NSLog(@"%@",str);

Use toPdfData method to get NSData to print.

NSData *data = [self toPdfData:str];

Add these method:

- (NSData *) toPdfData :(NSMutableString *)str
{
  //calculate size
  CGSize stringSize = [str sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320 , 999) lineBreakMode:UILineBreakModeWordWrap];

  // Creates a mutable data object for updating with binary data, like a byte array
  NSMutableData *pdfData = [NSMutableData data];

  // Points the pdf converter to the mutable data object and to the UIView to be converted
  UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, stringSize.width, stringSize.height), nil);
  UIGraphicsBeginPDFPage();

  // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
  [str drawInRect:CGRectMake(0, 0, stringSize.width, stringSize.height) withFont:[UIFont systemFontOfSize:15]];

  // remove PDF rendering context
  UIGraphicsEndPDFContext();

  // Retrieves the document directories from the iOS device
  NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

  NSString* documentDirectory = [documentDirectories objectAtIndex:0];
  NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

  // instructs the mutable data object to write its context to a file on disk
  [pdfData writeToFile:documentDirectoryFilename atomically:YES];
  NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);

  return pdfData;
}

EDIT : Now you have PDF Data and PDF file also store in NSDocument Directory so print using UIPrintInteractionController

Use following code to traverse and print.

    for (int i=0; i<[array count]; i++) {
    NSLog(@"%@",[array objectAtIndex:i]);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top