iPDFdev Tips & Tricks for PDF development

IFXPDFFactory – part 6 – Vector graphics

March 12th, 2013

After a long break and finishing the implementation of several internal details that will support the future public APIs, the library is ready to create content. For the beginning I added support for vector graphics: lines, curves, etc.

Lets see how they work.

The page graphics

The entire page area is considered a drawing surface. This drawing surface is actually unlimited, you can draw anywhere but the graphics outside the page area will not be visible. The top left corner of the page visible area is (0, 0), the X grows from left to right, the Y grows from top to bottom. The page visible area is defined by (0, 0) in top left corner and (page.width, page.height) in bottom right corner.

The page drawing surface is represented by the IFXPDFGraphics class. An instance of this class is created automatically when a page is created and it is exposed through the page.graphics property. The methods of this class are used to create the page content: vector graphics, text, images, optional content. Annotations and form fields are not part of page content.

Vector graphics

Vector graphics are actually path objects. A PDF path consists of multiple move to, line to or curve instructions and it is painted on the page using stroke and/or fill operations. The pen objects (IFXPDFPen class) are used for stroking, the brush objects (IFXPDFBrush class) are used for filling. IFXPDFFactory hides the complexity of the drawing process by using simple primitives like lines, arcs, pies, rectangles, ellipses, etc. It also provides a path object if you want to create complex shapes.
All the methods shown below are part of IFXPDFGraphics class. All the coordinates and dimensions are given in PDF points, 1 point = 1/72 inches

Lines

Lines can be drawn using 2 methods:

drawLineWithPen:fromX1:y1:toX2:y2: – draws a line between 2 points specified by their coordinates. The line outline is stroked with the specified pen.

drawLineWithPen:fromPoint:toPoint: – draws a line between 2 points specified by 2 IFXPDFPoint objects. The line outline is stroked with the specified pen.

The code below draws 2 lines between the opposite corners of the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
[page.graphics drawLineWithPen: redPen fromX1: 0 y1: 0 toX2: page.width y2: page.height];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
[page.graphics drawLineWithPen: bluePen fromX1: 0 y1: page.height toX2: page.width y2: 0];
 
[document writeToFile: @"Lines.pdf"];
[document release];

Rectangles

Rectangles can be drawn using 6 methods:

drawRectangleWithPen:atX:y:withWidth:height: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle outline is stroked with the specified pen

drawRectangleWithPen:atX:y:withWidth:height:rotation: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle is rotated around the top left corner with rotation degrees. The rectangle outline is stroked with the specified pen.

drawRectangleWithBrush:atX:y:withWidth:height: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle interior is filled with the specified brush.

drawRectangleWithBrush:atX:y:withWidth:height:rotation: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle is rotated around the top left corner with rotation degrees. The rectangle interior is filled with the specified brush.

drawRectangleWithPen:andBrush:atX:y:withWidth:height: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle outline is stroked with the specified pen. The rectangle interior is filled with the specified brush.

drawRectangleWithPen:andBrush:atX:y:withWidth:height:rotation: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle is rotated around the top left corner with rotation degrees. The rectangle outline is stroked with the specified pen. The rectangle interior is filled with the specified brush.

The code below draws several rectangles on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
 
[page.graphics drawRectangleWithBrush: greenBrush atX: 50 y: 300 withWidth: 200 height:100];
[page.graphics drawRectangleWithPen: redPen atX: 50 y: 300 withWidth: 200 height:100 rotation: 45];
[page.graphics drawRectangleWithPen: bluePen andBrush: yellowBrush atX: 50 y: 300 
                          withWidth: 200 height: 100 rotation:90];
 
[document writeToFile: @"Rectangles.pdf"];
[document release];

Rounded rectangles

Rounded rectangles can be drawn using 6 methods:

drawRoundRectangleWithPen:atX:y:withWidth:height:ellipseWidth:ellipseHeight: – draws a rectangle with the top left corner at x,y having the specified width and height. The rounded corner, a quarter of an ellipse, is defined by ellipseWidth and ellipseHeight. The rectangle outline is stroked with the specified pen.

drawRoundRectangleWithPen:atX:y:withWidth:height:ellipseWidth:ellipseHeight:rotation: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle is rotated around the top left corner with rotation degrees. The rounded corner, a quarter of an ellipse, is defined by ellipseWidth and ellipseHeight. The rectangle outline is stroked with the specified pen.

drawRoundRectangleWithBrush:atX:y:withWidth:height:ellipseWidth:ellipseHeight: – draws a rectangle with the top left corner at x,y having the specified width and height. The rounded corner, a quarter of an ellipse, is defined by ellipseWidth and ellipseHeight. The rectangle interior is filled with the specified brush.

drawRoundRectangleWithBrush:atX:y:withWidth:height:ellipseWidth:ellipseHeight:rotation: – draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle is rotated around the top left corner with rotation degrees. The rounded corner, a quarter of an ellipse, is defined by ellipseWidth and ellipseHeight. The rectangle interior is filled with the specified brush.

drawRoundRectangleWithPen:andBrush:atX:y:withWidth:height:ellipseWidth:ellipseHeight: – draws a rectangle with the top left corner at x,y having the specified width and height. The rounded corner, a quarter of an ellipse, is defined by ellipseWidth and ellipseHeight. The rectangle outline is stroked with the specified pen. The rectangle interior is filled with the specified brush.

drawRoundRectangleWithPen:andBrush:atX:y:
                withWidth:height:ellipseWidth:ellipseHeight:rotation:
– draws a rectangle with the top left corner at x,y having the specified width and height. The rectangle is rotated around the top left corner with rotation degrees. The rounded corner, a quarter of an ellipse, is defined by ellipseWidth and ellipseHeight. The rectangle outline is stroked with the specified pen. The rectangle interior is filled with the specified brush.

The code below draws several rounded rectangles on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
 
[page.graphics drawRoundRectangleWithBrush: greenBrush 
                                       atX: 50 y: 300 
                                 withWidth: 200 height:100 ellipseWidth: 100 ellipseHeight: 50];
[page.graphics drawRoundRectangleWithPen: redPen 
                                     atX: 50 y: 300 
                               withWidth: 200 height:100 ellipseWidth: 100 ellipseHeight: 50 
                                rotation: 45];
[page.graphics drawRoundRectangleWithPen: bluePen andBrush: yellowBrush 
                                     atX: 50 y: 300 
                               withWidth: 200 height: 100 ellipseWidth: 100 ellipseHeight: 50 
                                rotation: 90];
 
[document writeToFile: @"RoundRectangles.pdf"];
[document release];

Elllipses

Ellipses can be drawn using 6 methods:

drawEllipseWithPen:atX:y:withWidth:height: – draws an ellipse with the top left corner at x,y having the specified width and height. The ellipse outline is stroked with the specified pen

drawEllipseWithPen:atX:y:withWidth:height:rotation: – draws an ellipse with the top left corner at x,y having the specified width and height. The ellipse is rotated around the top left corner with rotation degrees. The ellipse outline is stroked with the specified pen.

drawEllipseWithBrush:atX:y:withWidth:height: – draws an ellipse with the top left corner at x,y having the specified width and height. The ellipse interior is filled with the specified brush.

drawEllipseWithBrush:atX:y:withWidth:height:rotation: – draws an ellipse with the top left corner at x,y having the specified width and height. The ellipse is rotated around the top left corner with rotation degrees. The ellipse interior is filled with the specified brush.

drawEllipseWithPen:andBrush:atX:y:withWidth:height: – draws an ellipse with the top left corner at x,y having the specified width and height. The ellipse outline is stroked with the specified pen. The ellipse interior is filled with the specified brush.

drawEllipseWithPen:andBrush:atX:y:withWidth:height:rotation: – draws an ellipse with the top left corner at x,y having the specified width and height. The ellipse is rotated around the top left corner with rotation degrees. The ellipse outline is stroked with the specified pen. The ellipse interior is filled with the specified brush.

The code below draws several ellipses on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
 
[page.graphics drawEllipseWithBrush: greenBrush atX: 50 y: 300 withWidth: 200 height:100];
[page.graphics drawEllipseWithPen: redPen atX: 50 y: 300 withWidth: 200 height:100 rotation: 45];
[page.graphics drawEllipseWithPen: bluePen andBrush: yellowBrush atX: 50 y: 300 
                          withWidth: 200 height: 100 rotation:90];
 
[document writeToFile: @"Ellipse.pdf"];
[document release];

Arcs

Arcs are defined by a bounding ellipse, a start angle and a sweep angle. Arcs can be drawn using 6 methods:

drawArcWithPen:atX:y:withWidth:height:startAngle:sweepAngle: – draws an arc defined by an ellipse with the top left corner at x,y having the specified width and height. The arc outline is stroked with the specified pen

drawArcWithPen:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws an arc defined by an ellipse with the top left corner at x,y having the specified width and height. The arc is rotated around the top left corner with rotation degrees. The arc outline is stroked with the specified pen.

drawArcWithBrush:atX:y:withWidth:height:startAngle:sweepAngle: – draws an arc defined by an ellipse with the top left corner at x,y having the specified width and height. The arc interior is filled with the specified brush.

drawArcWithBrush:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws an arc defined by an ellipse with the top left corner at x,y having the specified width and height. The arc is rotated around the top left corner with rotation degrees. The arc interior is filled with the specified brush.

drawArcWithPen:andBrush:atX:y:withWidth:height:startAngle:sweepAngle: – draws an arc defined by an ellipse with the top left corner at x,y having the specified width and height. The arc outline is stroked with the specified pen. The arc interior is filled with the specified brush.

drawArcWithPen:andBrush:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws an arc defined by an ellipse with the top left corner at x,y having the specified width and height. The arc is rotated around the top left corner with rotation degrees. The arc outline is stroked with the specified pen. The arc interior is filled with the specified brush.

The code below draws several arcs on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
 
[page.graphics drawArcWithPen: redPen
                          atX: 50 y: 50
                    withWidth: 200 height:100
                   startAngle: 45 sweepAngle: 45];
[page.graphics drawArcWithBrush: yellowBrush
                            atX: 50 y: 200
                      withWidth: 200 height:100
                     startAngle: 45 sweepAngle: 90];
[page.graphics drawArcWithPen: bluePen andBrush: greenBrush
                          atX: 50 y: 350
                    withWidth: 200 height:100
                   startAngle: 60 sweepAngle: 135 rotation: 30];
 
[document writeToFile: @"Arcs.pdf"];
[document release];

Pies

Pies are arcs that have their endings connected by line segments with the center of the ellipse. Like arcs, the pies are defined by a bounding ellipse, a start angle and a sweep angle. Pies can be drawn using 6 methods:

drawPieWithPen:atX:y:withWidth:height:startAngle:sweepAngle: – draws a pie defined by an ellipse with the top left corner at x,y having the specified width and height. The pie outline is stroked with the specified pen

drawPieWithPen:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws a pie defined by an ellipse with the top left corner at x,y having the specified width and height. The pie is rotated around the top left corner with rotation degrees. The pie outline is stroked with the specified pen.

drawPieWithBrush:atX:y:withWidth:height:startAngle:sweepAngle: – draws a pie defined by an ellipse with the top left corner at x,y having the specified width and height. The pie interior is filled with the specified brush.

drawPieWithBrush:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws a pie defined by an ellipse with the top left corner at x,y having the specified width and height. The pie is rotated around the top left corner with rotation degrees. The pie interior is filled with the specified brush.

drawPieWithPen:andBrush:atX:y:withWidth:height:startAngle:sweepAngle: – draws a pie defined by an ellipse with the top left corner at x,y having the specified width and height. The pie outline is stroked with the specified pen. The pie interior is filled with the specified brush.

drawPieWithPen:andBrush:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws a pie defined by an ellipse with the top left corner at x,y having the specified width and height. The pie is rotated around the top left corner with rotation degrees. The pie outline is stroked with the specified pen. The pie interior is filled with the specified brush.

The code below draws several pies on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
 
[page.graphics drawPieWithPen: redPen 
                          atX: 50 y: 50 
                    withWidth: 200 height:100 
                   startAngle: 45 sweepAngle: 45];
[page.graphics drawPieWithBrush: greenBrush 
                            atX: 50 y: 200 
                      withWidth: 200 height:100 
                     startAngle: 45 sweepAngle: 90];
[page.graphics drawPieWithPen: bluePen andBrush: yellowBrush 
                          atX: 50 y: 350 
                    withWidth: 200 height:100 
                   startAngle: 60 sweepAngle: 135];
 
[document writeToFile: @"Pies.pdf"];
[document release];

Chords

Chords are arcs that have their endings connected by a line segment. Like arcs, the chords are defined by a bounding ellipse, a start angle and a sweep angle. Chords can be drawn using 6 methods:

drawChordWithPen:atX:y:withWidth:height:startAngle:sweepAngle: – draws a chord defined by an ellipse with the top left corner at x,y having the specified width and height. The chord outline is stroked with the specified pen

drawChordWithPen:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws a chord defined by an ellipse with the top left corner at x,y having the specified width and height. The chord is rotated around the top left corner with rotation degrees. The chord outline is stroked with the specified pen.

drawChordWithBrush:atX:y:withWidth:height:startAngle:sweepAngle: – draws a chord defined by an ellipse with the top left corner at x,y having the specified width and height. The chord interior is filled with the specified brush.

drawChordWithBrush:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws a chord defined by an ellipse with the top left corner at x,y having the specified width and height. The chord is rotated around the top left corner with rotation degrees. The chord interior is filled with the specified brush.

drawChordWithPen:andBrush:atX:y:withWidth:height:startAngle:sweepAngle: – draws a chord defined by an ellipse with the top left corner at x,y having the specified width and height. The chord outline is stroked with the specified pen. The chord interior is filled with the specified brush.

drawChordWithPen:andBrush:atX:y:withWidth:height:startAngle:sweepAngle:rotation: – draws a chord defined by an ellipse with the top left corner at x,y having the specified width and height. The chord is rotated around the top left corner with rotation degrees. The chord outline is stroked with the specified pen. The chord interior is filled with the specified brush.

The code below draws several chords on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
 
[page.graphics drawChordWithPen: redPen 
                            atX: 50 y: 50 
                      withWidth: 200 height:100 
                     startAngle: 45 sweepAngle: 45];
[page.graphics drawChordWithBrush: greenBrush 
                              atX: 50 y: 200 
                        withWidth: 200 height:100 
                       startAngle: 45 sweepAngle: 90];
[page.graphics drawChordWithPen: bluePen andBrush:greenBrush 
                            atX: 50 y: 350 
                      withWidth: 200 height:100 
                     startAngle: 60 sweepAngle: 135];
 
[document writeToFile: @"Chords.pdf"];
[document release];

Bézier curves

Bézier curves can be drawn using 2 methods:

drawBezierWithPen:atX1:y1:x2:y2:x3:y3:x4:y4: - draws a Bézier curve defined by four ordered pairs of coordinates that represent the control points. The curve outline is stroked with the specified pen.

drawBezierWithPen:atPoint1:point2:point3:point4: - draws a Bézier curve specified by four IFXPDFPoint objects. The curve outline is stroked with the specified pen.

The code below draws several Bézier curves on the page.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* redPen = [IFXPDFPen penWithColor: [IFXPDFRgbColor redColor] andWidth: 5];
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
 
[page.graphics drawBezierWithPen: redPen 
                            atX1: 50 y1: 100 x2: 300 y2: 200 x3: 500 y3: 20 x4: 20 y4: 400];
[page.graphics drawBezierWithPen: bluePen 
                            atX1: 200 y1: 70 x2: 600 y2: 700 x3: 500 y3: 120 x4: 300 y4: 100];
 
[document writeToFile: @"Beziers.pdf"];
[document release];

Paths

The path object (IFXPDFPath class) lets you build any shape that is not available in the library. Paths can be created using either the designated initialiser init or with the class method emptyPath.
Once a path object has been created, it provides several methods to create its content (each method updates the current point on the path):

moveToX:y: - begins a new subpath at (x,y)

lineToX:y: - adds a line segment from current point to (x,y)

rectangleAtX:y:withWidth:height:rotation: - add a rectangle to current path

roundRectangleAtX:y:withWidth:height:ellipseWidth:ellipseHeight:rotation: - add a rounded rectangle to current path

ellipseAtX:y:withWidth:height:rotation: - add an ellipse to current path

bezierToX1:y1:x2:y2:x3:y3: - adds a bezier curve to the path, the current points being the first control point.

closeSubpath - closes the current subpath.

Once the path content has been created, it can be drawn on the page using 5 methods:

drawPath:withPen: - strokes the path with the given pen

drawPath:withBrush: - fills the path with the given brush

drawPath:withBrush:fillMode: - fills the path with the given brush using the specified fill mode

drawPath:withPen:andBrush: - strokes the path with the given pen and fills its interior with the given brush

drawPath:withPen:andBrush:fillMode: - strokes the path with the given pen and fills its interior with the given brush using the specified fill mode

The code below creates a simple path and draws it on the page:

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 15];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
 
bluePen.lineJoin = IFXPDFLineJoinBevel;
IFXPDFPath* path = [IFXPDFPath emptyPath];
[path moveToX: 50 y: 50];
[path lineToX: 150 y: 150];
[path lineToX: 250 y: 50];
[path closeSubpath];
[path ellipseAtX: 50 y: 300 withWidth: 300 height:200 rotation: 30];
[page.graphics drawPath: path withPen: bluePen andBrush: greenBrush];
 
[document writeToFile: @"Paths.pdf"];
[document release];

Form XObjects

Form XObjects define a self contained set of vector graphics that can be drawn repeatedly on multiple pages and they are stored only once the in the PDF file. The size of a form XObject is defined by its width and height properties. Its drawing surface is represented by the same IFXPDFGraphics class and it is exposed through the graphics property of the form XObject.
When a form XObject is drawn on another drawing surface, its content is scaled automatically to fit the destination rectangle.
Form XObjects can be created using either the designated initialiser initWidthWidth:andHeight: or with the class method formXObjectWithWidth:andHeight:.
Form XObjects can be drawn using 4 methods:

drawFormXObject:atX:y:withWidth:height: - draws the form XObject with the top left corner at (x, y) and scales the content to fit the given width and height.

drawFormXObject:atX:y:withWidth:height:rotation: - draws the form XObject with the top left corner at (x, y) and scales the content to fit the given width and height. The form XObject is rotated around top left corner with rotation degrees.

drawFormXObject:atX:y:withWidth:height:flip: - draws the form XObject with the top left corner at (x, y) and scales the content to fit the given width and height. The form XObject content is flipped on vertical and/or horizontal according to flip.

drawFormXObject:atX:y:withWidth:height:rotation:flip: - draws the form XObject with the top left corner at (x, y) and scales the content to fit the given width and height. The form XObject is rotated around top left corner with rotation degrees. The form XObject content is flipped on vertical and/or horizontal according to flip.

The code below creates a form XObject, draws 4 pies on it and then draws it on the page 5 times, scaling and flipping the content.

IFXPDFDocument* document = [[IFXPDFDocument alloc] init];
IFXPDFPage* page = [IFXPDFPage emptyPage];
[document.pages addPage: page];
 
IFXPDFPen* bluePen = [IFXPDFPen penWithColor: [IFXPDFRgbColor blueColor] andWidth: 1];
IFXPDFBrush* greenBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor greenColor]];
IFXPDFBrush* yellowBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor yellowColor]];
IFXPDFBrush* redBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor redColor]];
IFXPDFBrush* blueBrush = [IFXPDFBrush brushWithColor: [IFXPDFRgbColor blueColor]];
 
// Create a form XObject, 200*100 points in size
IFXPDFFormXObject* xo = [IFXPDFFormXObject formXObjectWithWidth: 200 andHeight: 100];
// Draw an ellipse on the form XObject
[xo.graphics drawEllipseWithPen: bluePen andBrush: greenBrush 
                            atX: 10 y: 10 
                      withWidth: 180 height: 80];
// Draw the form XObject on the page, its content is scaled to 500*700 points.
[page.graphics drawFormXObject: xo atX: 50 y: 50 withWidth: 500 height: 700];
 
xo = [IFXPDFFormXObject formXObjectWithWidth: 200 andHeight: 200];
[xo.graphics drawPieWithBrush: greenBrush 
                          atX: 0 y: 0 
                    withWidth: xo.width height: xo.height 
                   startAngle: 0 sweepAngle: 90];
[xo.graphics drawPieWithBrush: redBrush 
                          atX: 0 y: 0 
                    withWidth: xo.width height: xo.height 
                   startAngle: 90 sweepAngle: 90];
[xo.graphics drawPieWithBrush: blueBrush 
                          atX: 0 y: 0 
                    withWidth: xo.width height: xo.height 
                   startAngle: 180 sweepAngle: 90];
[xo.graphics drawPieWithBrush: yellowBrush 
                          atX: 0 y: 0 
                    withWidth: xo.width height: xo.height 
                   startAngle: 270 sweepAngle: 90];
 
// Draw the form XObject on the page 4 times and flip its content on vertical and/or horizontal
[page.graphics drawFormXObject: xo 
                           atX: 50 y: 50 withWidth: 200 height: 200 
                          flip: IFXPDFContentNoFlip];
[page.graphics drawFormXObject: xo 
                           atX: 400 y: 50 withWidth: 200 height: 200 
                          flip: IFXPDFContentVerticalFlip];
[page.graphics drawFormXObject: xo 
                           atX: 50 y: 500 withWidth: 200 height: 200 
                          flip: IFXPDFContentHorizontalFlip];
[page.graphics drawFormXObject: xo 
                           atX: 400 y: 500 withWidth: 200 height: 200 
                          flip: IFXPDFContentVerticalFlip | IFXPDFContentHorizontalFlip];
 
[document writeToFile: @"FormXObjects.pdf"];
[document release];

The generated PDF files can be downloaded here: VectorGraphics.zip

Comments (4) Trackbacks (0)
  1. Hi,
    I am trying to implement an export to pdf option for my iOS app that uses html natively. I also embed svg images and it would be great if I could export them as pdf vector graphics. This is the first library I find that shows promise and is usable in iOS. When will the library be available and for what price? Is it possible to get the current version even if working in progress?

    • The library will be available later this year and the planned price is US$299 per license. Current version supports what is described on the website but if you are interested I can make it available to you. I plan to support SVG images but this will be in a later release, maybe next year.

  2. Hi,

    This is a Great framework !

    Is there a possibility to convert svg content into The pdf files ?

    Regards

    Peter


Cancel reply

No trackbacks yet.