2013-07-05 116 views
0

我創建了一個PDF上下文,上下文保存在文件中。
由於現有的pdf上下文在不同的上下文中繪製?如何在另一個上下文中繪製pdf上下文?

pdfContext - > viewContext

我的代碼:

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. 

如何畫PDF語境?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); 
} 
+0

我不確定你在這裏是什麼意思。你能發佈你的代碼嗎? – Robert

+1

「在另一個上下文中繪製PDF上下文」沒有任何意義。上下文是繪製目的地;你只能將*繪製到上下文中。上下文不是可以在其他地方繪製的事物。那麼,您是否想要生成PDF上下文生成的PDF輸出並將該內容繪製到另一個上下文中,還是希望能夠用PDF上下文替換其他上下文並完全跳過PDF步驟? –

+0

我想採用PDF上下文生成的PDF輸出並將該內容繪製到另一個上下文中。例如PDF上下文到NSView上下文。 – TESTer

回答

1

顯示單頁的PDF的最簡單的方法是創建一個NSImage中的它,然後要麼把它轉換成圖像視圖(無邊界或其他)或繪製圖像劃分成一些部分,或者全部,你的觀點的界限。

如果你有一個多頁的PDF,並想顯示,並有縮放控件和其他這樣的可供性​​,那麼你會想使用PDF套件框架。它是OS X SDK的一部分;只需將其添加到Xcode中的目標中,然後將PDFView添加到您的筆尖或在代碼中創建一個PDFView,然後從該URL創建PDFDocument並將PDFDocument交給PDFView。

或者,您可以在Quick Look preview panel中顯示PDF。

+0

您可以向PDFView顯示更多詳細信息單個PDF上下文到NSImage和多頁PDF上下文嗎? – TESTer

+0

@TESTer:PDF上下文僅僅是您生成PDF文檔的方式。你已經完成了這部分;沒有其他你需要做的事情涉及上下文。您只需將與PDF上下文一起使用的URL傳遞給NSImage或PDFDocument。 –

+0

我想排除URL並繼續使用使用的內存(上下文),但沒有找到類似的解決方案,沒有URL並將其用作源PDF上下文。想要獲取contextPDF - > contextView,而不是contextPDF - > URL - > contextView。據我瞭解,這種解決方案不存在,因爲我想? – TESTer

相關問題