org.apache.pdfbox.pdmodel.edit
Class PDPageContentStream

java.lang.Object
  extended by org.apache.pdfbox.pdmodel.edit.PDPageContentStream

public class PDPageContentStream
extends Object

This class will is a convenience for creating page content streams. You MUST call close() when you are finished with this object.

Version:
$Revision: 1.19 $
Author:
Ben Litchfield

Constructor Summary
PDPageContentStream(PDDocument document, PDPage sourcePage)
          Create a new PDPage content stream.
PDPageContentStream(PDDocument document, PDPage sourcePage, boolean appendContent, boolean compress)
          Create a new PDPage content stream.
PDPageContentStream(PDDocument document, PDPage sourcePage, boolean appendContent, boolean compress, boolean resetContext)
          Create a new PDPage content stream.
 
Method Summary
 void addBezier31(float x1, float y1, float x3, float y3)
          Append a cubic Bézier curve to the current path.
 void addBezier312(float x1, float y1, float x2, float y2, float x3, float y3)
          Append a cubic Bézier curve to the current path.
 void addBezier32(float x2, float y2, float x3, float y3)
          Append a cubic Bézier curve to the current path.
 void addLine(float xStart, float yStart, float xEnd, float yEnd)
          add a line to the current path.
 void addPolygon(float[] x, float[] y)
          Add a polygon to the current path.
 void addRect(float x, float y, float width, float height)
          Add a rectangle to the current path.
 void appendCOSName(COSName name)
          This will append a COSName to the content stream.
 void appendRawCommands(byte[] commands)
          This will append raw commands to the content stream.
 void appendRawCommands(int data)
          This will append raw commands to the content stream.
 void appendRawCommands(String commands)
          This will append raw commands to the content stream.
 void beginMarkedContentSequence(COSName tag)
          Begin a marked content sequence.
 void beginMarkedContentSequence(COSName tag, COSName propsName)
          Begin a marked content sequence with a reference to an entry in the page resources' Properties dictionary.
 void beginText()
          Begin some text operations.
 void clipPath(int windingRule)
          Clip path.
 void close()
          Close the content stream.
 void closeAndStroke()
          Close and stroke the path.
 void closeSubPath()
          Close subpath.
 void concatenate2CTM(AffineTransform at)
          The Cm operator.
 void concatenate2CTM(double a, double b, double c, double d, double e, double f)
          The Cm operator.
 void drawImage(PDXObjectImage image, float x, float y)
          Draw an image at the x,y coordinates, with the default size of the image.
 void drawLine(float xStart, float yStart, float xEnd, float yEnd)
          Draw a line on the page using the current non stroking color and the current line width.
 void drawPolygon(float[] x, float[] y)
          Draw a polygon on the page using the current non stroking color.
 void drawString(String text)
          This will draw a string at the current location on the screen.
 void drawXObject(PDXObject xobject, AffineTransform transform)
          Draw an xobject(form or image) using the given AffineTransform to position the xobject.
 void drawXObject(PDXObject xobject, float x, float y, float width, float height)
          Draw an xobject(form or image) at the x,y coordinates and a certain width and height.
 void endMarkedContentSequence()
          End a marked content sequence.
 void endText()
          End some text operations.
 void fill(int windingRule)
          Fill the path.
 void fillPolygon(float[] x, float[] y)
          Draw and fill a polygon on the page using the current non stroking color.
 void fillRect(float x, float y, float width, float height)
          Draw a rectangle on the page using the current non stroking color.
 void lineTo(float x, float y)
          Add a move to the given coordinate.
 void moveTextPositionByAmount(float x, float y)
          The Td operator.
 void moveTo(float x, float y)
          Add a line to the given coordinate.
 void restoreGraphicsState()
          Q operator.
 void saveGraphicsState()
          q operator.
 void setFont(PDFont font, float fontSize)
          Set the font to draw text with.
 void setLineWidth(float lineWidth)
          Set linewidth to the given value.
 void setNonStrokingColor(Color color)
          Set the non stroking color, specified as RGB.
 void setNonStrokingColor(double g)
          Set the non stroking color, specified as Grayscale 0.0-1.0.
 void setNonStrokingColor(double c, double m, double y, double k)
          Set the non stroking color, specified as CMYK, 0.0-1.0.
 void setNonStrokingColor(float[] components)
          Set the color components of current non stroking colorspace.
 void setNonStrokingColor(int g)
          Set the non stroking color, specified as grayscale, 0-255.
 void setNonStrokingColor(int r, int g, int b)
          Set the non stroking color, specified as RGB, 0-255.
 void setNonStrokingColor(int c, int m, int y, int k)
          Set the non stroking color, specified as CMYK, 0-255.
 void setNonStrokingColorSpace(PDColorSpace colorSpace)
          Set the stroking color space.
 void setStrokingColor(Color color)
          Set the stroking color, specified as RGB.
 void setStrokingColor(double g)
          Set the stroking color, specified as Grayscale 0.0-1.0.
 void setStrokingColor(double c, double m, double y, double k)
          Set the stroking color, specified as CMYK, 0.0-1.0.
 void setStrokingColor(float[] components)
          Set the color components of current stroking colorspace.
 void setStrokingColor(int g)
          Set the stroking color, specified as grayscale, 0-255.
 void setStrokingColor(int r, int g, int b)
          Set the stroking color, specified as RGB, 0-255.
 void setStrokingColor(int c, int m, int y, int k)
          Set the stroking color, specified as CMYK, 0-255.
 void setStrokingColorSpace(PDColorSpace colorSpace)
          Set the stroking color space.
 void setTextMatrix(AffineTransform matrix)
          The Tm operator.
 void setTextMatrix(double a, double b, double c, double d, double e, double f)
          The Tm operator.
 void setTextRotation(double angle, double tx, double ty)
          The Tm operator.
 void setTextScaling(double sx, double sy, double tx, double ty)
          The Tm operator.
 void setTextTranslation(double tx, double ty)
          The Tm operator.
 void stroke()
          Stroke the path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PDPageContentStream

public PDPageContentStream(PDDocument document,
                           PDPage sourcePage)
                    throws IOException
Create a new PDPage content stream.

Parameters:
document - The document the page is part of.
sourcePage - The page to write the contents to.
Throws:
IOException - If there is an error writing to the page contents.

PDPageContentStream

public PDPageContentStream(PDDocument document,
                           PDPage sourcePage,
                           boolean appendContent,
                           boolean compress)
                    throws IOException
Create a new PDPage content stream.

Parameters:
document - The document the page is part of.
sourcePage - The page to write the contents to.
appendContent - Indicates whether content will be overwritten. If false all previous content is deleted.
compress - Tell if the content stream should compress the page contents.
Throws:
IOException - If there is an error writing to the page contents.

PDPageContentStream

public PDPageContentStream(PDDocument document,
                           PDPage sourcePage,
                           boolean appendContent,
                           boolean compress,
                           boolean resetContext)
                    throws IOException
Create a new PDPage content stream.

Parameters:
document - The document the page is part of.
sourcePage - The page to write the contents to.
appendContent - Indicates whether content will be overwritten. If false all previous content is deleted.
compress - Tell if the content stream should compress the page contents.
resetContext - Tell if the graphic context should be reseted.
Throws:
IOException - If there is an error writing to the page contents.
Method Detail

beginText

public void beginText()
               throws IOException
Begin some text operations.

Throws:
IOException - If there is an error writing to the stream or if you attempt to nest beginText calls.

endText

public void endText()
             throws IOException
End some text operations.

Throws:
IOException - If there is an error writing to the stream or if you attempt to nest endText calls.

setFont

public void setFont(PDFont font,
                    float fontSize)
             throws IOException
Set the font to draw text with.

Parameters:
font - The font to use.
fontSize - The font size to draw the text.
Throws:
IOException - If there is an error writing the font information.

drawImage

public void drawImage(PDXObjectImage image,
                      float x,
                      float y)
               throws IOException
Draw an image at the x,y coordinates, with the default size of the image.

Parameters:
image - The image to draw.
x - The x-coordinate to draw the image.
y - The y-coordinate to draw the image.
Throws:
IOException - If there is an error writing to the stream.

drawXObject

public void drawXObject(PDXObject xobject,
                        float x,
                        float y,
                        float width,
                        float height)
                 throws IOException
Draw an xobject(form or image) at the x,y coordinates and a certain width and height.

Parameters:
xobject - The xobject to draw.
x - The x-coordinate to draw the image.
y - The y-coordinate to draw the image.
width - The width of the image to draw.
height - The height of the image to draw.
Throws:
IOException - If there is an error writing to the stream.

drawXObject

public void drawXObject(PDXObject xobject,
                        AffineTransform transform)
                 throws IOException
Draw an xobject(form or image) using the given AffineTransform to position the xobject.

Parameters:
xobject - The xobject to draw.
transform - the transformation matrix
Throws:
IOException - If there is an error writing to the stream.

moveTextPositionByAmount

public void moveTextPositionByAmount(float x,
                                     float y)
                              throws IOException
The Td operator. A current text matrix will be replaced with a new one (1 0 0 1 x y).

Parameters:
x - The x coordinate.
y - The y coordinate.
Throws:
IOException - If there is an error writing to the stream.

setTextMatrix

public void setTextMatrix(double a,
                          double b,
                          double c,
                          double d,
                          double e,
                          double f)
                   throws IOException
The Tm operator. Sets the text matrix to the given values. A current text matrix will be replaced with the new one.

Parameters:
a - The a value of the matrix.
b - The b value of the matrix.
c - The c value of the matrix.
d - The d value of the matrix.
e - The e value of the matrix.
f - The f value of the matrix.
Throws:
IOException - If there is an error writing to the stream.

setTextMatrix

public void setTextMatrix(AffineTransform matrix)
                   throws IOException
The Tm operator. Sets the text matrix to the given values. A current text matrix will be replaced with the new one.

Parameters:
matrix - the transformation matrix
Throws:
IOException - If there is an error writing to the stream.

setTextScaling

public void setTextScaling(double sx,
                           double sy,
                           double tx,
                           double ty)
                    throws IOException
The Tm operator. Sets the text matrix to the given scaling and translation values. A current text matrix will be replaced with the new one.

Parameters:
sx - The scaling factor in x-direction.
sy - The scaling factor in y-direction.
tx - The translation value in x-direction.
ty - The translation value in y-direction.
Throws:
IOException - If there is an error writing to the stream.

setTextTranslation

public void setTextTranslation(double tx,
                               double ty)
                        throws IOException
The Tm operator. Sets the text matrix to the given translation values. A current text matrix will be replaced with the new one.

Parameters:
tx - The translation value in x-direction.
ty - The translation value in y-direction.
Throws:
IOException - If there is an error writing to the stream.

setTextRotation

public void setTextRotation(double angle,
                            double tx,
                            double ty)
                     throws IOException
The Tm operator. Sets the text matrix to the given rotation and translation values. A current text matrix will be replaced with the new one.

Parameters:
angle - The angle used for the counterclockwise rotation in radians.
tx - The translation value in x-direction.
ty - The translation value in y-direction.
Throws:
IOException - If there is an error writing to the stream.

concatenate2CTM

public void concatenate2CTM(double a,
                            double b,
                            double c,
                            double d,
                            double e,
                            double f)
                     throws IOException
The Cm operator. Concatenates the current transformation matrix with the given values.

Parameters:
a - The a value of the matrix.
b - The b value of the matrix.
c - The c value of the matrix.
d - The d value of the matrix.
e - The e value of the matrix.
f - The f value of the matrix.
Throws:
IOException - If there is an error writing to the stream.

concatenate2CTM

public void concatenate2CTM(AffineTransform at)
                     throws IOException
The Cm operator. Concatenates the current transformation matrix with the given AffineTransform.

Parameters:
at - the transformation matrix
Throws:
IOException - If there is an error writing to the stream.

drawString

public void drawString(String text)
                throws IOException
This will draw a string at the current location on the screen.

Parameters:
text - The text to draw.
Throws:
IOException - If an io exception occurs.

setStrokingColorSpace

public void setStrokingColorSpace(PDColorSpace colorSpace)
                           throws IOException
Set the stroking color space. This will add the colorspace to the PDResources if necessary.

Parameters:
colorSpace - The colorspace to write.
Throws:
IOException - If there is an error writing the colorspace.

setNonStrokingColorSpace

public void setNonStrokingColorSpace(PDColorSpace colorSpace)
                              throws IOException
Set the stroking color space. This will add the colorspace to the PDResources if necessary.

Parameters:
colorSpace - The colorspace to write.
Throws:
IOException - If there is an error writing the colorspace.

setStrokingColor

public void setStrokingColor(float[] components)
                      throws IOException
Set the color components of current stroking colorspace.

Parameters:
components - The components to set for the current color.
Throws:
IOException - If there is an error while writing to the stream.

setStrokingColor

public void setStrokingColor(Color color)
                      throws IOException
Set the stroking color, specified as RGB.

Parameters:
color - The color to set.
Throws:
IOException - If an IO error occurs while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(Color color)
                         throws IOException
Set the non stroking color, specified as RGB.

Parameters:
color - The color to set.
Throws:
IOException - If an IO error occurs while writing to the stream.

setStrokingColor

public void setStrokingColor(int r,
                             int g,
                             int b)
                      throws IOException
Set the stroking color, specified as RGB, 0-255.

Parameters:
r - The red value.
g - The green value.
b - The blue value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setStrokingColor

public void setStrokingColor(int c,
                             int m,
                             int y,
                             int k)
                      throws IOException
Set the stroking color, specified as CMYK, 0-255.

Parameters:
c - The cyan value.
m - The magenta value.
y - The yellow value.
k - The black value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setStrokingColor

public void setStrokingColor(double c,
                             double m,
                             double y,
                             double k)
                      throws IOException
Set the stroking color, specified as CMYK, 0.0-1.0.

Parameters:
c - The cyan value.
m - The magenta value.
y - The yellow value.
k - The black value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setStrokingColor

public void setStrokingColor(int g)
                      throws IOException
Set the stroking color, specified as grayscale, 0-255.

Parameters:
g - The gray value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setStrokingColor

public void setStrokingColor(double g)
                      throws IOException
Set the stroking color, specified as Grayscale 0.0-1.0.

Parameters:
g - The gray value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(float[] components)
                         throws IOException
Set the color components of current non stroking colorspace.

Parameters:
components - The components to set for the current color.
Throws:
IOException - If there is an error while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(int r,
                                int g,
                                int b)
                         throws IOException
Set the non stroking color, specified as RGB, 0-255.

Parameters:
r - The red value.
g - The green value.
b - The blue value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(int c,
                                int m,
                                int y,
                                int k)
                         throws IOException
Set the non stroking color, specified as CMYK, 0-255.

Parameters:
c - The cyan value.
m - The magenta value.
y - The yellow value.
k - The black value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(double c,
                                double m,
                                double y,
                                double k)
                         throws IOException
Set the non stroking color, specified as CMYK, 0.0-1.0.

Parameters:
c - The cyan value.
m - The magenta value.
y - The yellow value.
k - The black value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(int g)
                         throws IOException
Set the non stroking color, specified as grayscale, 0-255.

Parameters:
g - The gray value.
Throws:
IOException - If an IO error occurs while writing to the stream.

setNonStrokingColor

public void setNonStrokingColor(double g)
                         throws IOException
Set the non stroking color, specified as Grayscale 0.0-1.0.

Parameters:
g - The gray value.
Throws:
IOException - If an IO error occurs while writing to the stream.

addRect

public void addRect(float x,
                    float y,
                    float width,
                    float height)
             throws IOException
Add a rectangle to the current path.

Parameters:
x - The lower left x coordinate.
y - The lower left y coordinate.
width - The width of the rectangle.
height - The height of the rectangle.
Throws:
IOException - If there is an error while drawing on the screen.

fillRect

public void fillRect(float x,
                     float y,
                     float width,
                     float height)
              throws IOException
Draw a rectangle on the page using the current non stroking color.

Parameters:
x - The lower left x coordinate.
y - The lower left y coordinate.
width - The width of the rectangle.
height - The height of the rectangle.
Throws:
IOException - If there is an error while drawing on the screen.

addBezier312

public void addBezier312(float x1,
                         float y1,
                         float x2,
                         float y2,
                         float x3,
                         float y3)
                  throws IOException
Append a cubic Bézier curve to the current path. The curve extends from the current point to the point (x3 , y3 ), using (x1 , y1 ) and (x2 , y2 ) as the Bézier control points

Parameters:
x1 - x coordinate of the point 1
y1 - y coordinate of the point 1
x2 - x coordinate of the point 2
y2 - y coordinate of the point 2
x3 - x coordinate of the point 3
y3 - y coordinate of the point 3
Throws:
IOException - If there is an error while adding the .

addBezier32

public void addBezier32(float x2,
                        float y2,
                        float x3,
                        float y3)
                 throws IOException
Append a cubic Bézier curve to the current path. The curve extends from the current point to the point (x3 , y3 ), using the current point and (x2 , y2 ) as the Bézier control points

Parameters:
x2 - x coordinate of the point 2
y2 - y coordinate of the point 2
x3 - x coordinate of the point 3
y3 - y coordinate of the point 3
Throws:
IOException - If there is an error while adding the .

addBezier31

public void addBezier31(float x1,
                        float y1,
                        float x3,
                        float y3)
                 throws IOException
Append a cubic Bézier curve to the current path. The curve extends from the current point to the point (x3 , y3 ), using (x1 , y1 ) and (x3 , y3 ) as the Bézier control points

Parameters:
x1 - x coordinate of the point 1
y1 - y coordinate of the point 1
x3 - x coordinate of the point 3
y3 - y coordinate of the point 3
Throws:
IOException - If there is an error while adding the .

moveTo

public void moveTo(float x,
                   float y)
            throws IOException
Add a line to the given coordinate.

Parameters:
x - The x coordinate.
y - The y coordinate.
Throws:
IOException - If there is an error while adding the line.

lineTo

public void lineTo(float x,
                   float y)
            throws IOException
Add a move to the given coordinate.

Parameters:
x - The x coordinate.
y - The y coordinate.
Throws:
IOException - If there is an error while adding the line.

addLine

public void addLine(float xStart,
                    float yStart,
                    float xEnd,
                    float yEnd)
             throws IOException
add a line to the current path.

Parameters:
xStart - The start x coordinate.
yStart - The start y coordinate.
xEnd - The end x coordinate.
yEnd - The end y coordinate.
Throws:
IOException - If there is an error while adding the line.

drawLine

public void drawLine(float xStart,
                     float yStart,
                     float xEnd,
                     float yEnd)
              throws IOException
Draw a line on the page using the current non stroking color and the current line width.

Parameters:
xStart - The start x coordinate.
yStart - The start y coordinate.
xEnd - The end x coordinate.
yEnd - The end y coordinate.
Throws:
IOException - If there is an error while drawing on the screen.

addPolygon

public void addPolygon(float[] x,
                       float[] y)
                throws IOException
Add a polygon to the current path.

Parameters:
x - x coordinate of each points
y - y coordinate of each points
Throws:
IOException - If there is an error while drawing on the screen.

drawPolygon

public void drawPolygon(float[] x,
                        float[] y)
                 throws IOException
Draw a polygon on the page using the current non stroking color.

Parameters:
x - x coordinate of each points
y - y coordinate of each points
Throws:
IOException - If there is an error while drawing on the screen.

fillPolygon

public void fillPolygon(float[] x,
                        float[] y)
                 throws IOException
Draw and fill a polygon on the page using the current non stroking color.

Parameters:
x - x coordinate of each points
y - y coordinate of each points
Throws:
IOException - If there is an error while drawing on the screen.

stroke

public void stroke()
            throws IOException
Stroke the path.

Throws:
IOException

closeAndStroke

public void closeAndStroke()
                    throws IOException
Close and stroke the path.

Throws:
IOException

fill

public void fill(int windingRule)
          throws IOException
Fill the path.

Throws:
IOException

closeSubPath

public void closeSubPath()
                  throws IOException
Close subpath.

Throws:
IOException

clipPath

public void clipPath(int windingRule)
              throws IOException
Clip path.

Throws:
IOException

setLineWidth

public void setLineWidth(float lineWidth)
                  throws IOException
Set linewidth to the given value.

Parameters:
lineWidth - The width which is used for drwaing.
Throws:
IOException - If there is an error while drawing on the screen.

beginMarkedContentSequence

public void beginMarkedContentSequence(COSName tag)
                                throws IOException
Begin a marked content sequence.

Parameters:
tag - the tag
Throws:
IOException - if an I/O error occurs

beginMarkedContentSequence

public void beginMarkedContentSequence(COSName tag,
                                       COSName propsName)
                                throws IOException
Begin a marked content sequence with a reference to an entry in the page resources' Properties dictionary.

Parameters:
tag - the tag
propsName - the properties reference
Throws:
IOException - if an I/O error occurs

endMarkedContentSequence

public void endMarkedContentSequence()
                              throws IOException
End a marked content sequence.

Throws:
IOException - if an I/O error occurs

saveGraphicsState

public void saveGraphicsState()
                       throws IOException
q operator. Saves the current graphics state.

Throws:
IOException - If an error occurs while writing to the stream.

restoreGraphicsState

public void restoreGraphicsState()
                          throws IOException
Q operator. Restores the current graphics state.

Throws:
IOException - If an error occurs while writing to the stream.

appendRawCommands

public void appendRawCommands(String commands)
                       throws IOException
This will append raw commands to the content stream.

Parameters:
commands - The commands to append to the stream.
Throws:
IOException - If an error occurs while writing to the stream.

appendRawCommands

public void appendRawCommands(byte[] commands)
                       throws IOException
This will append raw commands to the content stream.

Parameters:
commands - The commands to append to the stream.
Throws:
IOException - If an error occurs while writing to the stream.

appendRawCommands

public void appendRawCommands(int data)
                       throws IOException
This will append raw commands to the content stream.

Parameters:
data - Append a raw byte to the stream.
Throws:
IOException - If an error occurs while writing to the stream.

appendCOSName

public void appendCOSName(COSName name)
                   throws IOException
This will append a COSName to the content stream.

Parameters:
name - the name
Throws:
IOException - If an error occurs while writing to the stream.

close

public void close()
           throws IOException
Close the content stream. This must be called when you are done with this object.

Throws:
IOException - If the underlying stream has a problem being written to.


Copyright © 2002-2012 Apache Software Foundation. All Rights Reserved.