1

我想将 NSMutableArray 的内容转换为 NSData,然后将其转换为 pdf。我正在使用以下代码来转换 NSdata 但它给出了错误。我搜索了很多文章但没有得到任何东西

  myArray=[[NSMutableArray alloc]init];


  [myArray addObject:@"Jamshed"];
  [myArray addObject:@"Imran"];
  [myArray addObject:@"Ali"];
  [myArray addObject:@"Hussain"];
  [myArray addObject:@"Faisal"];

 for (int i=0; i<[myArray count]; i++)
 {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[myArray objectAtIndex:i]];
NSLog(@"data %@",data);
//create code for pdf file for write b4 read and concatenate readed string with data to write in pdf file.  
 }




   - (NSData*) pdfDataWithSomeText;
   {
// For more on generating PDFs, see http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html
// The PDF content will be accumulated into this data object.
 NSMutableData *pdfData = [NSMutableData data];

// Use the system default font.
 UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];

// Use the default page size of 612*792.
 CGRect pageRect = CGRectMake(0, 0, 612, 792);

// Use the defaults for the document, and no metadata.
 UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);

// Create a page.
 UIGraphicsBeginPDFPageWithInfo(pageRect, nil);

// Store some placeholder text.
 NSString *topLine = @"PDF Sample from";
 NSString *bottomLine = @"http://stackoverflow.com/q/10122216/1318452";

// Draw that placeholder text, starting from the top left.
 CGPoint topLeft = CGPointZero;
 CGSize lineSize = [topLine sizeWithFont:font];
 [topLine drawAtPoint:topLeft withFont:font];
// Move down by the size of the first line before drawing the second.
 topLeft.y += lineSize.height;
 [bottomLine drawAtPoint:topLeft withFont:font];

// Close the PDF context.
 UIGraphicsEndPDFContext();  

// The pdfData object has now had a complete PDF file written to it.
 return pdfData;
 }
4

4 回答 4

3

将字符串写入 PDF 并不像从这些字符串生成 NSData 那样简单。查看iOS 的绘图和打印指南 - 生成 PDF 内容。是的,这是一个很大的文件。阅读。尝试其中的示例。尝试将您自己的字符串添加到他们的示例中。然后,如果您有几乎可以使用的东西,请回到这里询问。

生成 PDF

所以这里是来自上面链接的代码,通过使用 NSString 而不是 Core Text 绘制变得更加简单。它绘制输入数组,但可能需要一些更好的算术。你能让它以更结构化的方式绘制吗?

- (NSData*) pdfDataWithStrings:(NSArray*) strings;
{
    // For more on generating PDFs, see http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html
    strings = [strings arrayByAddingObject:@"https://stackoverflow.com/q/10122216/1318452"];
    // The PDF content will be accumulated into this data object.
    NSMutableData *pdfData = [NSMutableData data];

    // Use the system default font.
    UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];

    // Use the default page size of 612*792.
    CGRect pageRect = CGRectMake(0, 0, 612, 792);

    // Use the defaults for the document, and no metadata.
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);

    // Create a page.
    UIGraphicsBeginPDFPageWithInfo(pageRect, nil);

    // Add the strings within the space of the pageRect.
    // If you want to draw the strings in a column or row, in order, you will need to change this bit.
    for (NSString *line in strings)
    {
        // Hint: you will still need to know the lineSize.
        CGSize lineSize = [line sizeWithFont:font];
        CGFloat x = pageRect.origin.x + (arc4random_uniform(RAND_MAX)/(CGFloat) RAND_MAX*(pageRect.size.width-lineSize.width));
        CGFloat y = pageRect.origin.y + (arc4random_uniform(RAND_MAX)/(CGFloat) RAND_MAX*(pageRect.size.height-lineSize.height));
        CGPoint lineTopLeft = CGPointMake(x, y);

        // Having worked out coordinates, draw the line.
        [line drawAtPoint:lineTopLeft withFont:font];
    }

    // Close the PDF context.
    UIGraphicsEndPDFContext();  

    // The pdfData object has now had a complete PDF file written to it.
    return pdfData;
}

保存到文档目录

要保存文档,您需要找到保存用户文档的路径:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

您将在该路径中保存一个文件。对于您的最终应用,让用户选择自己的名称。对于此示例,名称将被构建到程序中。

NSString *pdfFilename = @"StackOverflow.pdf";

NSString 有一些优秀的路径操作方法,可以很容易地构造你将要写入的路径。

NSString *pdfPath = [documentsPath stringByAppendingPathComponent:pdfFilename];

然后获取您将写入该路径的数据。这里我将调用上面声明的方法。

NSData *pdfData = [self pdfDataWithStrings:myArray];

将这些数据写入路径。对于更好的应用程序,您可能在某些时候想要调用 [pdfData writeToFile:options:error:] 以便您可以显示任何出错的内容。

[pdfData writeToFile:pdfPath atomically:NO];

你怎么知道这是否有效?在模拟器上,您可以记录您写入的路径。在 Finder 中打开此路径,查看它是否包含您期望的 PDF。

NSLog(@"Wrote PDF to %@", pdfPath);

在实际设备上,您可以启用 iTunes 文件共享。请参阅如何为我的应用启用文件共享?

于 2012-04-12T12:07:39.583 回答
0

在做之前

NSData *data = [NSKeyedArchiver archivedDataWithRootObject: myArray];

您首先将数组转换为 json 字符串

NSString *jsonString = [myArray JSONRepresentation];

您必须先导入 json.h api

于 2012-04-12T11:02:59.723 回答
0

你可以使用转换成 json

 NSData *jsonData        =   [NSJSONSerialization dataWithJSONObject:finalDict options:NSJSONWritingPrettyPrinted error:nil];
于 2012-04-12T11:19:19.920 回答
-1
myArray=[[NSMutableArray alloc]init];


[myArray addObject:@"Jamshed"];
[myArray addObject:@"Imran"];
[myArray addObject:@"Ali"];
[myArray addObject:@"Hussain"];
[myArray addObject:@"Faisal"];

for (int i=0; i<[myArray count]; i++)
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[myArray objectAtIndex:i]];
    NSLog(@"data %@",data);
    //create code for pdf file for write b4 read and concatenate readed string with data to write in pdf file.  
}
于 2012-04-12T11:53:39.577 回答