• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Drawing Text in a PDF Context
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Drawing Text in a PDF Context


  • Subject: Drawing Text in a PDF Context
  • From: douglas welton <email@hidden>
  • Date: Sat, 22 Sep 2012 17:09:43 -0400

Hi All,

I am trying to create a pdf document that contains a grid of images with titles underneath.  I successfully create the pdf with the image, but the text does not appear.

My code, more or less, follows the examples in the Quartz 2D guide for drawing text. But obviously I am doing something incorrectly. The code is posted below.  Using NSLog(), I can validate that the title strings actually contains something and have a valid length.  When I comment out the code for drawing the images, I don't see any title text underneath where the image was placed.

Are there any additional steps I need to take to get the text into my pdf?  Am I missing something obvious?

TIA,

douglas

---

    //+
    //  Create a PDF document to hold the contact sheet items
    //-

    pdfContext = CGPDFContextCreateWithURL( (CFURLRef)targetURL, &paperRectangle, NULL );

    CGContextSelectFont( pdfContext, "Helvetica", 10, kCGEncodingMacRoman );
    CGContextSetCharacterSpacing( pdfContext, 10 );
    CGContextSetStrokeColorWithColor( pdfContext, CGColorGetConstantColor( kCGColorBlack ));
    CGContextSetFillColorWithColor( pdfContext, CGColorGetConstantColor( kCGColorBlack ));
    CGContextSetTextDrawingMode( pdfContext, kCGTextFillStroke );
    CGContextSetTextMatrix( pdfContext, CGAffineTransformIdentity );

    pageInfo = CFDictionaryCreateMutable( NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
    boxData = CFDataCreate( NULL, (const UInt8 *)&paperRectangle, sizeof (CGRect) );
    CFDictionarySetValue( pageInfo, kCGPDFContextMediaBox, boxData );

    pageIndex = 1;

    while ( pageIndex <= self.numberOfPages )
          {
            CGPDFContextBeginPage( pdfContext, pageInfo );

            imageIndex = ( pageIndex - 1 ) * ( self.numberOfColumns * self.numberOfRows );

            rowIndex = 0;
            columnIndex = 0;

            horizontalPosition = leftMargin + paperRectangle.origin.x;
            verticalPosition = CGRectGetMaxY( paperRectangle ) - topMargin - imageHeight;

            //+
            //  Draw all the images on this page
            //-

            while ( imageIndex < [self.contactSheetItems count] && imageIndex < ( pageIndex * ( self.numberOfColumns * self.numberOfRows )))
                  {
                    item = [self.contactSheetItems objectAtIndex: imageIndex];
                    itemImage = (CGImageRef)[item imageRepresentation];

                    targetRectangle = CGRectMake( horizontalPosition, verticalPosition + titleOffset, imageWidth, imageHeight - titleOffset);
                    itemRectangle = AVMakeRectWithAspectRatioInsideRect( CGSizeMake( CGImageGetWidth( itemImage ), CGImageGetHeight( itemImage )),  targetRectangle );

                    CGContextDrawImage( pdfContext, itemRectangle, itemImage );

                    CGContextSetTextPosition( pdfContext, horizontalPosition, itemRectangle.origin.y - titleOffset );
                    CGContextShowText( pdfContext, [[item imageTitle] UTF8String], [[item imageTitle] length] );

                    NSLog( @"-- title: %@, length: %ld", [item imageTitle], [[item imageTitle] length] );

                    columnIndex++;
                    if ( columnIndex >= self.numberOfColumns )
                       {
                         columnIndex = 0;
                         horizontalPosition = leftMargin + paperRectangle.origin.x;

                         rowIndex++;
                         if ( rowIndex >= self.numberOfRows )
                            {
                              rowIndex = 0;
                              verticalPosition = CGRectGetMaxY( paperRectangle ) - imageHeight;
                              }
                            else
                                 verticalPosition = verticalPosition - imageHeight - adjustedIntercellSpacing;
                         }
                       else
                            horizontalPosition = horizontalPosition + imageWidth + adjustedIntercellSpacing;

                    imageIndex++;
                    }

            CGPDFContextEndPage( pdfContext );

            pageIndex++;
            }

    //+
    //  All done
    //-

    CGContextRelease( pdfContext );


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: KVO change dict not reflecting edits in UI
  • Next by Date: Re: Share NSView between processes
  • Previous by thread: Re: KVO change dict not reflecting edits in UI
  • Next by thread: toolbar buttons are missing in iOS 6
  • Index(es):
    • Date
    • Thread