iOS word文件转pdf文件

思路:1、加载word文件 2、获取页面打印样式并转化为pdf

1、利用webView隐式加载word文件

创建文件管理单例

@interface YXWaterMarkWordManger ()<WKNavigationDelegate>
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,strong) NSMutableArray *pathArray;
@property (nonatomic,assign) int count;
@end

+ (YXWaterMarkWordManger *)manger {
static YXWaterMarkWordManger *manger = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    manger = [super allocWithZone:NULL];
});
return manger;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [self manger];
}

加载文件

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];

完成加载

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
//转化为pdf
NSData *pdfData = [self.webView toPDFData];
[self pdfData:pdfData];
}

2、获取页面打印样式并转化为pdf

- (NSData *)toPDFData{

//返回视图的打印格式化
UIViewPrintFormatter *format = [self viewPrintFormatter];
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:format startingAtPageAtIndex:0];

// 设置PDF文件每页的尺寸
CGRect pageRect =  CGRectMake(0, 0, 600, 768);
//呈现每个页面的上下文的尺寸大小
CGRect printableRect = CGRectInset(pageRect, 50, 50);

[render setValue:[NSValue valueWithCGRect:pageRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];

NSMutableData *pdfData = [NSMutableData data];

UIGraphicsBeginPDFContextToData(pdfData, pageRect, NULL);

for (NSInteger i = 0; i < [render numberOfPages]; i++) {
    
    UIGraphicsBeginPDFPage();
    CGRect bounds = UIGraphicsGetPDFContextBounds();
    [render drawPageAtIndex:i inRect:bounds];
}

UIGraphicsEndPDFContext();

return pdfData; 
}

- (NSString *)joinPDF:(NSArray *)listOfPaths {
NSString *fileName = [NSString stringWithFormat:@"pdf新.pdf"];
NSString *pdfPathOutput = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:fileName];

CFURLRef pdfURLOutput = (CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:pdfPathOutput]);

NSInteger numberOfPages = 0;

CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

for (NSString *source in listOfPaths) {

    CFURLRef pdfURL = (CFURLRef)CFBridgingRetain([[NSURL alloc] initFileURLWithPath:source]);
    
    CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
    
    numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);
    
    CGPDFPageRef page;
    CGRect mediaBox;

    for (int i=1; i<=numberOfPages; i++) {

        page = CGPDFDocumentGetPage(pdfRef,i);

        mediaBox = CGPDFPageGetBoxRect(page,kCGPDFMediaBox);

        CGContextBeginPage(writeContext,&mediaBox);

        CGContextDrawPDFPage(writeContext,page);

        CGContextEndPage(writeContext);

    }
    CGPDFDocumentRelease(pdfRef);
    CFRelease(pdfURL);
}
CFRelease(pdfURLOutput);

CGPDFContextClose(writeContext);

CGContextRelease(writeContext);

return pdfPathOutput;
}

- (void)pdfData:(NSData *)pdfData {
NSMutableData *outputPDFData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);
CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable(NULL,0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrDictionary,kCGPDFContextTitle,CFSTR("测试"));
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer,NULL,attrDictionary);
CFRelease(dataConsumer);
CFRelease(attrDictionary);
CFDataRef myPDFData = (__bridge CFDataRef)pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider);
size_t count = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf,self.count);
CGRect pageRect = CGPDFPageGetBoxRect(page,kCGPDFMediaBox);
CGContextBeginPage(pdfContext,&pageRect);
CGContextDrawPDFPage(pdfContext,page);
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
NSString *docsDirectory = NSTemporaryDirectory();
NSString *pdfFilePath = [NSString stringWithFormat:@"%@/outPutWordPDF%d.pdf",docsDirectory,self.count];
[outputPDFData writeToFile:pdfFilePath atomically:YES];
[self.pathArray addObject:pdfFilePath];
if (self.count>=count) {
    NSString *newPath = [self joinPDF:self.pathArray.mutableCopy];
    return;
}
self.count++;
[self addSignature:imgSignature onPDFData:pdfData backPth:backPath];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容