打印纸张尺寸和内容插图 [英] Print paper size and content inset

查看:259
本文介绍了打印纸张尺寸和内容插图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if(![UIPrintInteractionController isPrintingAvailable])

使用下面的代码打印包含文本和图像的HTML内容。 {
UIAlertView * alertView = [[[UIAlertView alloc]
initWithTitle:NSLocalizedString(@Printer Availability Error Title,@)
message:NSLocalizedString(@Printer Availability Error Message ,@)
delegate:nil
cancelButtonTitle:NSLocalizedString(@OK,@OK)
otherButtonTitles:nil] autorelease];
[alertView show];
return;
}

UIPrintInteractionController * pic =
[UIPrintInteractionController sharedPrintController];

if(!pic){
NSLog(@Could not get shared UIPrintInteractionController!);
return;
}

pic.delegate = self;

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

NSString * htmlString = [self prepareHTMLText];
UIMarkupTextPrintFormatter * htmlFormatter =
[[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
//边长为1英寸
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0,72.0,72.0,72.0);
//打印的内容应该在这些边距内为6英寸宽b $ b htmlFormatter.maximumContentWidth = 6 * 72.0;
pic.printFormatter = htmlFormatter;
[htmlFormatter发布];

pic.showsPageRange = YES;

void(^ completionHandler)(UIPrintInteractionController *,BOOL,NSError *)=
^(UIPrintInteractionController * printController,BOOL completed,NSError * error){

if (!完成&&错误){
NSLog(@打印无法完成,因为错误:%@,错误);
}
};
$ b $ if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad){
[pic presentFromBarButtonItem:self.myPrintBarButton
animated:YES
completionHandler:completionHandler];

} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}

查看附件中的结果(缩小版本可能不是很清楚,但,希望你能得到这张照片)。



以下是我的问题:


  1. 由AirPrint确定的打印纸张尺寸如何?如果我想特别格式化并打印A4纸的数据,该怎么办?

  2. 使用上述代码和使用不同模拟打印机(打印机模拟器)打印的结果是在所有情况下,我会在第一页的顶部留出1英寸的边距,但不是连续的页面上。为什么?使用上述代码和使用不同模拟打印机(打印机模拟器)打印的结果是,在某些情况下,字体样式会丢失。结果,内容向下移动。为什么?


解决方案


  1. 我实现了< UIPrintInteractionControllerDelegate> 协议的 printInteractionController:choosePaper:方法,并且在支持的情况下返回A4纸张尺寸(使用 [UIPrintPaper bestPaperForPageSize:withPapersFromArray:] 进行测试)。请注意,此处未设置纵向/横向方向,而是由 UIPrintInfo property orientation 。


  2. 属性 htmlFormatter.contentInsets 在整个页面被页面渲染器分割为不同页面之前,只设置了内容的插图。我可以通过添加空白页眉和页脚来设置每页边距1厘米, c $ c> UIPrintPageRenderer ,然后在HTML打印f的左侧和右侧添加1cm的边距ormatter:

      UIPrintPageRenderer * renderer = [[UIPrintPageRenderer alloc] init]; 
    renderer.headerHeight = 30.0f;
    renderer.footerHeight = 30.0f;
    pic.printPageRenderer = renderer;
    [渲染器发布];

    UIMarkupTextPrintFormatter * htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(0.0f,30.0f,0.0f,30.0f);
    [renderer addPrintFormatter:htmlFormatter startingAtPageAtIndex:0];
    [htmlFormatter发布];


  3. 无法对付这个抱歉。



I'm using following code to print HTML content containing text and images.

if (![UIPrintInteractionController isPrintingAvailable]) {
    UIAlertView *alertView = [[[UIAlertView alloc] 
        initWithTitle:NSLocalizedString(@"Printer Availability Error Title", @"")
        message:NSLocalizedString(@"Printer Availability Error Message", @"")
        delegate:nil
        cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
        otherButtonTitles:nil] autorelease];
    [alertView show];
    return;
}

UIPrintInteractionController *pic = 
    [UIPrintInteractionController sharedPrintController];

if(!pic) {
    NSLog(@"Couldn't get shared UIPrintInteractionController!");
    return;
}

pic.delegate = self;

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

NSString *htmlString = [self prepareHTMLText];
UIMarkupTextPrintFormatter *htmlFormatter = 
    [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
// 1-inch margins on all sides
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
// printed content should be 6-inches wide within those margins
htmlFormatter.maximumContentWidth = 6 * 72.0;   
pic.printFormatter = htmlFormatter;
[htmlFormatter 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);
    }
};

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [pic presentFromBarButtonItem:self.myPrintBarButton 
        animated:YES 
        completionHandler:completionHandler];

} else {
    [pic presentAnimated:YES completionHandler:completionHandler];
}

See attached for results (the scaled down version may not be very clear, but, hopefully, you get the picture).

Here are my questions:

  1. How is the print paper size determined by AirPrint? What if I want to specifically format and print data for A4 paper?

  2. The result of using the above code and printing using different simulated printers (Printer Simulator) is that, in all cases, I get a 1 inch margin on the top of first page, but not on consecutive pages. Why?

  3. The result of using the above code and printing using different simulated printers (Printer Simulator) is that, in some cases, the font style is lost. As a result, the content is shifted down. Why?

解决方案

  1. To specifically select A4 paper size, I implemented the printInteractionController:choosePaper: method of the <UIPrintInteractionControllerDelegate> protocol, and returned an A4 paper size if supported by the printer (test with [UIPrintPaper bestPaperForPageSize:withPapersFromArray:]. Note that portrait/landscape orientation is not set here, but by the UIPrintInfo property orientation.

  2. The property htmlFormatter.contentInsets only sets the inset for the content as a whole, before it's been split across pages by the page renderer. I was able to set a 1cm per-page margin by adding blank headers and footers via a UIPrintPageRenderer, and then adding a 1cm margin to the left and right of the HTML print formatter:

    UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
    renderer.headerHeight = 30.0f;
    renderer.footerHeight = 30.0f;
    pic.printPageRenderer = renderer;
    [renderer release];
    
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText: htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(0.0f, 30.0f, 0.0f, 30.0f);
    [renderer addPrintFormatter: htmlFormatter startingAtPageAtIndex: 0];
    [htmlFormatter release];
    

  3. Can't help with this one sorry.

这篇关于打印纸张尺寸和内容插图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆