Edit AllPages

I’m trying to find a better way to insert numerous pages into a PDFDocument for display in a PDFView.

I have an array containing PDFPages I’m calling “_pages”.

I’ve tried the intuitive approach, but the PDFView is screwing it up…it won’t display the thing, for some reason if I change the [_pages objectAtIndex:i] index value to a static number it will work (putting the same page in throughout the whole script), but otherwise there are always problems, either at the end of the document where the PDFView goes berserk (indexForPage:page not found). Sometimes it puts in a default small page. I’ve looked pretty extensively into my _pages array and counters and they appear to be alright, so I think it’s a problem with my understanding of PDFView and/or PDFDocument rather than a pointer error. (Also, others have had this problem)

int i;

PDFDocument *masterScriptDocument = [[PDFDocument alloc] initWithData:[_pages objectAtIndex:0] dataRepresentation]];
for (i=1;i<_numberOfPages; i++) {
	[masterScriptDocument insertPage:[_pages objectAtIndex:i] atIndex:i];

I’ve looked around online and found this solution:

int i;

PDFDocument *masterScriptDocument = [[PDFDocument alloc] initWithData:_pages objectAtIndex:0] dataRepresentation;
for (i=1;i<_numberOfPages; i++) {
	PDFDocument *aPage = [[PDFDocument alloc] initWithData:_pages objectAtIndex:0]  dataRepresentation;
	[masterScriptDocument insertPage:[aPage pageAtIndex:0] atIndex:i];
	masterScriptDocument = [[PDFDocument alloc] initWithData:[masterScriptDocument dataRepresentation]]; 


This works, but freezes up my computer on anything over twenty pages.

Anybody have a fix for this?

Thanks - EricBadros

I’ve figured it out and though it is reasonably slow (it takes about 3-4 times as long as preparing the document for printing) it works consistently and correctly…I’m going to try out a few more things to speed it up (and make it more elegant) before posting my solution. - EricBadros

Sorry, almost forgot that I had promised to post this.

I have two NSTableViews subclass instances, one with the revision document and one with the source document. One can choose pages from the source document and then choose which pages to replace, or insert in the revision document…that’s why you may see a lot of extraneous stuff that isn’t vitally important but provides a more complete solution (I think). This is in my ScriptManagerController. Please forgive me, I made an assumption that both documents pages are 612x792…easily fixed but gets the point across.



Then, the I have subclassed NSView for the ScriptCreatorView and here is the relevant code:

Hope that helps…I know a lot of people have been trying to make this work and I’ve found this is pretty fast…for 120 pages on a Powerbook G4 1.6 with 1GB I just get the pinwheel before the new PDFDocument is created. EricBadros

Maybe I’m misunderstanding the problem but why not do it like so?…

return [masterDocument autorelease]; }

That’s basically the way I do it. As long as everything in the array is a PDFPage it should work ok. If I really need to insert pages in specific places (instead of just adding to the end) then I modify/re-order the array before making the document.

I went ahead and turned that into PDFDocumentCategory so..yeah.