views:

433

answers:

3

Hi all,

In the process of updating my iPad app I've been attempting to draw a page from an existing PDF document into a Core Graphics context then save it as a new PDF, but am having difficulty getting the text to display properly. Images in the newly-created PDF look great, but text rarely appears correctly: more often that not it appears white/invisible or garbled. When the text is invisible, I am still able to to select where it -should- be and copy/paste correctly into a text editor. Is this an issue related to the limited number of fonts available on the iPad?

My code is as follows:

CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumberToRetrieve);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

UIGraphicsBeginPDFContextToFile(pathToFile, pageRect, nil);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextBeginPage(context, NULL);

// I don't think this line is necessary, but I have tried both with and without it.
CGContextSetTextDrawingMode (context, kCGTextFill);

CGContextDrawPDFPage(context, page);

CGContextEndPage(context);
UIGraphicsEndPDFContext();
CGDataProviderRelease(dataProvider);
CGPDFDocumentRelease(document);

If anyone has any suggestions I would greatly appreciate hearing them.

Thanks for your time.

Rob

A: 

Do you want to render on the screen? I don't see the need for UIGraphicsBeginPDFContextToFile? However, to render on the screen, you can use something like this:

pageReference = CGPDFDocumentGetPage(pdfReference, page);

  CGContextRef context = UIGraphicsGetCurrentContext();
  @try {
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, scale, -scale);

    CGContextSaveGState(context);
    @try {
      CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageReference, kCGPDFCropBox, self.bounds, 0, true);
      CGContextConcatCTM(context, pdfTransform);
      CGContextDrawPDFPage(context, pageReference);
    }
    @finally {
      CGContextRestoreGState(context);
    }
  }
  @finally {
    UIGraphicsEndImageContext();
  }
Fuggly
"In the process of updating my iPad app I've been attempting to draw a page from an existing PDF document into a Core Graphics context then save it as a new PDF,[...]" <-- That's the very first line of his question. Not sure how it's possible to miss that... :]
Kalle
That's actually quiet easy at 8:15 in the morning for me <g>.Seriously, did you have a look at the "Quartz 2d Programming Guide: PDF Document creation, viewing and transformation" document? If not, there's a code sample on how to create a document, render data to its context and save that file then.
Fuggly
+1  A: 

Drawing into an image context does not pose a problem (text displays correctly).

What I am trying to do is create a -new- PDF file containing just a few pages from the original PDF. It seems that text does not draw correctly into the new file for some reason.

The information is there (I can select text by 'guessing' where it should be in Preview) but it doesn't render. I assume CGContextDrawPDFPage writes the string to the PDF file, but doesn't draw it because it doesn't know what the characters of that font 'look like'?

I thought the point of embedded fonts in PDFs was that programs would be able to perform these sorts of manipulations even if that font wasn't installed on the system (in this case, the iPad). Is this a limitation of the format, or the Quartz framework?

Rob
A: 

Hi,

You got any solution for the text problem???

I am also facing the same issue while creating a pdf using an existing pdf.

If you found any solution please share.

Thank you..

chaitanya
Sorry, I'm yet to find a solution.PDF support in iOS is poor. Simply using CGContextDrawPDFPage() can crash an app with no warning whatsoever if the page contains certain elements. I've seen PDFs that will reliably crash both Mail and GoodReader when a certain page is scrolled to. Those same PDFs render perfectly in any desktop application.
Rob