Currently I am working on a Paint Application for iPhones and iPads. I want to print the current screen display (Drawing) of the screen. Can any body help me in these case ?
Here is the code i have used so far ,
-(void)printImage {
NSString *path = [[NSBundle mainBundle] pathForResource:#"micky" ofType:#"png"];
NSData *dataFromPath = [NSData dataWithContentsOfFile:path];
UIPrintInteractionController *pCon = [UIPrintInteractionController sharedPrintController];
if(pCon && [UIPrintInteractionController canPrintData:dataFromPath]) {
pCon.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pCon.printInfo = printInfo;
pCon.showsPageRange = YES;
pCon.printingItem = dataFromPath;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(#"Unsuccessfull %# error%u", error.domain, error.code);
}
};
[pCon presentAnimated:YES completionHandler:completionHandler];
}
}
Again , all what i want to know is i should be able to print the Current Window(Drawing, Screen) instead of the Image Micky.png (as in the code)
This code takes a screenshot of the current view and outputs an UIImage of it:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *imageFromCurrentView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Related
I have simple UIPrintInteractionController in a controller that is presnented modally but I am getting transparent background of UIPrintInteractionController. You can see the issue in attached picture,
This is the code I am using to present UIPrintInteractionController,
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
if(!pic){
NSLog(#"Couldn't get shared UIPrintInteractionController!");
return;
}
// We need a completion handler block for printing.
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(completed && error)
NSLog(#"FAILED! due to error in domain %# with error code %u", error.domain, error.code);
};
if(pic && [UIPrintInteractionController canPrintData: myData] ) {
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPhone"]) {
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [_filePath lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
[printInfo setOrientation:UIPrintInfoOrientationPortrait];
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = myData;
docsScrollView.hidden = YES;
toolBar.hidden = YES;
self.modalPresentationStyle = UIModalPresentationFullScreen;
[pic presentAnimated:YES completionHandler:completionHandler];
}
Been trying to get this printing but it doesn't show up in the printer simulator.
This is my code to get it printing:
-(void)printItem:(NSString*)path {
// NSString *path = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"png"];
NSData *dataFromPath = [NSData dataWithContentsOfFile:#"http://www.endnote.com/support/helpdocs/EN%208%20Scripted%20Demo.pdf"];
// path = #"http://www.samplepdf.com/sample.pdf";
path = #"http://www.endnote.com/support/helpdocs/EN%208%20Scripted%20Demo.pdf";
UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
NSLog(#"Can we print? %#",printController);
// NSLog(#"Secondly, Can we print? %#",[UIPrintInteractionController canPrintURL:[NSURL URLWithString:path]]);
// if(printController && [UIPrintInteractionController canPrintData:dataFromPath]) {
printController.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
printController.printInfo = printInfo;
printController.showsPageRange = YES;
printController.printingItem = [NSURL URLWithString:path];
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(#"FAILED! due to error in domain %# with error code %u", error.domain, error.code);
}
};
[printController presentAnimated:YES completionHandler:completionHandler];
// }
}
The printer options modal pops up and allows me to choose which printer, range, copy and double sided. When I press the range, it knows that the url I'm trying to print out has 4 pages in it. I know this because when I try another url with 1 page, the page range shows 1 page.
Any ideas?
Thanks.
How can make a IBAction method for printing a UITextView with AirPrint in objective-c?
Check whether printing is available:
if ([UIPrintInteractionController isPrintingAvailable])
{
// Available
} else {
// Not Available
}
Print after button click:
-(IBAction) buttonClicked: (id) sender;
{
NSMutableString *printBody = [NSMutableString stringWithFormat:#"%#, %#",self.encoded.text, self.decoded.text];
[printBody appendFormat:#"\n\n\n\nPrinted From *myapp*"];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.titleLabel.text;
pic.printInfo = printInfo;
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
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;
[textFormatter release];
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);
}
};
[pic presentFromBarButtonItem:self.rightButton animated:YES completionHandler:completionHandler];
}
Originally posted by '87vert' at iPhone Dev SDK: Airprint Tutorial - Simple Print File
The following method uses the name of the file to be printed and also the bar button code from where you want the airprint popup to be shown.
It works for me and im sure will be helpfull
-(void)printJob:(int)jobType:(NSString*)jobName:(UIBarButtonItem *)barButton{
NSString *path;
if ([jobName isEqualToString:#"Printout.png"]) {
path= [self documentsPathForFileName:#"Printout.png"];
}
NSData *mydata=[NSData dataWithContentsOfFile:path];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = mydata;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(#"Printing could not complete because of error: %#", error);
}
};
[pic presentFromBarButtonItem:barButton animated:YES completionHandler:completionHandler];
}
I attached a file to the mail I am using this code.
[mail addAttachmentData:[myView PDFData] mimeType:#"application/pdf" fileName:#"name.pdf"];
How can I do the same thing for printing a file, I need to print this [myView PDFData].
I found only this for printing:
NSString *PDFFileWithName = [[NSBundle mainBundle] pathForResource:#"name" ofType:#"pdf"];
NSData *dataFromPath = [NSData dataWithContentsOfFile:PDFFileWithName];
Thanks
You should read through the Drawing and Printing Guide for iOS. The printingItem property of UIPrintInteractionController can be set to the NSData of a PDF.
Update for added code
The value of dataFromPath should be equal to [myView PDFData] although I would recommend changing the variable name once you get it working.
NSData *dataFromPath = [myView PDFData];
Full code to print pdf
UIPrintInteractionController *pc = [UIPrintInteractionController
sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = UIPrintInfoOrientationPortrait;
printInfo.jobName =#"Report";
pc.printInfo = printInfo;
pc.showsPageRange = YES;
pc.printingItem = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://test.com/Print_for_Client_Name.pdf"]];
// You can use here image or any data type to print.
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
NSError *error) {
if(!completed && error){
NSLog(#"Print failed - domain: %# error code %ld", error.domain,
(long)error.code);
}
};
[pc presentFromRect:CGRectMake(0, 0, 300, 300) inView:self.view animated:YES completionHandler:completionHandler];
write below code and check it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathFolder = [NSString stringWithFormat:#"%#",pdfFileName];
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pathFolder];
NSURL *targetURL = [NSURL fileURLWithPath:path];
UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = UIPrintInfoOrientationPortrait;
printInfo.jobName =#“Print”;
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pc.printInfo = printInfo;
pc.showsPageRange = YES;
pc.printingItem = targetURL;
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
NSError *error) {
if(!completed && error){
NSLog(#"Print failed - domain: %# error code %ld", error.domain, (long)error.code);
}
};
[pc presentFromRect:shareButton.frame inView:self.view animated:YES completionHandler:completionHandler];
Posted the wrong link earlier - this one should help!
Blog - Printing in iOS - Goes into great detail and includes a tutorial on Printing PDFs
my application though paper feed action is called but nothing is printed and paper comes out blank.. I am using the following code for print.
-(IBAction)printButtonAction:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"Preview.pdf"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:writableDBPath]];
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [writableDBPath lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
controller.printInfo = printInfo;
controller.showsPageRange = YES;
controller.printingItem = data;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error)
NSLog(#"FAILED! due to error in domain %# with error code %u",
error.domain, error.code);
};
UIViewPrintFormatter *viewFormatter = [documentView viewPrintFormatter];
viewFormatter.startPage = 0;
controller.printFormatter = viewFormatter;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromBarButtonItem:printButton animated:YES
completionHandler:completionHandler];
} else {
[controller presentAnimated:YES completionHandler:completionHandler];
}
}
Thanks
Deepika jain
I think the below code will work,
-(IBAction)printButtonAction:(id)sender{
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion>4.1) {
NSData *myPdfData = [NSData dataWithContentsOfFile:pdfPath]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf.
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if (controller && [UIPrintInteractionController canPrintData:myPdfData]){
controller.delegate = delegate; //if necessary else nil
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [pdfPath lastPathComponent];
//printInfo.duplex = UIPrintInfoDuplexLongEdge;
controller.printInfo = printInfo;
controller.showsPageRange = YES;
controller.printingItem = myPdfData;
// We need a completion handler block for printing.
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(completed && error){
NSLog(#"FAILED! due to error in domain %# with error code %u", error.domain, error.code);
}
};
[controller presentFromRect:rect inView:senderView animated:YES completionHandler:completionHandler];
}else {
NSLog(#"Couldn't get shared UIPrintInteractionController!");
}
}
Check the path of the pdf you fetched, I think you missed the '/' after the Documents directory. You try putting a break point and check the path, then check the |data| variable for the content from the path you specified. Try it and respond with ur comments. :)