0

I created a PDF context and the context is saved in the file.
As the existing pdf context to draw in a different context?

pdfContext -> viewContext

my code:

void myCreatePDFFile(CGRect pageRect, const char *fileName){

CGContextRef pdfContext;
CFStringRef pathPDF;
CFURLRef urlPDF;
CFDataRef dataPDF;
CFMutableDictionaryRef myDictionary;
CFMutableDictionaryRef pageDictionary;

// Creates a CFString object from a filename passed to the function MyCreatePDFFile.
pathPDF = CFStringCreateWithCString(kCFAllocatorDefault,
                                    fileName,
                                    kCFStringEncodingUTF8);

// Creates a CFURL object from the CFString object.
urlPDF = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                       pathPDF,
                                       kCFURLPOSIXPathStyle,
                                       false);

CFRelease(pathPDF);

// Creates an empty CFDictionary object to hold metadata. The next two lines add a title and creator. You can add as many key-value pairs as you’d like using the function CFDictionarySetValue. For more information on creating dictionaries, see CFDictionary Reference .
myDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                         0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("TESTer"));

/* Creates a PDF graphics context, passing three parameters:
 ● A CFURL object that specifies a location for the PDF data.
 ● A pointer to a rectangle that defines the default size and location of the PDF page. The origin of the rectangle is typically (0, 0). Quartz uses this rectangle as the default bounds of the page media box. If you pass NULL, Quartz uses a default page size of 8.5 by 11 inches (612 by 792 points).
 ● A CFDictionary object that contains PDF metadata. Pass NULL if you don’t have metadata to add. You can use the CFDictionary object to specify output intent options—intent subtype, condition, condition identifier, registry name, destination output profile, and a human-readable text string that contains additional information or comments about the intended target device or production condition. For more information about output intent options, see CGPDFContext Reference .
 */
pdfContext = CGPDFContextCreateWithURL(urlPDF, &pageRect, myDictionary);

CFRelease(myDictionary);
CFRelease(urlPDF);

// Creates a CFDictionary object to hold the page boxes for the PDF page. This example sets the media box.
pageDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                           0,
                                           &kCFTypeDictionaryKeyCallBacks,
                                           &kCFTypeDictionaryValueCallBacks);

dataPDF = CFDataCreate(kCFAllocatorDefault,
                       (const UInt8 *)&pageRect,
                        sizeof(pageRect));

CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, dataPDF);

// Signals the start of a page. When you use a graphics context that supports multiple pages (such as PDF), you call the function CGPDFContextBeginPage together with CGPDFContextEndPage to delineate the page boundaries in the output. Each page must be bracketed by calls to CGPDFContextBeginPage and CGPDFContextEndPage. Quartz ignores all drawing operations performed outside a page boundary in a page-based context.
CGPDFContextBeginPage(pdfContext, NULL);

// draw square to pdf context
CGContextSetRGBFillColor(pdfContext, 1, 0, 0, 1);
CGContextFillRect(pdfContext, CGRectMake(25, 25, 50, 50));

// Calls an application-defined function to draw content to the PDF context. You supply your drawing routine here.

How to draw PDF context? pdfContext -> viewContext?

// Signals the end of a page in a page-based graphics context.
CGPDFContextEndPage(pdfContext);

// Releases the PDF context.
CGContextRelease(pdfContext);

// Releases the page dictionary.
CFRelease(pageDictionary);
CFRelease(dataPDF);
}
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
TESTer
  • 55
  • 6
  • I'm not sure what you mean here. Can you post your code? – Robert Jul 05 '13 at 14:25
  • 1
    “draw a PDF context in another context” doesn't make sense. Contexts are drawing destinations; you can only draw *into* a context. A context isn't a thing that can be drawn somewhere else. So, do you want to take the PDF output that the PDF context generated and draw that content into another context, or do you want to be able to substitute the other context for the PDF context and skip the PDF step entirely? – Peter Hosey Jul 06 '13 at 23:06
  • I want to take the PDF output that the PDF context generated and draw that content into another context. For example PDF context to NSView context. – TESTer Jul 07 '13 at 08:58

1 Answers1

1

The simplest way to display a single-page PDF is to create an NSImage of it, then either put it into an image view (borderless or otherwise) or draw that image into some portion, or the entirety, of your view's bounds.

If you had a multi-page PDF and wanted to show that, and have zoom controls and other such affordances, then you would want to use the PDF Kit framework. It's part of the OS X SDK; just add it to your target within Xcode, then add a PDFView to your nib or create one in code, then create a PDFDocument from the URL and hand the PDFDocument to the PDFView.

Alternatively, you could show the PDF in a Quick Look preview panel.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • You can show more details single PDF context to NSImage and multi-page PDF context to PDFView? – TESTer Jul 08 '13 at 18:11
  • @TESTer: The PDF context is only how you generate a PDF document. You've already done that part; nothing else you need to do involves a context. You need only pass the same URL you used with the PDF context to NSImage or PDFDocument. – Peter Hosey Jul 08 '13 at 19:46
  • I wanted to exclude URL and continue to work with the using memory (context) but not found similar solutions without URL and using as the source PDF context. Wanted to get contextPDF -> contextView instead contextPDF -> URL -> contextView. As I understand it, such a solution does not exist, as I want? – TESTer Jul 09 '13 at 06:11
  • @TESTer: Again, you can't draw a context into something else—contexts are destinations, not sources. You can, however, create a context that outputs to an NS/CFMutableData object, then create the NSImage or PDFDocument from the data. It works the same way, just with a data object instead of a file. – Peter Hosey Jul 09 '13 at 07:08