在iPad上直接从UiTextView创建多页PDF

3

我创建了一个特定布局的pdf。问题是当文本视图的内容非常长时,pdf不会自动生成新页面,并且我无法预定义在运行时将创建多少页。请问有人能帮我吗?


1
首先欢迎,你尝试了什么? - iPatel
你试过这样做了吗?你完成了吗? - 08442
3个回答

2

您可以尝试使用以下代码,这可能会对您有所帮助。 在界面构建器中保留一个文本视图和一个按钮,并使用按钮操作方法并生成PDF。 导入这些框架。

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>



    -(NSString*)getPDFFileName
    {
        NSString* fileName = @"sample.PDF";
        NSArray *arrayPaths =
        NSSearchPathForDirectoriesInDomains(
                                            NSDocumentDirectory,
                                            NSUserDomainMask,
                                            YES);
        NSString *path = [arrayPaths objectAtIndex:0];
        NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
        return pdfFileName;

    }
    -(IBAction)PdfGeneration:(id)sender{
        // Prepare the text using a Core Text Framesetter.
        CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (__bridge CFStringRef)txtObj.text, NULL);
        if (currentText) {
                CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
                if (framesetter) {

                NSString *pdfFileName = [self getPDFFileName];

                // Create the PDF context using the default page size of 612 x 792.

                UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
                CFRange currentRange = CFRangeMake(0, 0);
                NSInteger currentPage = 0;
                BOOL done = NO;
                do {

                    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
                    currentPage++;
                    [self drawPageNumber:currentPage];
                    currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];
                    if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))

                        done = YES;

                } while (!done);
                UIGraphicsEndPDFContext();
               CFRelease(framesetter);
            } else {

                NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");

            }

            // Release the attributed string.

            CFRelease(currentText);

        } else {

            NSLog(@"Could not create the attributed string for the framesetter");

        }
    }
    // Use Core Text to draw the text in a frame on the page.

    - (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange

           andFramesetter:(CTFramesetterRef)framesetter

    {

        // Get the graphics context.

        CGContextRef    currentContext = UIGraphicsGetCurrentContext();
        CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
        CGRect    frameRect = CGRectMake(72, 72, 468, 648);
        CGMutablePathRef framePath = CGPathCreateMutable();
        CGPathAddRect(framePath, NULL, frameRect);
        CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
        CGPathRelease(framePath);
        CGContextTranslateCTM(currentContext, 0, 792);
        CGContextScaleCTM(currentContext, 1.0, -1.0);
        CTFrameDraw(frameRef, currentContext);
        currentRange = CTFrameGetVisibleStringRange(frameRef);
        currentRange.location += currentRange.length;
        currentRange.length = 0;
        CFRelease(frameRef);
        return currentRange;

    }
    - (void)drawPageNumber:(NSInteger)pageNum

    {

        NSString *pageString = [NSString stringWithFormat:@"Page %d", pageNum];
        UIFont *theFont = [UIFont systemFontOfSize:12];
        CGSize maxSize = CGSizeMake(612, 72);
        CGSize pageStringSize = [pageString sizeWithFont:theFont constrainedToSize:maxSize lineBreakMode:UILineBreakModeClip];
        CGRect stringRect = CGRectMake(((612.0 - pageStringSize.width) / 2.0),720.0 + ((72.0 - pageStringSize.height) / 2.0),pageStringSize.width,pageStringSize.height);
           [pageString drawInRect:stringRect withFont:theFont];

    }

谢谢您的回复。这会根据textView中字符串的长度生成多页PDF文件吗? - Chaitali Jain

0

很多用户正在寻找此代码以使其与Swift版本兼容。因此,这里提供了适用于Swift 4和5的代码。

// PDF名称并在本地路径上生成:

func writeToPDF(filenameStr:String)

{
    pageSize = CGSize.init(width: 768, height: 10000)

   // let fileName: NSString = "test.pdf"
    let fileName = String.init(format: "%@.pdf", filenameStr)
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let pdfPathWithFileName =   documentDirectory.appending("/" + fileName)//  documentDirectory.appending(fileName as String)
    self.PdfGeneration(filePath: pdfPathWithFileName)

    //lines written to get the document directory path for the generated pdf file.
    if let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.path {
        print("documentsPath",documentsPath)
        // “var/folder/…/documents ” copy the full path

    }


}

// 生成PDF文件以显示文本和图片的方法。

func PdfGeneration(filePath: String)
{
// Prepare the text using a Core Text Framesetter.
//CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (__bridge CFStringRef)txtObj.text, NULL);
let currentText = CFAttributedStringCreate(nil, (self.textVW_file.text as? CFString?)!, nil)
if(currentText != nil)
{
let framesetter = CTFramesetterCreateWithAttributedString(currentText!)
if (framesetter !=  nil) {
UIGraphicsBeginPDFContextToFile(filePath, .zero, nil)
    var currentRange = CFRangeMake(0, 0)
    var currentPage = 0
    var done = false

repeat {
 repeat {
UIGraphicsBeginPDFPageWithInfo(CGRect.init(x:0, y:0, width:612, height:792), nil)
currentPage = currentPage + 1
self.drawPageNumber(currentPage)
currentRange = self.renderPage(currentPage, withTextRange: currentRange, andFramesetter: framesetter)
let aText:CFAttributedString = currentText!
    print("currentText %@",currentText!)
    print("currentRange.location %@ and %@",currentRange.location,CFAttributedStringGetLength(aText))
if (currentRange.location == CFAttributedStringGetLength(aText)) {
done = true
}
}while (!done)
 UIGraphicsEndPDFContext()
}
else
{
print("Could not create the framesetter needed to lay out the atrributed string.")
}
}
else
{
print("Could not create the framesetter needed to lay out the atrributed string.")

}


}

// 使用 Core Text 在页面上的框架中绘制文本。

func renderPage(_ pageNum: Int, withTextRange currentRange: CFRange, andFramesetter framesetter: CTFramesetter?) -> CFRange {
    var currentRangeIn:CFRange
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.textMatrix = .identity
let frameRect = CGRect(x: 72, y: 72, width: 468, height: 648)
let framePath = CGMutablePath()
framePath.addRect(frameRect, transform: .identity)
    let frameRef = CTFramesetterCreateFrame(framesetter!, currentRange, framePath, nil)
currentContext?.translateBy(x: 0, y: 792)
currentContext?.scaleBy(x: 1.0, y: -1.0)
    CTFrameDraw(frameRef, currentContext!)
currentRangeIn = currentRange
 currentRangeIn = CTFrameGetVisibleStringRange(frameRef)
currentRangeIn.location += currentRange2.length
currentRangeIn.length = 0
return currentRangeIn
}

//在底部绘制页面编号

func drawPageNumber(_ pageNum: Int) {
    let pageString = "Page \(pageNum)"
    let theFont = UIFont.systemFont(ofSize: 12)
    let maxSize = CGSize(width: 612, height: 72)
    let pageStringSize: CGSize = pageString.size(withAttributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)])

    //let pageStringSize = pageString.size(with: theFont, constrainedTo: maxSize, lineBreakMode: [])
    let stringRect = CGRect(x: (612.0 - pageStringSize.width) / 2.0, y: 720.0 + ((72.0 - pageStringSize.height) / 2.0), width: pageStringSize.width, height: pageStringSize.height)
    pageString.draw(in: stringRect, withAttributes: [NSAttributedString.Key.font : theFont])
}

0
//StartNewPage
                UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);

如果输入的文本超出页面框架设置,请查找TextView中的文本高度并调用此方法。PageFrame是PDF的新页面框架。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接