0

I am converting a pdf file to png image so I can add text to the png then convert back to a pdf. I am using the code below to convert to the png. This is resulting in a substantial decrease in the quality of the appearance. Is there anything that can be done to prevent or minimize the loss of quality?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"];

CFStringRef path;
CFURLRef url;

path = CFStringCreateWithCString (NULL, [writeableDBPath UTF8String], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);

CGPDFDocumentRef myDocument;

myDocument = CGPDFDocumentCreateWithURL(url);

UIGraphicsBeginImageContext(CGSizeMake(612,792));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 10, 792); //596,842 //640x960, 
CGContextScaleCTM(currentContext, 1.0, -1.0); // make sure the page is the right way up

CGPDFPageRef page = CGPDFDocumentGetPage (myDocument, 1); // first page of PDF is page 1 (not zero)
CGContextDrawPDFPage (currentContext, page);  // draws the page in the graphics context

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString* imagePath = [documentsDirectory stringByAppendingPathComponent: @"test.png"];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];
Mike
  • 235
  • 2
  • 8
  • 25

1 Answers1

6

You can try increasing the size of the image context you are rendering to:

UIGraphicsBeginImageContext(CGSizeMake(1224,1584));
...
CGContextTranslateCTM(currentContext, 10, 1584);

But you really probably should be using a PDF context like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"];

CFStringRef path;
CFURLRef url;

path = CFStringCreateWithCString (NULL, [writeableDBPath UTF8String], kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);

CGPDFDocumentRef myDocument;
myDocument = CGPDFDocumentCreateWithURL(url);

// Create URL for PDF file
NSString *filename = @"sample-out.pdf";
NSURL *outputURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]];

// Create PDF context
CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)outputURL, NULL, NULL);
CGPDFContextBeginPage(pdfContext, NULL);
UIGraphicsPushContext(pdfContext);

// Flip coordinate system
CGRect bounds = CGContextGetClipBoundingBox(pdfContext);
CGContextScaleCTM(pdfContext, 1.0, -1.0);
CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height);

CGContextDrawPDFPage (UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage (myDocument, 1));
// Drawing commands
[@"Hello World!" drawAtPoint:CGPointMake(0, 0) withFont:[UIFont boldSystemFontOfSize:72.0f]];
// Clean up
UIGraphicsPopContext();
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
Community
  • 1
  • 1
Ric
  • 8,615
  • 3
  • 17
  • 21
  • Thank you. I tried forever to add text using pdf context but I concluded that is wasn't possible. Am I mistaken? – Mike Mar 16 '13 at 22:07
  • Have you seen [this Stack Overflow answer](http://stackoverflow.com/a/4987139/2089625)? – Ric Mar 16 '13 at 22:09
  • Yea I've seen that and tested it. It works if you are creating a brand new pdf but I am looking to add text to an existing pdf. Maybe I am doing something wrong? – Mike Mar 16 '13 at 22:15
  • Did you draw the page you have to your new page before drawing the text? `CGContextDrawPDFPage (UIGraphicsGetCurrentContext(), CGPDFDocumentGetPage (myDocument, 1));` with the document from `myDocument = CGPDFDocumentCreateWithURL(url);` – Ric Mar 16 '13 at 22:21
  • I put this line right after CGContextDrawPDFPage: [@"Hello World!" drawAtPoint:CGPointMake(300, 300) withFont:[UIFont boldSystemFontOfSize:172.0f]]; – Mike Mar 16 '13 at 22:25
  • What happened? Is a PDF created that is the same as the original? Is there any text? Is the new PDF blank? – Ric Mar 16 '13 at 22:28
  • The new pdf is just the original with no text added. I see a drawLayer: inContext function I am going to try quick. – Mike Mar 16 '13 at 22:35
  • Try `drawAtPoint` (0, 0) and saving to a new file to make sure it's at least trying to generate a new PDF. – Ric Mar 16 '13 at 22:46
  • OK. So writing worked if I then saved the context to the png image like is shown in the code above. How do I save to another pdf instead of the png? – Mike Mar 16 '13 at 22:54
  • OK. That worked! However, everything is mirrored. I'll work on the fix. Or do you know off hand? – Mike Mar 16 '13 at 23:10
  • Horizontally mirrored or vertically mirrored? If vertically, try removing the 3 lines under `Flip coordinate system` – Ric Mar 16 '13 at 23:14
  • The page was flipped vertically but the text was good. I commented out those three lines and that fixed the page and flipped the text. I should be able to play with the text and get it working. – Mike Mar 16 '13 at 23:21
  • 1
    OK. Flipped the coordinate system after page is drawn and before text is written. Everything is perfect. I can't thank you enough for your help Ric. – Mike Mar 16 '13 at 23:23