title: How to Convert PDF to NSImage and change the DPI?
body: <p>I have tried to convert PDF Pages to NSImage and save to JPG files successfully. However the output result is as normal 72 DPI. I want to change the DPI to 300 DPI but failed. Below is the code:</p>
  
  <pre><code>- (IBAction)TestButton:(id)sender {
  
      NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
      NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"1.pdf"];
  
      NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
      NSPDFImageRep *pdfImg = [NSPDFImageRep imageRepWithData:pdfData];
  
  
      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSInteger pageCount = [pdfImg pageCount];
  
  
  
      for(int i = 0 ; i &lt; pageCount ; i++) {
          [pdfImg setCurrentPage:i];
          NSImage *temp = [[NSImage alloc] init];
          [temp addRepresentation:pdfImg];
  
          CGFloat factor = 300/72; // Scale from 72 DPI to 300 DPI
          //NSImage *img; // Source image
          NSSize newSize = NSMakeSize(temp.size.width*factor, temp.size.height*factor);
          NSImage *scaledImg = [[NSImage alloc] initWithSize:newSize];
          [scaledImg lockFocus];
          [[NSColor whiteColor] set];
          [NSBezierPath fillRect:NSMakeRect(0, 0, newSize.width, newSize.height)];
          NSAffineTransform *transform = [NSAffineTransform transform];
          [transform scaleBy:factor];
          [transform concat];
          [temp drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
          [scaledImg unlockFocus];
  
  
          NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
          NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
          NSString *pageName = [NSString stringWithFormat:@"Page_%ld.jpg", (long)[pdfImg currentPage]];
          [fileManager createFileAtPath:[NSString stringWithFormat:@"%@%@", pdfPath, pageName] contents:finalData attributes:nil];
      }
  }
  </code></pre>