Generating a paginated PDF for use with PDFDocument

  • Hi,

    I've searched the archives but solutions posted here
    don't seem to have worked for me.

    Basically, I have a paginated view, a bit like the
    multiple page view in text edit, except that mine is
    used only for printing. This view gets printed or
    saved as a PDF through the inbuilt print panels/sheets
    perfectly.

    However, what I would now like to do is generate PDF
    data from this view that can be used to create a
    PDFDocument (using PDFDocument's -initWithData:) and
    then displayed in the PDFView right inside my app. In
    other words, I want to create a sort of one-click
    preview of the paginated view right within in my app,
    without having to go to print > preview and without
    having to save the PDF to disk.

    Unfortunately, all of the methods I have tried to do
    this just create one long PDF page rather than a
    paginated document with borders.

    For instance, I thought this would work:

    KBTextPagePrintingView *printView = [self
    draftPrintingView];
    NSMutableData *data = [NSMutableData data];
    NSPrintOperation *printOp = [NSPrintOperation
    PDFOperationWithView:printView insideRect:[printView
    bounds] toData:data printInfo:[printView printInfo]];
    [printOp runOperation];

    (Note that KBTextPagePrintingView is my paginated
    view.)

    But it doesn't. (Neither does NSView's
    dataWithPDFInsideRect:) When loaded into a PDFDocument
    and then displayed in a PDFView, the document is one
    long (and thin) document.

    Is there any way of doing this without having to save
    the file as a PDF file to disk first?

    Many thanks in advance,
    Keith


    ____________________________________________________________________________________
    Got a little couch potato?
    Check out fun summer activities for kids.
    http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+k
    ids&cs=bz
  • One solution that quickly comes to mind (not elegant I'm afraid) is to
    use -[NSView dataWithPDFInsideRect:] multiple times (once for each
    page).

    You'll have to compute the rect for each page.  And you'll have to
    create a PDFDocument for each page as well (this is the particularly
    inelegant part).  WIth a PDFDocument per page, you can then get the
    one PDFPage from each PDFDocument, add it to an empty PDFDocument and
    then release the donor PDFDocument.  Does that make sense?

    Something like:

    PDFDocument    newDocument = [[PDFDocument alloc] init];        // NOTE, may
    only work on Leopard, on Tiger you may need to keep around the first
    donor document below and add pages to it.
    NSRect            pageBounds = NSMakeRect(0, 0, PAGE_WIDE, PAGE_TALL);
    int                p;

    for (p =0; p < numPages; p++)
    {
    // Create new PDFDocumetn for one page.
    PDFDocument *donor = [[PDFDocument alloc] initWithData: [myView
    dataWithPDFInsideRect: pageBounds]];

    // Insert page from donor document into newDocument, release donor.
    [newDocument insertPage: [donor pageAtIndex: 0] atIndex: [newDocument
    pageCount] - 1];
    [donor release];

    // Move rect down.
    pageBounds = NSOffsetRect(pageBounds, 0, PAGE_TALL);
    }

    See if you can get something like that working.

    On Sep 12, 2007, at 11:03 AM, Keith Blount wrote:
    > However, what I would now like to do is generate PDF
    > data from this view that can be used to create a
    > PDFDocument (using PDFDocument's -initWithData:) and
    > then displayed in the PDFView right inside my app. In
    > other words, I want to create a sort of one-click
    > preview of the paginated view right within in my app,
    > without having to go to print > preview and without
    > having to save the PDF to disk.
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today