3

現在、UIView の PDF を作成して保存しています。うまく機能していますが、UIView からではなく、ディスクに保存されたテキスト データから PDF を作成しようとしています。理想的には、UITextView のテキストに基づいて PDF を作成したいと考えています。以下のコードは、テキスト ビューの最初の部分を正しく書き込みますが、テキスト ビュー全体から PDF を作成しません。これはかなりの量のテキストなので、ビューに収まる量のテキストからのみ PDF を作成しました。私が現在使用しているコードは次のとおりです。

- (void)createPDFfromUIView:(UITextView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{

   NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks));

    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, textview.bounds, myDictionary);
    UIGraphicsBeginPDFPage();

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [textview.layer renderInContext:pdfContext]; // this line

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"filename"];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
}

そして、それを呼び出して保存するには、このメッセージ送信を IBAction に入れます。[self createPDFfromUIView:textview saveToDocumentsWithFileName:@"pdflocation"];

今、私はこのコードとドキュメントを何時間も解析してきましたが、ビューではなく保存されたテキスト ファイルに基づいて PDF を作成する方法がわかりません。私はまだプログラミングに慣れていないので、これの多くは頭を悩ませています。上記のコードを変更して、ビューではなく保存されたテキストに基づいて PDF を作成する方法を知っている人はいますか?

オプション: NSString の出力に基づいて PDF を作成できます。保存したテキストを UITextView に投げて、それに基づいて PDF を作成することもできますが (元の計画)、どちらの方法もわかりません。

4

2 に答える 2