- Posts: 25
- Thank you received: 0
Microsoft Windows Phone 8.1 support ends (13 Jul 2017)
Microsoft has ended support for Windows Phone 8.1
Questions about iOS development and PDF
Adding image with PDFDoc newImageJPEG
11 years 1 week ago #9571
by mobileAsd
Adding image with PDFDoc newImageJPEG was created by mobileAsd
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
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
11 years 1 week ago #9579
by emanuele
Replied by emanuele on topic Adding image with PDFDoc newImageJPEG
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:
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:
Code:
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];
Time to create page: 0.387 seconds