2

iPad アプリケーションで UIView から pdf を作成しています。サイズは768 * 2000です。pdfを作成すると、同じサイズで作成され、すべてのコンテンツが1ページに表示されます。そのため、iPadから印刷すると問題に直面しています。私はpdfを作成するために次のコードを使用しています:-

-(void)drawPdf:(UIView *)previewView{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
    //CGRect tempRect = CGRectMake(0, 0, 768, 1068);
    CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
    CGContextBeginPage (pdfContext,nil); // 6

    //turn PDF upsidedown

    CGAffineTransform transform = CGAffineTransformIdentity;    
    transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    CGContextConcatCTM(pdfContext, transform);

    //Draw view into PDF
    [previewView.layer renderInContext:pdfContext]; 
    CGContextEndPage (pdfContext);// 8
    CGContextRelease (pdfContext);  
}

//Create empty PDF context on iPhone for later randering in it

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{

    CGContextRef myOutContext = NULL;

    CFURLRef url;

    url = CFURLCreateWithFileSystemPath (NULL, // 1

                                     path,

                                     kCFURLPOSIXPathStyle,

                                     false);

    if (url != NULL) {

        myOutContext = CGPDFContextCreateWithURL (url,// 2

                                              &inMediaBox,                                                NULL);        
        CFRelease(url);// 3     
    }   
    return myOutContext;// 4    
} 

どうすればPDFのサイズを小さくでき、複数のページがありますか?

前もって感謝します。

4

2 に答える 2

0

「iOS用の描画および印刷ガイド」の例を参照してください

https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

基本的に、リスト4-1のコード例では、do whileループがあり、ループ内で新しいPDFページがどのように開始されるかに注意してください。

..。

// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

..。

現在のメソッドでは、ページ開始メソッドの1つを1回だけ呼び出したため、ページは1つしかありません。

于 2011-05-30T20:38:43.843 に答える
0

作成する新しい PDF ページごとに UIGraphicsBeginPDFPage を呼び出す必要があります。可変高さの UIView があると仮定すると、実行時に必要な数の PDF ページに分割する方法は次のとおりです。

NSInteger pageHeight = 792; // Standard page height - adjust as needed
NSInteger pageWidth = 612; // Standard page width - adjust as needed

/* CREATE PDF */
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0,0,pageWidth,pageHeight), nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
for (int page=0; pageHeight * page < theView.frame.size.height; page++)
{
    UIGraphicsBeginPDFPage();
    CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
    [theView.layer renderInContext:pdfContext];
}

UIGraphicsEndPDFContext();
于 2014-09-08T04:36:36.877 に答える