Как я могу написать контекст для рисования многостраничного PDF? - PullRequest
2 голосов
/ 02 августа 2009

Я рисую PDF со следующим кодом:

CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;

CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename,
                                  kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
                                     kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for 'signing' the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
//CGContextSetLineCap(pdfContext, kCGLineCapButt);

// Done creating our PDF Context, now it's time to draw to it

CGContextBeginPage (pdfContext, &pageRect);

Но когда вызов идет, чтобы начать метод страницы, он просто рисует одну страницу. Как я могу объявить правильный контекст, чтобы он мог рисовать несколько страниц?

Вот что у меня есть:

-(void)createPDFFileWithRect: (CGRect) pageRect andFileName:(const char*)filename
{

    // This code block sets up our PDF Context so that we can draw to it
    CGPDFContextCreateWithURL(url,
                              ((0, 0), (1000, 1000)), nil);
    CGContextRef pdfContext;
    CFStringRef path;
    CFURLRef url;



    CFMutableDictionaryRef myDictionary = NULL;
    // Create a CFString from the filename we provide to this method when we call it
    path = CFStringCreateWithCString (NULL, filename,
                                      kCFStringEncodingUTF8);
    // Create a CFURL using the CFString we just defined
    url = CFURLCreateWithFileSystemPath (NULL, path,
                                         kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    // This dictionary contains extra options mostly for 'signing' the PDF
    myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    // Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
    // Cleanup our mess
    CFRelease(myDictionary);
    CFRelease(url);
    //CGContextSetLineCap(pdfContext, kCGLineCapButt);

    // Done creating our PDF Context, now it's time to draw to it

    // Starts our first page
    //for(int i =0;i<2;i++)
    //{ 
    CGContextBeginPage (pdfContext, &pageRect);

        //CGContextRef pdfContext1;

    //CGContextBeginPage (pdfContext, &pageRect);
    //}
    // Draws a black rectangle around the page inset by 50 on all sides
    CGContextStrokeRect(pdfContext, CGRectMake(50, 50, 500,700));



    CGContextShowTextAtPoint (pdfContext, 60, 699, text, strlen(text));
    // End text

    // We are done drawing to this page, let's end it
    // We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
    CGContextEndPage (pdfContext);
    // cpde to draw a new page 
//  CGContextBeginPage (pdfContext, &pageRect);
    // We are done with our context now, so we release it
    CGContextRelease (pdfContext);
}

1 Ответ

1 голос
/ 02 августа 2009

Извините, я не очень знаком с PDF-файлами, так что это может быть не очень полезно.

У меня есть небольшое, еще не опубликованное черновое приложение PDF, и я делаю рисунок с этим (немного упрощенным):

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, bounds, 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);

Я не вижу CGContextDrawPDFPage в вашем коде. Используем ли мы два разных метода, вы забыли включить часть своего кода или что-то еще?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...