在 iPad 应用程序中从 UIView 创建 pdf 时出现问题
我正在 iPad 应用程序中从 UIView 创建 pdf。它的大小为 768 * 2000。当我创建 pdf 时,它会以相同的大小创建,并在一页上显示所有内容。所以当我从 iPad 打印它时遇到问题。我正在使用以下代码来创建 pdf:-
-(void)drawPdf:(UIView *)previewView{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
//CGRect tempRect = CGRectMake(0, 0, 768, 1068);
CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
CGContextBeginPage (pdfContext,nil); // 6
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
//Draw view into PDF
[previewView.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);// 8
CGContextRelease (pdfContext);
}
//Create empty PDF context on iPhone for later randering in it
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{
CGContextRef myOutContext = NULL;
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, // 1
path,
kCFURLPOSIXPathStyle,
false);
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL (url,// 2
&inMediaBox, NULL);
CFRelease(url);// 3
}
return myOutContext;// 4
}
谁能建议我如何减小 pdf 大小并且它有多个页面?
提前致谢。
I am creating a pdf from a UIView in my iPad Application. It has size 768 * 2000. When I create pdf then it create with same size and it shows all content on one page. So I am facing problem when I print it from iPad. i am using the following code for creating pdf :-
-(void)drawPdf:(UIView *)previewView{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
//CGRect tempRect = CGRectMake(0, 0, 768, 1068);
CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
CGContextBeginPage (pdfContext,nil); // 6
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
//Draw view into PDF
[previewView.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);// 8
CGContextRelease (pdfContext);
}
//Create empty PDF context on iPhone for later randering in it
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{
CGContextRef myOutContext = NULL;
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, // 1
path,
kCFURLPOSIXPathStyle,
false);
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL (url,// 2
&inMediaBox, NULL);
CFRelease(url);// 3
}
return myOutContext;// 4
}
Can anyone suggest me how can I reduce the pdf size and it has multiple page ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅“iOS 绘图和打印指南”中的示例
https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratePDF/GeneratePDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1
do while 循环,并注意它如何在循环中启动一个新的 PDF 页面
……
基本上在清单 4-1 代码示例中,他们有一个
: 在您当前的方法中,您只调用了开始页面方法之一,因此这就是为什么您只会有一个页面。
See example in the "Drawing and Printing Guide for iOS"
https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1
Basically in the Listing 4-1 code example, they have a do while loop and take notice how it starts a new PDF page in the loop:
...
...
In your current method, you only called one of the begin page methods once so that's why you'll only ever have one page.
您需要为要创建的每个新 PDF 页面调用 UIGraphicsBeginPDFPage。假设您有一个可变高度的 UIView,以下是您在运行时根据需要将其分解为尽可能多的 PDF 页面的方法:
You need to call on UIGraphicsBeginPDFPage for every new PDF page you want to create. Assuming you have a UIView of variable height, here is how you would break it down to as many PDF pages as needed during run time: