Adding image with PDFDoc newImageJPEG - RadaeePDF.com
Signin/Signup with: 
Welcome, Guest
Username: Password: Remember me
13 Jul 2017
Microsoft has ended support for Windows Phone 8.1
Read More...
Questions about iOS development and PDF
  • Page:
  • 1

TOPIC: Adding image with PDFDoc newImageJPEG

Adding image with PDFDoc newImageJPEG 3 years 2 months ago #9571

  • mobileAsd
  • mobileAsd's Avatar Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 25
  • Thank you received: 0
Hi,

We are currently using the PDFDoc newImageJPEG to draw image to a newly created PDF document. The PDF document is successfully created but with blank content, below is our code, is there any step we have left? Thanks

UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
PDFDoc *tmpPDF = [[PDFDoc alloc] init];
[tmpPDF create:tmpPDFPath];
NSString *cacheFile = [[NSTemporaryDirectory() stringByAppendingString:@""] stringByAppendingString:@"cache.dat"];
[tmpPDF setCache:cacheFile];
PDFPage *tmpPDFPage = [tmpPDF newPage:1 :img.size.width :img.size.height];
PDFPageContent *tmpPageContent = [[PDFPageContent alloc] init];
PDFVPage *tmpVPage = [[PDFVPage alloc] init:tmpPDF :0];
[tmpPageContent drawImage:[tmpPDFPage addResImage:[tmpPDF newImageJPEG:imgPath]]];
[tmpPDFPage addContent:tmpPageContent :false];
[tmpVPage DeleteBmp];
[tmpVPage RenderPrepare];
[[tmpVPage Cache] Render];
[tmpPDF save];

Here please find the resultant PDF file and the JPEG image to be added

File Attachment:

File Name: testImage.pdf
File Size: 92 KB

Attachments:

Please Log in or Create an account to join the conversation.

Adding image with PDFDoc newImageJPEG 3 years 2 months ago #9579

  • emanuele
  • emanuele's Avatar
  • Offline
  • Moderator
  • Moderator
  • Posts: 517
  • Karma: 15
  • Thank you received: 57
Hi,

you should also set the pageContent matrix, to set the right image size.
With your code, the page isn't completely white, there is the image in the left bottom cornet, but with a incorrect size.

to fix this problem, you should modify your code in this way:
UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
PDFDoc *tmpPDF = [[PDFDoc alloc] init];
[tmpPDF create:tmpPDFPath];
NSString *cacheFile = [[NSTemporaryDirectory() stringByAppendingString:@""] stringByAppendingString:@"cache.dat"];
[tmpPDF setCache:cacheFile];
PDFPage *tmpPDFPage = [tmpPDF newPage:1 :img.size.width :img.size.height];
PDFPageContent *tmpPageContent = [[PDFPageContent alloc] init];
PDFVPage *tmpVPage = [[PDFVPage alloc] init:tmpPDF :0];

    PDFDocImage *dm = [tmpPDF newImageJPEG:imgPath];
    PDFMatrix *mat = [[PDFMatrix alloc] init:img.size.width :img.size.height :0 :0];
    [tmpPageContent gsCatMatrix:mat];
    [tmpPageContent drawImage:[tmpPDFPage addResImage:dm]];

[tmpPDFPage addContent:tmpPageContent :false];
[tmpVPage DeleteBmp];
[tmpVPage RenderPrepare];
[[tmpVPage Cache] Render];
[tmpPDF save];
The following user(s) said Thank You: mobileAsd

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Powered by Kunena Forum