Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iphone应用程序中编辑PDF_Iphone_Objective C_Ios - Fatal编程技术网

在iphone应用程序中编辑PDF

在iphone应用程序中编辑PDF,iphone,objective-c,ios,Iphone,Objective C,Ios,现在我正在创建一个类似于“iAnnotate PDF”的应用程序 到目前为止,我已经完成了在UIView中阅读和显示pdf页面。 就连我也成功地拿到了TOC 现在我想编辑PDF,编辑包括标记、突出显示和书写笔记 现在我的问题是如何编辑?我的意思是,我们是否需要创建另一个pdf并用新的更改覆盖它 或者有什么方法可以更新现有PDF中的单个页面?? 如果是,怎么做 如果我们重写整个pdf,这会不会造成开销?也许这个想法可以在iPhone上实现,我认为我们无法直接编辑PDF,但我们可以做的是,我们可以生

现在我正在创建一个类似于“iAnnotate PDF”的应用程序 到目前为止,我已经完成了在UIView中阅读和显示pdf页面。 就连我也成功地拿到了TOC

现在我想编辑PDF,编辑包括标记、突出显示和书写笔记

现在我的问题是如何编辑?我的意思是,我们是否需要创建另一个pdf并用新的更改覆盖它

或者有什么方法可以更新现有PDF中的单个页面?? 如果是,怎么做


如果我们重写整个pdf,这会不会造成开销?

也许这个想法可以在iPhone上实现,我认为我们无法直接编辑PDF,但我们可以做的是,我们可以生成iNotate应用程序所具有的功能,即绘制线条和文本,然后将PDF保存为一页一页的图片,在其中你也会包含编辑过的页面,然后你就可以了通过图像再次创建PDF,然后我认为您将获得已编辑的PDF。

您可以使用CoreGraphics轻松创建/编辑PDF。只需创建一个定义大小的rect,然后创建上下文,推送上下文,调用CFPDFContextBeginPage,然后像平常一样开始绘图……下面是一个示例:

    CGRect mediaBox = CGRectMake(0, 0, 550, 800);
    NSString *url = [path stringByExpandingTildeInPath];
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:url], &mediaBox, NULL);
    UIGraphicsPushContext(ctx);


//Page1 
    CGPDFContextBeginPage(ctx, NULL);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
    //name and value are NSStrings
    [name drawInRect:rectName withFont:fontName];
    [value drawInRect:rectValue withFont:fontValue];
    [[UIImage imageNamed:@"logo.png"] drawInRect:CGRectMake(8, 15, 91, 99)];
    CGPDFContextEndPage(ctx);

    UIGraphicsPopContext();
    CFRelease(ctx);
更新 您可以复制所需的页面,然后按如下方式在其上绘制:

CGPDFDocumentRef  originalDoc = NULL;
CGContextRef pdfContext = NULL;
CGRect docRect = CGRectZero;


NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf"];
NSString *newPath = @"/Users/***/Desktop/new.pdf";
CFURLRef newURL = CFURLCreateWithFileSystemPath (NULL, 
                                                 (CFStringRef)newPath,
                                                 kCFURLPOSIXPathStyle,
                                                 false);

//Original doc and it's dimesnions
originalDoc = CGPDFDocumentCreateWithURL((CFURLRef)originalURL);
CGPDFPageRef firstPage = CGPDFDocumentGetPage(originalDoc, 1);
if (firstPage == NULL) {
    NSLog(@"This document has no pages..Exiting.");
}
docRect = CGPDFPageGetBoxRect(firstPage, kCGPDFCropBox);
NSLog(@"%@", NSStringFromRect(docRect));


//New doc context
if (newURL != NULL) {
    pdfContext = CGPDFContextCreateWithURL(newURL, &docRect, NULL);

    if (pdfContext == NULL) {
        NSLog(@"Error creating context");
    }

    CFRelease(newURL);
} else {
    NSLog(@"Error creating url");
}
然后分别复制每一页。在这个特定的示例中,我将在页面中添加“Bates”(编号)

//Copy original to new, and write bates
size_t count = CGPDFDocumentGetNumberOfPages(originalDoc);

for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {



    CGPDFPageRef originalPage = CGPDFDocumentGetPage(originalDoc, pageNumber);

    CGContextBeginPage (pdfContext,nil);

    CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1);
    CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5);

    // Draw a circle (filled)
    CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25));

    CGContextSaveGState(pdfContext);

    //flip context due to different origins
    CGContextTranslateCTM(pdfContext, 0.0, (docRect.size.height - (docRect.size.height * 0.80))/2);
    CGContextScaleCTM(pdfContext, 1.0, 0.8);

    //copy content of template page on the corresponding page in new file
    CGContextDrawPDFPage(pdfContext, originalPage);

    CGContextRestoreGState(pdfContext);

    //flip context back
    //CGContextTranslateCTM(pdfContext, 0.0, -(docRect.size.height - (docRect.size.height * 0.80))/2);
    //CGContextScaleCTM(pdfContext, 1.0, 1.25);

    CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1);
    CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5);

    // Draw a circle (filled)
    CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25));

    NSString *bate = [self generateStringForCount:(int)pageNumber-1];

    int fontSize = 15;
    CGContextSelectFont(pdfContext, "Helvetica", fontSize, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
    CGContextSetTextPosition(pdfContext, 0.0f, round(fontSize / 4.0f));
    CGContextShowText(pdfContext, [bate UTF8String], strlen([bate UTF8String]));


    CGContextEndPage(pdfContext);

}

CFRelease(pdfContext);
//将原始文件复制到新文件,然后编写bates
大小\u t计数=CGPDFDocumentGetNumberOfPages(原始ALDOC);

对于(size_t pageNumber=1;pageNumber)来说,随着最新版本的iOS,在iPhone上编辑PDF变得非常容易

  • 在iPhone上打开PDF(通常在书籍/照片上)
  • 单击标记工具
  • 从底部的“更多”选项中,可以选择文本、签名和放大镜

  • 你也可以为文本添加不同的颜色。但是你不能编辑现有的文本。我建议你使用一些最好的PDF编辑软件。

    但是你不认为这会产生过度的效果吗?对于单个页面,我们需要编写整个PDF,PDF中的页面没有限制。但这是一种方式。我认为没有直接的API或API提供此类功能的库这取决于您的想法。我知道如何创建pdf。我想编辑pdf。如何编辑单行而不是单页。我想避免重写整个pdf。如果可以在现有pdf中编辑单页,则将很容易…或任何复制大量页面并替换特定pa的方法ge也会很好的。更新,展示如何复制页面。有了它,你可以在页面上画图或添加新内容。好的。现在明白你的意思了。谢谢。将实现它:)所以我们没有任何东西要更新单个页面,对吧,我们需要重新创建整个pdf:(是的:(.你可以使用PDFKit,但它的功能非常有限…ios上没有pdf工具包:(OP希望实现自己的pdf编辑软件,而不是预安装的书籍/照片。