iPhone应用程序中html页面的打印输出

iPhone应用程序中html页面的打印输出,iphone,ios,printing,Iphone,Ios,Printing,我在iPhone应用程序中获得html格式的文本。我需要把打印出来的页面,但我不希望标签也显示在打印出来的简单文本 在我的iPhone应用程序中,如何实现直接打印html文档而不打印html标记的打印功能?我将做一些假设,因为你的问题不是很清楚。您正试图打印HTML文档,但该文档的某些部分您不想打印。对吗 如果是这样的话,一种选择是使用类似的方法来解析文档并删除您不想要的部分。我将做一些假设,因为您的问题不是很清楚。您正试图打印HTML文档,但该文档的某些部分您不想打印。对吗 如果是这样的话,一

我在iPhone应用程序中获得html格式的文本。我需要把打印出来的页面,但我不希望标签也显示在打印出来的简单文本


在我的iPhone应用程序中,如何实现直接打印html文档而不打印html标记的打印功能?

我将做一些假设,因为你的问题不是很清楚。您正试图打印HTML文档,但该文档的某些部分您不想打印。对吗


如果是这样的话,一种选择是使用类似的方法来解析文档并删除您不想要的部分。

我将做一些假设,因为您的问题不是很清楚。您正试图打印HTML文档,但该文档的某些部分您不想打印。对吗


如果是这样的话,一个选项是使用类似的方法来解析文档并删除不需要的部分。

在这里,您可以使用以下功能在iPhone应用程序中弹出html页面

(1) 此方法在要打印html的控制器中使用

- (IBAction) btnPrint_Clicked:(id)sender
{
    NSString *htmlData = @"Your HTML Text";        
    [appDelegate.myWebView loadHTMLString:htmlData baseURL:nil];

    [appDelegate printWebPage:[NSString stringWithFormat:@"Employee - %@ %@",[dicEmployeeData valueForKey:@"firstname"],[dicEmployeeData valueForKey:@"lastname"]]];
}
(2) 在appDelegate类中使用此方法,并导入MyPrintPageRenderer.h

#import "MyPrintPageRenderer.h".

- (void) printWebPage:(NSString *)title
{
    printController = [UIPrintInteractionController sharedPrintController];
    if(!printController){
        NSLog(@"Couldn't get shared UIPrintInteractionController!");
        return;
    }

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


    // Obtain a printInfo so that we can set our printing defaults.
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    // This application produces General content that contains color.
    printInfo.outputType = UIPrintInfoOutputGeneral;
    // We'll use the URL as the job name
    printInfo.jobName = title;
    // Set duplex so that it is available if the printer supports it. We
    // are performing portrait printing so we want to duplex along the long edge.
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    // Use this printInfo for this print job.
    printController.printInfo = printInfo;

    // Be sure the page range controls are present for documents of > 1 page.
    printController.showsPageRange = YES;

    // This code uses a custom UIPrintPageRenderer so that it can draw a header and footer.
    MyPrintPageRenderer *myRenderer = [[MyPrintPageRenderer alloc] init];
    // The MyPrintPageRenderer class provides a jobtitle that it will label each page with.
    myRenderer.jobTitle = printInfo.jobName;
    // To draw the content of each page, a UIViewPrintFormatter is used.
    UIViewPrintFormatter *viewFormatter = [myWebView viewPrintFormatter];

#if SIMPLE_LAYOUT
    /*
     For the simple layout we simply set the header and footer height to the height of the
     text box containing the text content, plus some padding.

     To do a layout that takes into account the paper size, we need to do that 
     at a point where we know that size. The numberOfPages method of the UIPrintPageRenderer 
     gets the paper size and can perform any calculations related to deciding header and
     footer size based on the paper size. We'll do that when we aren't doing the simple 
     layout.
     */
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
    CGSize titleSize = [myRenderer.jobTitle sizeWithFont:font];
    myRenderer.headerHeight = myRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;
#endif
    [myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
    // Set our custom renderer as the printPageRenderer for the print job.
    printController.printPageRenderer = myRenderer;
    [myRenderer release];

    // The method we use presenting the printing UI depends on the type of 
    // UI idiom that is currently executing. Once we invoke one of these methods
    // to present the printing UI, our application's direct involvement in printing
    // is complete. Our custom printPageRenderer will have its methods invoked at the
    // appropriate time by UIKit.

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        if(orientationStyle == 1)
            [printController presentFromRect:CGRectMake(510, 150, 0, 0) inView:self.window animated:YES completionHandler:completionHandler];
        else
            [printController presentFromRect:CGRectMake(770, 165, 0, 0) inView:self.window animated:YES completionHandler:completionHandler];
    }
    else
        [printController presentAnimated:YES completionHandler:completionHandler];  // iPhone
}
(3) 在这里,您还需要为打印功能添加一个类文件:

MyPrintPageRenderer.h文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/*
  Setting SIMPLE_LAYOUT to 1 uses a layout that involves less application code and
  produces a layout where the web view content has margins that are relative to
  the imageable area of the paper.

  Setting SIMPLE_LAYOUT to 0 uses a layout that involves more computation and setup
  and produces a layout where the webview content is inset 1/2 from the edge of the 
  paper (assuming it can be without being clipped). See the comments in 
  MyPrintPageRenderer.m immediately after #if !SIMPLE_LAYOUT.
*/
#define SIMPLE_LAYOUT 1

// The point size to use for the height of the text in the
// header and footer.
#define HEADER_FOOTER_TEXT_HEIGHT     10

// The left edge of text in the header will be offset from the left 
// edge of the imageable area of the paper by HEADER_LEFT_TEXT_INSET.
#define HEADER_LEFT_TEXT_INSET        20

// The header and footer will be inset this much from the edge of the
// imageable area just to avoid butting up right against the edge that
// will be clipped.
#define HEADER_FOOTER_MARGIN_PADDING  5

// The right edge of text in the footer will be offset from the right 
// edge of the imageable area of the paper by FOOTER_RIGHT_TEXT_INSET.
#define FOOTER_RIGHT_TEXT_INSET       20

#if !SIMPLE_LAYOUT

// The header and footer content will be no closer than this distance
// from the web page content on the printed page.
#define MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT 10

// Enforce a minimum 1/2 inch margin on all sides.
#define MIN_MARGIN 36

#endif

@interface MyPrintPageRenderer : UIPrintPageRenderer {
  NSRange pageRange;
  NSString *jobTitle;
}

@property (readwrite, retain) NSString *jobTitle;

@end
#import "MyPrintPageRenderer.h"
#import <CoreGraphics/CoreGraphics.h>

@implementation MyPrintPageRenderer

@synthesize jobTitle;

#if !SIMPLE_LAYOUT
/*
 For the case where we are not doing SIMPLE_LAYOUT, this code does the following:
 1) Makes the minimum margin for the content of the content portion of the printout (i.e.
    the webpage) at least 1/4" away from the edge of the paper. If the hardware margins
    of the paper are greater than that, then the hardware margins will force the content
    margins to be as large as they allow.
 2) Because this format is relative to the edge of the sheet rather than the imageable area, we
    need to compute these values once we know the paper size and printableRect. This is known
    in the numberOfPages method and that is the reason this code overrides that method.
 3) Since the header and footer heights of a UIPrintFormatter plays a part in determining height of
    of the content area, this code computes those heights, taking into account that we want
    the minimum 1/4" margin on the content.
 4) This code also enforces a minimum distance (MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT)
    between the header and footer and the content area.
 5) Note that the insets used for the contentInsets property of a UIPrintFormatter are relative
    to the imageable area of the paper being printed upon. The header and footer height are also
    imposed relative to the edge of the top and bottom hardware margin. 
*/


// Compute an edge inset to produce the minimum margin
// based on the imageable area margin of the edge.
static inline CGFloat EdgeInset(CGFloat imageableAreaMargin)
{
  /*
    Since the offsets specified to a print formatter are relative 
    to printRect and we want our edges to be at least MIN_MARGIN
    from the edge of the sheet of paper, here we compute the necessary
    offset to achieve our margin. If the imageable area margin is
    larger than our MIN_MARGIN, we return an offset of zero which means
    that the imageable area margin will be used.
  */
  CGFloat val = MIN_MARGIN - imageableAreaMargin;
  return val > 0 ? val : 0;
}

// Compute a height for the header or footer, based on the margin for the edge
// in question and the height of the text being drawn.
static CGFloat HeaderFooterHeight(CGFloat imageableAreaMargin, CGFloat textHeight)
{
  /*
    Make the header and footer height provide for a minimum margin of MIN_MARGIN. We
    want the content to appear at least MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT from
    the header/footer text. If that requires a margin > MIN_MARGIN then we'll use that.
    Remember, the header/footer height returned needs to be relative to the edge of 
    the imageable area.
  */
  CGFloat headerFooterHeight = imageableAreaMargin + textHeight + 
        MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT + HEADER_FOOTER_MARGIN_PADDING;
  if(headerFooterHeight < MIN_MARGIN)
    headerFooterHeight = MIN_MARGIN - imageableAreaMargin;
  else {
    headerFooterHeight -= imageableAreaMargin;
  }

  return headerFooterHeight;
}

/*  Override numberOfPages so we can compute the values for our UIPrintFormatter based on the
    paper used for the print job. When this is called, self.paperRect and self.printableRect
    reflect the paper size and imageable area of the destination paper.
*/
- (NSInteger)numberOfPages
{
  // We only have one formatter so obtain it so we can set its paramters.
  UIPrintFormatter *myFormatter = (UIPrintFormatter *)[self.printFormatters objectAtIndex:0];

  // Compute insets so that margins are 1/2 inch from edge of sheet, or at
  // the edge of the imageable area if it is larger than that. The EdgeInset
  // function takes a margin for the edge being calculated.
  CGFloat leftInset = EdgeInset(self.printableRect.origin.x);
  CGFloat rightInset = EdgeInset(self.paperRect.size.width - CGRectGetMaxX(self.printableRect));

  // Top inset is only used if we want a different inset for the first page and we don't.
  // The bottom inset is never used by a viewFormatter.
  myFormatter.contentInsets = UIEdgeInsetsMake(0, leftInset, 0, rightInset);

  // Now compute what we want for the header size and footer size. These determine the
  // size and placement of the content height.

  // First compute the 
  UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
  // We'll use the same title height for the header and footer. This is the minimum height 
  // the footer can be. 
  CGFloat titleHeight = [self.jobTitle sizeWithFont:font].height;

  /*
    We want to calculate these heights so that the content top and bottom
    edges are a minimum distance from the edge of the sheet and are inset at least
    MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT from the header and footer.
  */
  self.headerHeight = HeaderFooterHeight(CGRectGetMinY(self.printableRect), titleHeight); 
  self.footerHeight = HeaderFooterHeight(self.paperRect.size.height - CGRectGetMaxY(self.printableRect), titleHeight); 

  // Just to be sure, never allow the content to go past our minimum margins for the content area.
  myFormatter.maximumContentWidth = self.paperRect.size.width - 2*MIN_MARGIN;
  myFormatter.maximumContentHeight = self.paperRect.size.height - 2*MIN_MARGIN;


  /*
      Let the superclass calculate the total number of pages. Since this UIPrintPageRenderer only uses a UIPrintFormatter,
      the superclass knows the number of pages based on the formatter metrics and the paper/printable rects. 

      Note that since this code only uses a single print formatter we could just as easily use myFormatter.pageCount 
      to obtain the total number of pages. But it would be more complex than that if we had multiple printformatters 
      for our job so we're using a more general approach here for illustration and it is correct for 1 or more formatters.
   */
  return [super numberOfPages];
}

#endif

/*
 Our pages don't have any intrinsic notion of page
 number; our footer will number the pages so that users
 know the order. So for us, we will always render the first
 page printed as page 1, even if the range is n-m.
 So we track which page in the range is the first index
 as well as the total length of our range.
*/
- (void)prepareForDrawingPages:(NSRange)range
{
  pageRange = range;
  [super prepareForDrawingPages:range];
}

- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect
{
  if(self.jobTitle){
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
    // Position the title starting from the left and inset slightly from the left edge of the imageable area.
    CGFloat startX = CGRectGetMinX(headerRect) + HEADER_LEFT_TEXT_INSET;
    // The header will always be a fixed amount inset from edge of the the imageable area of the paper.
    // The point passed to drawAtPoint is at the top-left of the text that will be drawn, hence
    // the starting point is the y coordinate of the top of the imageable area rect plus any padding.
    CGFloat startY = self.printableRect.origin.y + HEADER_FOOTER_MARGIN_PADDING; 
    CGPoint startPoint = CGPointMake(startX, startY);
    [self.jobTitle drawAtPoint:startPoint withFont: font];
  }
}


- (void)drawFooterForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)footerRect 
{
  NSString *localizedPageNumberString = NSLocalizedString(@"Page %d of %d", @"Localized Page Count String");
  // Compute the page numbers so that the first page in the range being 
  // printed is always page 1 of N, where N is the total number of pages.
  NSString *pageNumberString = [NSString stringWithFormat:localizedPageNumberString,
                  pageIndex+1 - pageRange.location, pageRange.length]; 
  UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT];
  CGSize pageNumSize = [pageNumberString sizeWithFont:font]; 
  // Position the page number string so that it ends inset by FOOTER_RIGHT_TEXT_INSET from the right edge
  // of the imageable area.
  CGFloat startX = CGRectGetMaxX(footerRect) - pageNumSize.width - FOOTER_RIGHT_TEXT_INSET; 
  // The footer will always be a fixed amount inset from the bottom edge of the imageable area of the paper.
  // The point passed to drawAtPoint is the top-left of the text that will be drawn, hence
  // the height needs to be subtracted from the starting point.
  CGFloat startY = CGRectGetMaxY(self.printableRect) - pageNumSize.height - HEADER_FOOTER_MARGIN_PADDING; 
  CGPoint startPoint = CGPointMake(startX, startY); 

  [pageNumberString drawAtPoint:startPoint withFont: font];
}

@end
#导入
#进口
/*
将SIMPLE_LAYOUT设置为1使用的布局涉及较少的应用程序代码和
生成一个布局,其中web视图内容的页边距相对于
纸张的可成像区域。
将SIMPLE_LAYOUT设置为0将使用涉及更多计算和设置的布局
并生成一个布局,其中webview内容从页面边缘插入1/2
纸张(假设可以不用剪裁)。请参阅中的注释
MyPrintPageRenderer.m紧跟在#if!简单的布局。
*/
#定义简单布局1
//用于文本中文本高度的点大小
//页眉和页脚。
#定义页眉\页脚\文本\高度10
//标题中文本的左边缘将从左侧偏移
//通过页眉\左\文本\插图显示纸张可成像区域的边缘。
#定义标题\左\文本\插图20
//页眉和页脚将从页面边缘插入这么多
//可成像区域,以避免正好撞到
//会被剪掉的。
#定义页眉\页脚\页边距\填充5
//页脚中文本的右边缘将从右侧偏移
//页脚\右\文本\插入的纸张可成像区域的边缘。
#定义页脚\右\文本\插图20
#如果!简单布局
//页眉和页脚内容的距离不会小于此距离
//从打印页面上的网页内容。
#定义最小页眉页脚距离内容10
//在所有侧面强制执行最小1/2英寸的余量。
#定义最小裕度36
#恩迪夫
@接口MyPrintPageRenderer:UIPrintPageRenderer{
NSRange页面范围;
NSString*职务名称;
}
@属性(读写、保留)NSString*作业标题;
@结束
MyPrintPageRenderer.m文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/*
  Setting SIMPLE_LAYOUT to 1 uses a layout that involves less application code and
  produces a layout where the web view content has margins that are relative to
  the imageable area of the paper.

  Setting SIMPLE_LAYOUT to 0 uses a layout that involves more computation and setup
  and produces a layout where the webview content is inset 1/2 from the edge of the 
  paper (assuming it can be without being clipped). See the comments in 
  MyPrintPageRenderer.m immediately after #if !SIMPLE_LAYOUT.
*/
#define SIMPLE_LAYOUT 1

// The point size to use for the height of the text in the
// header and footer.
#define HEADER_FOOTER_TEXT_HEIGHT     10

// The left edge of text in the header will be offset from the left 
// edge of the imageable area of the paper by HEADER_LEFT_TEXT_INSET.
#define HEADER_LEFT_TEXT_INSET        20

// The header and footer will be inset this much from the edge of the
// imageable area just to avoid butting up right against the edge that
// will be clipped.
#define HEADER_FOOTER_MARGIN_PADDING  5

// The right edge of text in the footer will be offset from the right 
// edge of the imageable area of the paper by FOOTER_RIGHT_TEXT_INSET.
#define FOOTER_RIGHT_TEXT_INSET       20

#if !SIMPLE_LAYOUT

// The header and footer content will be no closer than this distance
// from the web page content on the printed page.
#define MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT 10

// Enforce a minimum 1/2 inch margin on all sides.
#define MIN_MARGIN 36

#endif

@interface MyPrintPageRenderer : UIPrintPageRenderer {
  NSRange pageRange;
  NSString *jobTitle;
}

@property (readwrite, retain) NSString *jobTitle;

@end
#import "MyPrintPageRenderer.h"
#import <CoreGraphics/CoreGraphics.h>

@implementation MyPrintPageRenderer

@synthesize jobTitle;

#if !SIMPLE_LAYOUT
/*
 For the case where we are not doing SIMPLE_LAYOUT, this code does the following:
 1) Makes the minimum margin for the content of the content portion of the printout (i.e.
    the webpage) at least 1/4" away from the edge of the paper. If the hardware margins
    of the paper are greater than that, then the hardware margins will force the content
    margins to be as large as they allow.
 2) Because this format is relative to the edge of the sheet rather than the imageable area, we
    need to compute these values once we know the paper size and printableRect. This is known
    in the numberOfPages method and that is the reason this code overrides that method.
 3) Since the header and footer heights of a UIPrintFormatter plays a part in determining height of
    of the content area, this code computes those heights, taking into account that we want
    the minimum 1/4" margin on the content.
 4) This code also enforces a minimum distance (MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT)
    between the header and footer and the content area.
 5) Note that the insets used for the contentInsets property of a UIPrintFormatter are relative
    to the imageable area of the paper being printed upon. The header and footer height are also
    imposed relative to the edge of the top and bottom hardware margin. 
*/


// Compute an edge inset to produce the minimum margin
// based on the imageable area margin of the edge.
static inline CGFloat EdgeInset(CGFloat imageableAreaMargin)
{
  /*
    Since the offsets specified to a print formatter are relative 
    to printRect and we want our edges to be at least MIN_MARGIN
    from the edge of the sheet of paper, here we compute the necessary
    offset to achieve our margin. If the imageable area margin is
    larger than our MIN_MARGIN, we return an offset of zero which means
    that the imageable area margin will be used.
  */
  CGFloat val = MIN_MARGIN - imageableAreaMargin;
  return val > 0 ? val : 0;
}

// Compute a height for the header or footer, based on the margin for the edge
// in question and the height of the text being drawn.
static CGFloat HeaderFooterHeight(CGFloat imageableAreaMargin, CGFloat textHeight)
{
  /*
    Make the header and footer height provide for a minimum margin of MIN_MARGIN. We
    want the content to appear at least MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT from
    the header/footer text. If that requires a margin > MIN_MARGIN then we'll use that.
    Remember, the header/footer height returned needs to be relative to the edge of 
    the imageable area.
  */
  CGFloat headerFooterHeight = imageableAreaMargin + textHeight + 
        MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT + HEADER_FOOTER_MARGIN_PADDING;
  if(headerFooterHeight < MIN_MARGIN)
    headerFooterHeight = MIN_MARGIN - imageableAreaMargin;
  else {
    headerFooterHeight -= imageableAreaMargin;
  }

  return headerFooterHeight;
}

/*  Override numberOfPages so we can compute the values for our UIPrintFormatter based on the
    paper used for the print job. When this is called, self.paperRect and self.printableRect
    reflect the paper size and imageable area of the destination paper.
*/
- (NSInteger)numberOfPages
{
  // We only have one formatter so obtain it so we can set its paramters.
  UIPrintFormatter *myFormatter = (UIPrintFormatter *)[self.printFormatters objectAtIndex:0];

  // Compute insets so that margins are 1/2 inch from edge of sheet, or at
  // the edge of the imageable area if it is larger than that. The EdgeInset
  // function takes a margin for the edge being calculated.
  CGFloat leftInset = EdgeInset(self.printableRect.origin.x);
  CGFloat rightInset = EdgeInset(self.paperRect.size.width - CGRectGetMaxX(self.printableRect));

  // Top inset is only used if we want a different inset for the first page and we don't.
  // The bottom inset is never used by a viewFormatter.
  myFormatter.contentInsets = UIEdgeInsetsMake(0, leftInset, 0, rightInset);

  // Now compute what we want for the header size and footer size. These determine the
  // size and placement of the content height.

  // First compute the 
  UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
  // We'll use the same title height for the header and footer. This is the minimum height 
  // the footer can be. 
  CGFloat titleHeight = [self.jobTitle sizeWithFont:font].height;

  /*
    We want to calculate these heights so that the content top and bottom
    edges are a minimum distance from the edge of the sheet and are inset at least
    MIN_HEADER_FOOTER_DISTANCE_FROM_CONTENT from the header and footer.
  */
  self.headerHeight = HeaderFooterHeight(CGRectGetMinY(self.printableRect), titleHeight); 
  self.footerHeight = HeaderFooterHeight(self.paperRect.size.height - CGRectGetMaxY(self.printableRect), titleHeight); 

  // Just to be sure, never allow the content to go past our minimum margins for the content area.
  myFormatter.maximumContentWidth = self.paperRect.size.width - 2*MIN_MARGIN;
  myFormatter.maximumContentHeight = self.paperRect.size.height - 2*MIN_MARGIN;


  /*
      Let the superclass calculate the total number of pages. Since this UIPrintPageRenderer only uses a UIPrintFormatter,
      the superclass knows the number of pages based on the formatter metrics and the paper/printable rects. 

      Note that since this code only uses a single print formatter we could just as easily use myFormatter.pageCount 
      to obtain the total number of pages. But it would be more complex than that if we had multiple printformatters 
      for our job so we're using a more general approach here for illustration and it is correct for 1 or more formatters.
   */
  return [super numberOfPages];
}

#endif

/*
 Our pages don't have any intrinsic notion of page
 number; our footer will number the pages so that users
 know the order. So for us, we will always render the first
 page printed as page 1, even if the range is n-m.
 So we track which page in the range is the first index
 as well as the total length of our range.
*/
- (void)prepareForDrawingPages:(NSRange)range
{
  pageRange = range;
  [super prepareForDrawingPages:range];
}

- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect
{
  if(self.jobTitle){
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
    // Position the title starting from the left and inset slightly from the left edge of the imageable area.
    CGFloat startX = CGRectGetMinX(headerRect) + HEADER_LEFT_TEXT_INSET;
    // The header will always be a fixed amount inset from edge of the the imageable area of the paper.
    // The point passed to drawAtPoint is at the top-left of the text that will be drawn, hence
    // the starting point is the y coordinate of the top of the imageable area rect plus any padding.
    CGFloat startY = self.printableRect.origin.y + HEADER_FOOTER_MARGIN_PADDING; 
    CGPoint startPoint = CGPointMake(startX, startY);
    [self.jobTitle drawAtPoint:startPoint withFont: font];
  }
}


- (void)drawFooterForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)footerRect 
{
  NSString *localizedPageNumberString = NSLocalizedString(@"Page %d of %d", @"Localized Page Count String");
  // Compute the page numbers so that the first page in the range being 
  // printed is always page 1 of N, where N is the total number of pages.
  NSString *pageNumberString = [NSString stringWithFormat:localizedPageNumberString,
                  pageIndex+1 - pageRange.location, pageRange.length]; 
  UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT];
  CGSize pageNumSize = [pageNumberString sizeWithFont:font]; 
  // Position the page number string so that it ends inset by FOOTER_RIGHT_TEXT_INSET from the right edge
  // of the imageable area.
  CGFloat startX = CGRectGetMaxX(footerRect) - pageNumSize.width - FOOTER_RIGHT_TEXT_INSET; 
  // The footer will always be a fixed amount inset from the bottom edge of the imageable area of the paper.
  // The point passed to drawAtPoint is the top-left of the text that will be drawn, hence
  // the height needs to be subtracted from the starting point.
  CGFloat startY = CGRectGetMaxY(self.printableRect) - pageNumSize.height - HEADER_FOOTER_MARGIN_PADDING; 
  CGPoint startPoint = CGPointMake(startX, startY); 

  [pageNumberString drawAtPoint:startPoint withFont: font];
}

@end
#导入“MyPrintPageRenderer.h”
#进口
@MyPrintPageRenderer的实现
@综合职称;
#如果!简单布局
/*
对于不进行简单_布局的情况,此代码执行以下操作:
1) 为打印输出内容部分的内容设置最小边距(即。
网页)距离纸张边缘至少1/4英寸。如果硬件边距
如果纸张的厚度大于此值,则硬件的边距将强制内容
利润应尽可能大。
2) 因为此格式是相对于图纸的边缘而不是可成像区域,所以我们
一旦我们知道纸张大小和可打印矩形,就需要计算这些值。这是已知的
在numberOfPages方法中,这就是此代码重写该方法的原因。
3) 因为UIPrintFormatter的页眉和页脚高度在确定
对于内容区域,此代码计算这些高度,并考虑我们想要的高度
内容上的最小1/4“边距。
4) 此代码还强制执行最小距离(最小页眉与页脚与内容之间的距离)
在页眉、页脚和内容区域之间。
5) 请注意,用于UIPrintFormatter的contentInsets属性的插入是相对的
打印到要打印的纸张的可成像区域。页眉和页脚的高度也相同
相对于顶部和底部硬件边缘施加。
*/
//计算边插入以生成最小边距
//基于边缘的可成像区域边界。
静态内嵌CGFloat EdgeInset(CGFloat imageableAreaMargin)
{
/*
因为指定给打印格式化程序的偏移量是相对的
我们希望我们的边至少是最小的
从这张纸的边缘,我们计算出必要的
偏移以获得我们的边距。如果可成像区域边距为
大于我们的最小裕度,我们返回零的偏移量,这意味着
将使用可成像区域边距。
*/
CGFloat val=最小裕度-可成像区域裕度;
返回val>0?val:0;
}
//根据边的边距计算页眉或页脚的高度
//以及正在绘制的文本的高度。
静态CGFloat磁头高度(CGFloat imageableAreaMargin,CGFloat textHeight)
{
/*
使页眉和页脚高度提供最小页边空白。我们
希望内容至少显示在距内容的最小\u页眉\u页脚\u距离\u
页眉/页脚文本。如果需要边距>最小边距,则我们将使用它。
请记住,返回的页眉/页脚高度需要相对于页眉的边缘
可成像区域。
*/
CGFloat headerFooterHeight=可成像区域边距+文本高度+
最小页眉页脚距内容+页眉页脚边距填充;
if(水头高度<最小裕度)