Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:32:17+00:00 2026-06-01T23:32:17+00:00

I want to convert the contents of NSMutableArray to NSData and then convert it

  • 0

I want to convert the contents of NSMutableArray to NSData and then convert it to pdf.
I am using following code to conver NSdata but it gives error .I have searched many article but not getting anything

  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;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:32:19+00:00Added an answer on June 1, 2026 at 11:32 pm

    Writing strings to a PDF is not as simple as generating NSData from those strings. Look at the Drawing and Printing Guide for iOS – Generating PDF Content. Yes, it is a big document. Read it. Try the examples from it. Try adding your own strings to their example. Then, if you have something that almost works, come back here to ask about it.

    Generating the PDF

    So here is the code from the link above, made even simpler by drawing with NSString instead of Core Text. It draws the input array, but will probably need to some better arithmetic. Can you make it draw in a more structured way?

    - (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;
    }
    

    Saving to the Documents Directory

    To save a document, you need to find the path where the user’s documents are kept:

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

    You will be saving a file within that path. For your final app, let the user select their own name. For this example, the name will be build into the program.

    NSString *pdfFilename = @"StackOverflow.pdf";
    

    NSString has some excellent path manipulation methods, making it easy to construct the path you will be writing to.

    NSString *pdfPath = [documentsPath stringByAppendingPathComponent:pdfFilename];
    

    Then get the data you’ll be writing to that path. Here I’ll call the method declared above.

    NSData *pdfData = [self pdfDataWithStrings:myArray];
    

    Write those data to the path. For a better app, you may at some point want to call [pdfData writeToFile:options:error:] so you can display anything that went wrong.

    [pdfData writeToFile:pdfPath atomically:NO];
    

    How do you know if this worked? On the simulator, you can log the path you wrote to. Open this path in the Finder, and see if it contains the PDF you expect.

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

    On actual devices, you can enable iTunes File Sharing. See How to enable file sharing for my app?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to convert my webpage(displayed in UIWebView) contents in pdf file. which can
I want to convert the following PHP operation to C# code, could anyone please
I have file contents in a java string variable, which I want to convert
Basically, I have a RichTextBox and I want to convert the formatted contents of
I want to convert a PDF file into a CSV file. I am using
I want to convert the mysql database table contents to an Excel(.xls) or comma
I want to convert a column of numbers to numeric, but there are certain
I want to convert plaintext to link for an input I have. The HTML
Let's say i have the following string: a test-eh'l I want to capitalize the
Suppose I have an InputStream that contains text data, and I want to convert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.