1

在这里,我正在打印大小为“pageSize = CGSizeMake(640, 832);”的 pdf。这个尺寸比 A4 尺寸的页面大。所以我会截掉一些文本(意味着它不会打印整个页面)。

在使用 MAC 打印相同的 pdf 时,它将在选项的帮助下打印整个页面(缩放以适应)。所以任何人都可以帮助我摆脱这个问题.. IOS sdk中是否有任何选项可以适应规模。

这是我的代码..

-(void)printItem
{

NSArray *aArrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;
NSString *aStr = [[aArrPaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"PropertyReport_%d.pdf",self.propertyId]];

  // NSString *aStr = [[NSBundle mainBundle] pathForResource:@"TRADUZIONE HELP SECTIONS REV2" ofType:@"pdf"];
NSURL *url=[[NSURL alloc] initFileURLWithPath:aStr];
NSData *data=[[NSData alloc] initWithContentsOfURL:url];
printController = [UIPrintInteractionController sharedPrintController];
if(printController && [UIPrintInteractionController canPrintData:data])
{
    printController.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    //printInfo.jobName = [NSString stringWithFormat:@"New Image"];
    printInfo.duplex = UIPrintInfoDuplexLongEdge;

    printController.printInfo = printInfo;
    printController.showsPageRange = YES;

    printController.printingItem = data;




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

  //  aWebViewPDF.hidden=FALSE;
    [printController presentAnimated:YES completionHandler:completionHandler];
}

}

谢谢!

4

2 回答 2

2

听起来很奇怪,试试:

printController.showsPageRange = NO;

这似乎启用了自动缩放,但有时会在作业结束时打印一个额外的空白页。AirPrint 基本上是巫术。

于 2013-04-24T11:19:42.933 回答
0

您只有一个选择是从 PDF 的scale down所有页面中选择然后打印。我已经完成了所有页面并将其缩小到要求:ratioprinted formatpdf into images

NSString *strFilePath = [[NSBundle mainBundle] pathForResource:@"pdftouch" ofType:@"pdf"];
pic.printingItems = [[self allPagesImageFromPDF:strFilePath] copy];

添加所有三种方法:

Convert all PDF pages into UIImages

-(NSMutableArray *)allPagesImageFromPDF:(NSString *)strPath
{
  NSMutableArray *arrImages = [NSMutableArray array];
  CGPDFDocumentRef pdf  = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:strPath]);
  NSInteger numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);
  for (NSInteger pageIndex = 1; pageIndex <= numberOfPages;
     pageIndex++)
  {
    UIImage *img = [self getThumbForPage:pdf pageIndex:pageIndex];
    [arrImages addObject:[self resizedImage:img]];
  }
  return arrImages;
}

Resize images

-(UIImage *)resizedImage:(UIImage *)image
{
  UIGraphicsBeginImageContext(CGSizeMake(612, 792)); //A4 size
  [image drawInRect:CGRectMake(0,0,612, 792)]; //A4 size
  UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return resizedImg;
}

Get UIImage from CGPDFPageRef

-(UIImage *)getThumbForPage:(CGPDFDocumentRef)pdfRef pageIndex:(int)page_number{

  // Get the page
  CGPDFPageRef myPageRef = CGPDFDocumentGetPage(pdfRef, page_number);
  // Changed this line for the line above which is a generic line

  CGRect pageRect = CGPDFPageGetBoxRect(myPageRef, kCGPDFMediaBox);
  //CGFloat pdfScale = width/pageRect.size.width;
  //pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
  pageRect.origin = CGPointZero;

  UIGraphicsBeginImageContext(pageRect.size);

  CGContextRef context = UIGraphicsGetCurrentContext();

  // White BG
  CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
  CGContextFillRect(context,pageRect);

  CGContextSaveGState(context);

  // Next 3 lines makes the rotations so that the page look in the right direction

  CGContextTranslateCTM(context, 0.0, pageRect.size.height);
  CGContextScaleCTM(context, 1.0, -1.0);
  CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFMediaBox, pageRect, 0, true));

  CGContextDrawPDFPage(context, myPageRef);
  CGContextRestoreGState(context);

  UIImage *thm = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();
  return thm;  
}

编辑:参考打印-uiimage-using-airprint-causes-cut-off-content

于 2012-11-01T10:59:35.270 回答