Skip navigation.
 
mlHow do I create a PDF fle paginated according to my own rules?
FROM : Paul Archibald
DATE : Fri Aug 29 02:51:31 2008

Comrades,

I have been working on creating a PDF file from a potentially large 
block of text. I have gotten some good advice from some of you 
already, but I am having a problem creating my PDF document in just 
the way I want it.

What I want to do is have a method that takes an array of strings and 
a filename, and iterates through the array, making separate "pages" 
of pdf content , and finally "gluing" them all together in a single 
document, so that when the user opens the document in Preview, each 
of the strings is on its own numbered page.

My original plan was to do something like this:


//--
turn the text into an array of strings (how is unimportant)
create a textview
create a pdfdoc with textview
for each string in array
   insert string into textview
   resize textview to fit the string
   create a pdfpage with textview
   insert pdfpage into pdfdoc
create a file with the pdfdoc data representation
--//

But this does not seem to be possible. The PDFDocument and PDFPage 
classes seem to have a weird relationship, where you can get a page 
from a document, or add/remove a page, but there does not seem to be 
any way to create a PDFPage and draw or set text into it directly.

It looks as though the only way to do what I want would be:

//--
turn the text into an array of strings
create a textview
create a pdfdoc#1 with textview
remove pdfpage[0] from pdfdoc#1 so it is empty
for each string in array
   create another textview#2
   insert string into textview#2
   resize textview#2 to fit the string
   create a 2nd pdfdoc with textview#2
   create a pdfpage from pdfdoc#2
   insert pdfpage into pdfdoc#1
create a file with the pdfdoc#1 data representation
--//

I have implemented this, and it sort of works, but the pages in the 
output file are blank. I know that the pages DO contain the text, 
since in my implementation I am spitting out each page to a separate 
pdf file as it is created, and those individual files DO look correct.

So, here is the method I am using now. It has some testing code in 
it, and comments that reflect what happens while running it.


//----------
- (BOOL) renderToPDFFile: (NSString*) text :(NSString*) file {
   NSArray *textChunks = [text componentsSeparatedByString:@"\n"]; // 
for testing just break on newlines
//    NSLog(@"textChunks == %@", textChunks);

   NSRect r = NSMakeRect(0, 0, 600, 1);
   NSTextView* view = [[NSTextView alloc] initWithFrame:r];
   r = [view bounds];
   PDFDocument *outputDoc = [[PDFDocument alloc] initWithData:[view 
dataWithPDFInsideRect:r]];
   [outputDoc removePageAtIndex:0];
   
   int i;
   for(i = 0; i < [textChunks count]; i++) {
       NSTextView* tempView = [[NSTextView alloc] initWithFrame:r];
       [tempView setBackgroundColor:[NSColor redColor]];
       NSString *s = [textChunks objectAtIndex:i];
       [tempView insertText:s];
       [tempView sizeToFit];        // this is important for me
       r = [tempView bounds];        // and it seems to work fine
       PDFDocument *tempDoc = [[PDFDocument alloc] initWithData:[tempView 
dataWithPDFInsideRect:r]];

       if(spitting) {
           // just for testing, spit out a pdf file for each line
           NSString *tempName =  [[file stringByDeletingPathExtension] 
stringByAppendingString:[NSString stringWithFormat:@"%d", i]];
           tempName = [tempName stringByAppendingPathExtension:@"pdf"];
           BOOL b2 = [tempDoc writeToFile:tempName];
           // okay, that worked, I got a pdf file with a single line of text, 
the right size
       }
                                   
       PDFPage *page = [[PDFPage alloc] initWithDocument:tempDoc];    // does 
this copy the content of the doc to the page?
       [outputDoc insertPage:page
               atIndex:i];        // this does insert the page into the output doc, 
but the page is blank
       [tempView autorelease];
       [page autorelease];
       [tempDoc autorelease];
   }
   

   NSData *data = [outputDoc dataRepresentation];
   BOOL b = [data writeToFile:file atomically:YES];
   [outputDoc autorelease];
   [view autorelease];        // if(alloced) autorelease
   return b;
}

----------//


So, does anyone know why the PDFPage objects that I create and add to 
the output PDFDocument don't have any text in them? The intermediate 
documents have the text. I must be missing something about just what 
a PDFPage does. The documentation says

"PDFPage objects are flexible and powerful. With them you can render 
PDF content onscreen or to a printer, add annotations, count 
characters, define selections, and get the textual content of a page 
as an NSString object.
Your application instantiates a PDFPage object by asking for one from 
a PDFDocument object."

That sounds like the right thing to use, what am I doing wrong?


Paul Archibald

Every decent man is ashamed of the government he lives under.
H.L. Mencken (1880-1956)

Related mailsAuthorDate
mlHow do I create a PDF fle paginated according to my own rules? Paul Archibald Aug 29, 02:51
mlRe: How do I create a PDF fle paginated according to my own rules? John Calhoun Aug 29, 03:27