2012-03-10 44 views

Trả lời

5

Có bạn có thể làm điều đó. Vui lòng thực hiện theo mã bên dưới

//Open a pdf context for the single file 
UIGraphicsBeginPDFContextToFile(oldFile, paperSize, nil); 

//Run a loop to the number of pages you want 
for (pageNumber = 1; pageNumber <= count; pageNumber++) 
{ 
//Open a pdf page context 
UIGraphicsBeginPDFPageWithInfo(paperSize, nil); 

//Get graphics context to draw the page 
CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

//Flip and scale context to draw the pdf correctly 
CGContextTranslateCTM(currentContext, 0, paperSize.size.height); 
CGContextScaleCTM(currentContext, 1.0, -1.0); 

//Get document access of the pdf from which you want a page 
CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) newUrl); 

//Get the page you want 
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber); 

//Drawing the page 
CGContextDrawPDFPage (currentContext, newPage); 

//Clean up 
newPage = nil;  
CGPDFDocumentRelease(newDocument); 
newDocument = nil; 
newUrl = nil; 

} 

UIGraphicsEndPDFContext(); 

Vì vậy, bạn phải viết điều kiện cần thiết để lấy các trang thích hợp từ pdf thích hợp trước khi vẽ trang. Bạn đã tạo một pdf từ nhiều nguồn!

+0

tôi đã kết hợp pdf của tôi như đã đề cập ở trên nhưng với vấn đề nsdata.the là chỉ có trang đầu tiên được in trong tài liệu kết hợp cuối cùng.đây là câu hỏi tôi đã đăng trong http://stackoverflow.com/questions/9668047/combining- multiple-pdf-documents-into-single-document-not-working – sujith1406

+0

Câu trả lời đã được đăng lên liên kết câu hỏi này là chính xác. – cocoakomali

+0

@cocoakomali: bạn có thể vui lòng trợ giúp về việc làm theo SO Tệp được hợp nhất không hiển thị http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge- in-ios –

1

Dùng thử API 2D của Quartz. Nó có hỗ trợ rất tốt cho việc đọc các tập tin PDF.

  1. Bằng cách nhìn vào các API bạn sẽ có thể đọc tất cả các đầu vào PDF tài liệu
  2. get PDF trang cho họ
  3. tạo ra một bối cảnh PDF cho tài liệu mới của bạn
  4. Trang bốc thăm PDF trong PDF Context

Xin lưu ý rằng mã vẽ trang PDF phải nằm giữa trang bắt đầu và kết thúc cuộc gọi trang

CGPDFDocumentCreateWithURL 
CGContextBeginPage 
CGPDFDocumentGetPage 
CGPDFContextCreateWithURL 
CGContextDrawPDFPage 
CGContextEndPage 

Kiểm tra tài liệu của Apple cho

[CGContext][1] 
[CGPDFDocument][2] 
[CGPDFPage][3] 

Check-out nếu nó giúp, hoặc cho tôi biết nếu bạn cần một số mẫu mã giả.

5

Dưới đây là một thói quen mà tôi đã viết để có một url cho một pdf và gắn nó vào một bối cảnh mà bạn đã tạo (để bạn có thể gọi nó là cho nhiều tập tin này và gắn nó tất cả):

- (void)appendPdfAtURL:(NSURL *)pdfURL toContext:(CGContextRef)pdfDestinationContext { 
    CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfURL); 
    if (pdfDoc) { 
     size_t numPages = CGPDFDocumentGetNumberOfPages(pdfDoc); 
     if (numPages > 0) { 
      // Loop through each page in the source file 
      for (size_t i = 1; i <= numPages; i++) { 
       CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, i); 
       if (pdfPage) { 
        // Get the page size 
        CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); 

        // Copy the page from the source file to the context 
        CGContextBeginPage(pdfDestinationContext, &pdfCropBoxRect); 
        CGContextDrawPDFPage(pdfDestinationContext, pdfPage); 
       } 
      } 
     } 

     // Close the source file 
     CGPDFDocumentRelease(pdfDoc); 
    } 
} 
+0

Lưu ý rằng vì bạn đang viết từ pdf sang pdf mà bạn không phải dịch/mở rộng nó trước khi viết nó. – lnafziger

Các vấn đề liên quan