Class PdfManipulator

java.lang.Object
org.jpedal.manipulator.PdfManipulator

public final class PdfManipulator extends Object
PDF Manipulator allows for editing PDF documents.
This class supports in memory editing and method chaining which reduces the number of writes to disk.

Example usage:

  
     final PdfManipulator pdfManipulator = new PdfManipulator();
     pdfManipulator.loadDocument(new File("inputFile.pdf"));
     pdfManipulator.addPage(1, PaperSize.A4_PORTRAIT)
          .addText(1, "Hello World", 10, 10, BaseFont.TimesRoman, 12, 1.0f, 0.2f, 0.2f)
          .scalePageContent(1, 0.5f, 0.5f, ScalePageContent.TOP_RIGHT)
          .apply();
     pdfManipulator.writeDocument(new File("outputFile.pdf"));
     pdfManipulator.closeDocument();
 
 
  • Constructor Details

    • PdfManipulator

      public PdfManipulator()
      Instantiate a new PDF Manipulator
  • Method Details

    • main

      public static void main(String[] args) throws IOException
      Perform a single manipulation operation on a PDF file
      Parameters:
      args - the arguments for the chosen operation. Run with '--help' to see parameters
      Throws:
      IOException - if a file handling or document processing exception occurs
    • loadDocument

      public PdfManipulator loadDocument(File input) throws IOException
      Load a new PDF document into memory from a File.
      Replaces the currently loaded document, you should call closeDocument() before calling this method.
      Parameters:
      input - the document to load
      Throws:
      IOException - if a file handling exception occurs
    • loadDocument

      public PdfManipulator loadDocument(byte[] input) throws IOException
      Load a new PDF document into memory from a byte[].
      Replaces the currently loaded document, you should call closeDocument() before calling this method.
      Parameters:
      input - the document to load
      Throws:
      IOException - if a document loading exception occurs
    • loadDocument

      public PdfManipulator loadDocument(File input, byte[] password) throws IOException
      Load a new PDF document into memory from a File.
      Replaces the currently loaded document, you should call closeDocument() before calling this method.
      Parameters:
      input - the document to load
      password - the password to open the document with
      Throws:
      IOException - if a file handling exception occurs
    • loadDocument

      public PdfManipulator loadDocument(byte[] input, byte[] password) throws IOException
      Load a new PDF document into memory from a byte[].
      Replaces the currently loaded document, you should call closeDocument() before calling this method.
      Parameters:
      input - the document to load
      password - the password to open the document with
      Throws:
      IOException - if a document loading exception occurs
    • newDocument

      public PdfManipulator newDocument(float[] mediaBox) throws IOException
      Create a new PDF document with a single blank page.
      Replaces the currently loaded document, you should call closeDocument() before calling this method.

      Note: If you want to remove the default page, you should first add a new page before removing the included page otherwise the document will enter an invalid state.

      Parameters:
      mediaBox - the size for the included page
      Throws:
      IOException - if the document cannot be created
    • writeDocument

      public boolean writeDocument(File output) throws IOException
      Writes the currently loaded PDF document, with any modifications applied, to a File.
      Warning: this method allows overwriting of files!
      Parameters:
      output - the destination file to write to
      Returns:
      true if the renaming operation succeeded, false otherwise.
      Throws:
      IOException - if a file handling exception occurs
      Implementation Note:
      This method first writes the PDF document to a temporary .tmp file before renaming it to the destination file.
    • writeDocument

      public byte[] writeDocument() throws IOException
      Writes the currently loaded PDF document, with any modifications applied, to a byte[].
      Returns:
      the resultant document
      Throws:
      IOException - if a document writing exception occurs
    • closeDocument

      public PdfManipulator closeDocument() throws IOException
      Closes and frees the currently loaded document.
      You should call this before loading a new document.
      Throws:
      IOException - if a document closing exception occurs
    • apply

      public PdfManipulator apply() throws IOException
      Applies all the added manipulations, in the order that they were added
      Throws:
      IOException - if the document cannot be processed
    • reset

      public PdfManipulator reset()
      Clears the list of added manipulations
    • addPage

      public PdfManipulator addPage(PageRanges pages, float[] mediaBox)
      Inserts a new blank page, before the specified page.

      Note: each new page is inserted relative to the original document state. For example, inserting pages 1–3 into a 5-page document results in: new 1, old 1, new 2, old 2, new 3, old 3, old 4, old 5 not: new 1, new 2, new 3, old 1, old 2, old 3, old 4, old 5

      Parameters:
      pages - the pages which the new pages will be inserted before, all following pages are shifted by 1. Page indexes start from 1. To append a page to the end of the file, supply 1 higher than the file page index.
      mediaBox - the media box (dimensions) of the page in the form [lower-left x, lower-left y, upper-right x, upper-right y]. See PaperSize for common paper sizes.
    • addPage

      public PdfManipulator addPage(int page, float[] mediaBox)
      Inserts a new blank page, before the specified page.
      Parameters:
      page - the page number which the new page will be inserted before, all following pages are shifted by 1. Page indexes start from 1. To append a page to the end of the file, supply 1 higher than the file page index.
      mediaBox - the media box (dimensions) of the page in the form [lower-left x, lower-left y, upper-right x, upper-right y]. See PaperSize for common paper sizes.
    • removePage

      public PdfManipulator removePage(PageRanges pages)
      Deletes the specified pages.
      Parameters:
      pages - the pages to delete in the new file
    • removePage

      public PdfManipulator removePage(int page)
      Deletes the specified pages.
      Parameters:
      page - the pages to delete in the new file
    • addImage

      public PdfManipulator addImage(PageRanges pages, BufferedImage image, float[] rect)
      Adds an image onto the specified pages.
      Parameters:
      pages - the pages to add the image to
      image - the image to add
      rect - a rectangle specifying the origin and dimensions of the image
    • addImage

      public PdfManipulator addImage(int page, BufferedImage image, float[] rect)
      Adds an image onto the specified page.
      Parameters:
      page - the page to add the image to
      image - the image to add
      rect - a rectangle specifying the origin and dimensions of the image
    • getPageMediaBox

      public List<float[]> getPageMediaBox(PageRanges pages) throws IOException
      Returns the media box of the specified pages.
      Parameters:
      pages - the pages to query
      Returns:
      the media box as a List of float[]
      Throws:
      IOException - if a file handling exception occurs
    • getPageMediaBox

      public float[] getPageMediaBox(int page) throws IOException
      Returns the media box of the specified page.
      Parameters:
      page - the page to query
      Returns:
      the media box as a float[]
      Throws:
      IOException - if a file handling exception occurs
    • getPageRotation

      public List<Integer> getPageRotation(PageRanges pages) throws IOException
      Returns the rotation of the specified pages.
      Parameters:
      pages - the pages to query
      Returns:
      the rotation as a clockwise angle in degrees or 0 if no rotation is found
      Throws:
      IOException - if a file handling exception occurs
    • getPageRotation

      public int getPageRotation(int page) throws IOException
      Returns the rotation of the specified pages.
      Parameters:
      page - the page to query
      Returns:
      the rotation as a clockwise angle in degrees or 0 if no rotation is found
      Throws:
      IOException - if a file handling exception occurs
    • getPageCropBox

      public List<float[]> getPageCropBox(PageRanges pages) throws IOException
      Returns the crop box of the specified page.
      Parameters:
      pages - the pages to query
      Returns:
      the crop box as a List of float[]
      Throws:
      IOException - if a file handling exception occurs
    • getPageCropBox

      public float[] getPageCropBox(int page) throws IOException
      Returns the crop box of the specified page.
      Parameters:
      page - the page to query
      Returns:
      the crop box as a float[]
      Throws:
      IOException - if a file handling exception occurs
    • getPageCount

      public int getPageCount() throws IOException
      Returns the number of pages in the document.
      Returns:
      the number of pages
      Throws:
      IOException - if a file handling exception occurs
    • scalePage

      public PdfManipulator scalePage(PageRanges pages, float scaleX, float scaleY)
      Scales the specified page.
      Parameters:
      pages - the pages to scale
      scaleX - the horizontal scaling factor. 100% is 1.0f
      scaleY - the vertical scaling factor. 100% is 1.0f
    • scalePage

      public PdfManipulator scalePage(int page, float scaleX, float scaleY)
      Scales the specified page.
      Parameters:
      page - the page to scale
      scaleX - the horizontal scaling factor. 100% is 1.0f
      scaleY - the vertical scaling factor. 100% is 1.0f
    • scalePageContent

      public PdfManipulator scalePageContent(PageRanges pages, float scaleX, float scaleY, float translateX, float translateY)
      Scales the specified pages' contents and translates the scaled content, leaving the page dimensions intact.
      Scaling values must be expressed as a fraction.
      Parameters:
      pages - the pages whose content to scale
      scaleX - the horizontal scaling factor, where 100% == 1.0f. Values greater than 1.0f may result in content appearing outside the media box.
      scaleY - the vertical scaling factor, where 100% == 1.0f Values greater than 1.0f may result in content appearing outside the media box.
      translateX - the horizontal translation value. Values outside the range [0.0, mediaBoxWidth - (mediaBoxWidth * shrinkX)] may result in content appearing outside the media box. You may query a page's media box using getPageMediaBox(int).
      translateY - the vertical translation value. Values outside the range [0.0, mediaBoxHeight - (mediaBoxHeight * shrinkY)] may result in content appearing outside the media box. You may query a page's media box using getPageMediaBox(int).
    • scalePageContent

      public PdfManipulator scalePageContent(int page, float scaleX, float scaleY, float translateX, float translateY)
      Scales the specified page's contents and translates the scaled content, leaving the page dimensions intact.
      Scaling values must be expressed as a fraction.
      Parameters:
      page - the page whose content is to be scaled
      scaleX - the horizontal scaling factor, where 100% == 1.0f. Values greater than 1.0f may result in content appearing outside the media box.
      scaleY - the vertical scaling factor, where 100% == 1.0f Values greater than 1.0f may result in content appearing outside the media box.
      translateX - the horizontal translation value. Values outside the range [0.0, mediaBoxWidth - (mediaBoxWidth * shrinkX)] may result in content appearing outside the media box. You may query a page's media box using getPageMediaBox(int).
      translateY - the vertical translation value. Values outside the range [0.0, mediaBoxHeight - (mediaBoxHeight * shrinkY)] may result in content appearing outside the media box. You may query a page's media box using getPageMediaBox(int).
    • scalePageContent

      public PdfManipulator scalePageContent(PageRanges pages, float scaleX, float scaleY, int translatePreset)
      Scales the specified page's contents and translates the scaled content to an anchor location, leaving the page dimensions intact.
      Scaling values must be expressed as a fraction.
      Parameters:
      pages - the pages whose content to scale
      scaleX - the horizontal scaling factor, where 100% == 1.0f. Values greater than 1.0f may result in content appearing outside the media box.
      scaleY - the vertical scaling factor, where 100% == 1.0f Values greater than 1.0f may result in content appearing outside the media box.
      translatePreset - anchors the content to a side or corner of the page. See ScalePageContent
    • scalePageContent

      public PdfManipulator scalePageContent(int page, float scaleX, float scaleY, int translatePreset)
      Scales the specified pages' contents and translates the scaled content to an anchor location, leaving the page dimensions intact.
      Scaling values must be expressed as a fraction.
      Parameters:
      page - the page whose content to scale
      scaleX - the horizontal scaling factor, where 100% == 1.0f. Values greater than 1.0f may result in content appearing outside the media box.
      scaleY - the vertical scaling factor, where 100% == 1.0f Values greater than 1.0f may result in content appearing outside the media box.
      translatePreset - anchors the content to a side or corner of the page. See ScalePageContent
    • addText

      public PdfManipulator addText(PageRanges pages, String text, float x, float y, String baseFontName, int fontSize, float r, float g, float b)
      Adds text to the specified page.
      This method only supports strings using WinAnsiEncoding.
      Parameters:
      pages - the pages to draw text on
      text - the text to draw
      x - the horizontal starting position of the text
      y - the vertical starting position of the text
      baseFontName - the name of the base font to use. See BaseFont
      fontSize - the font size of the text
      r - the red color component
      g - the green color component
      b - the blue color component
    • addText

      public PdfManipulator addText(int page, String text, float x, float y, String baseFontName, int fontSize, float r, float g, float b)
      Adds text to the specified page.
      This method only supports strings using WinAnsiEncoding.
      Parameters:
      page - the pages to draw text on
      text - the text to draw
      x - the horizontal starting position of the text
      y - the vertical starting position of the text
      baseFontName - the name of the base font to use. See BaseFont
      fontSize - the font size of the text
      r - the red color component
      g - the green color component
      b - the blue color component
    • addText

      public PdfManipulator addText(PageRanges pages, String text, float x, float y, String baseFontName, int fontSize, float r, float g, float b, float a)
      Adds text to the specified page.
      This method only supports strings using WinAnsiEncoding.
      Parameters:
      pages - the pages to draw text on
      text - the text to draw
      x - the horizontal starting position of the text
      y - the vertical starting position of the text
      baseFontName - the name of the base font to use. See BaseFont
      fontSize - the font size of the text
      r - the red color component
      g - the green color component
      b - the blue color component
      a - the transparency component
    • addText

      public PdfManipulator addText(int page, String text, float x, float y, String baseFontName, int fontSize, float r, float g, float b, float a)
      Adds text to the specified page.
      This method only supports strings using WinAnsiEncoding.
      Parameters:
      page - the pages to draw text on
      text - the text to draw
      x - the horizontal starting position of the text
      y - the vertical starting position of the text
      baseFontName - the name of the base font to use. See BaseFont
      fontSize - the font size of the text
      r - the red color component
      g - the green color component
      b - the blue color component
      a - the transparency component
    • addShape

      public PdfManipulator addShape(PageRanges pages, Shape shape, DrawParameters drawParameters)
      Adds a shape to the specified pages.
      Allows stroking and filling.
      Parameters:
      pages - the pages to draw the shape on
      shape - the shape to draw
      drawParameters - the drawing rules to use. See DrawParameters
    • addShape

      public PdfManipulator addShape(int page, Shape shape, DrawParameters drawParameters)
      Adds a shape to the specified page.
      Allows stroking and filling.
      Parameters:
      page - the page to draw the shape on
      shape - the shape to draw
      drawParameters - the drawing rules to use. See DrawParameters
    • addShape

      public PdfManipulator addShape(PageRanges pages, Shape[] shapes, DrawParameters[] drawParameters)
      Adds shapes to the specified pages.
      Allows stroking and filling.
      Parameters:
      pages - the pages to draw the shape on
      shapes - the shapes to draw
      drawParameters - the drawing rules to use. See DrawParameters
    • addShape

      public PdfManipulator addShape(int page, Shape[] shapes, DrawParameters[] drawParameters)
      Adds shapes to the specified page.
      Allows stroking and filling.
      Parameters:
      page - the page to draw the shapes on
      shapes - the shapes to draw
      drawParameters - the drawing rules to use. See DrawParameters
    • isolatePage

      public PdfManipulator isolatePage(PageRanges pages)
      Removes all pages from the document except the pages in the specified page range.
      Parameters:
      pages - the pages to keep
    • isolatePage

      public PdfManipulator isolatePage(int page)
      Removes all pages from the document except the pages in the specified page range.
      Parameters:
      page - the pages to keep
    • rotatePage

      public PdfManipulator rotatePage(PageRanges pages, int angle)
      Rotates pages by the specified angle (current page rotation + supplied angle).

      See getPageRotation(PageRanges) to get a page's rotation.

      Parameters:
      pages - the pages to rotate
      angle - the number of degrees by which the page shall be rotated clockwise. Must be a multiple of 90
    • rotatePage

      public PdfManipulator rotatePage(int page, int angle)
      Rotates a page by the specified angle (current page rotation + supplied angle).

      See getPageRotation(PageRanges) to get a page's rotation.

      Parameters:
      page - the page to rotate
      angle - the number of degrees by which the page shall be rotated clockwise. Must be a multiple of 90
    • setPageRotation

      public PdfManipulator setPageRotation(PageRanges pages, int rotation)
      Sets the rotation of pages to the specified angle.

      See getPageRotation(PageRanges) to get a page's rotation.

      Parameters:
      pages - the pages to rotate
      rotation - the number of degrees by which the page shall be rotated clockwise. Must be a multiple of 90
    • setPageRotation

      public PdfManipulator setPageRotation(int page, int rotation)
      Sets the rotation of pages to the specified angle.

      See getPageRotation(PageRanges) to get a page's rotation.

      Parameters:
      page - the page to rotate
      rotation - the number of degrees by which the page shall be rotated clockwise. Must be a multiple of 90
    • embedFile

      public PdfManipulator embedFile(File file, String name)
      Embeds a file within the document.
      Parameters:
      file - the file to embed
      name - a name to identify the file
    • attachFile

      public PdfManipulator attachFile(PageRanges pages, File file, String name, float[] rect, float[] color)
      Embeds a file within a document and attaches it to the pages using a FileAttachment annotation.
      Parameters:
      pages - the pages to add the annotation to
      file - the file to embed
      name - a name to identify the file
      rect - the rect for the file attachment annotation
      color - the color for the annotation
    • attachFile

      public PdfManipulator attachFile(int page, File file, String name, float[] rect, float[] color)
      Embeds a file within a document and attaches it to a page using a FileAttachment annotation.
      Parameters:
      page - the page to add the annotation to
      file - the file to embed
      name - a name to identify the file
      rect - the rect for the file attachment annotation
      color - the color for the annotation
    • attachFile

      public PdfManipulator attachFile(PageRanges pages, File file, String name, float[] rect, float[] color, String icon)
      Embeds a file within a document and attaches it to the pages using a FileAttachment annotation.
      Parameters:
      pages - the pages to add the annotation to
      file - the file to embed
      name - a name to identify the file
      rect - the rect for the file attachment annotation
      color - the color for the annotation
      icon - the icon to use for the annotation. See AnnotationIcons
    • attachFile

      public PdfManipulator attachFile(int page, File file, String name, float[] rect, float[] color, String icon)
      Embeds a file within a document and attaches it to a page using a FileAttachment annotation.
      Parameters:
      page - the page to add the annotation to
      file - the file to embed
      name - a name to identify the file
      rect - the rect for the file attachment annotation
      color - the color for the annotation
      icon - the icon to use for the annotation. See AnnotationIcons
    • splitInHalf

      public static boolean splitInHalf(File input, File output, int page) throws IOException
      Split a PDF document in half at the specified page.
      The specified page will appear in the upper half.
      Parameters:
      input - the document to split
      output - the directory for the resulting files
      page - the page to split at
      Returns:
      true if the both renaming operation succeed, false otherwise.
      Throws:
      IOException - if a file handling exception occurs
      Implementation Note:
      This method first writes the PDF documents to temporary .tmp files before renaming them to the destination file.
    • splitInHalf

      public static boolean splitInHalf(File input, File output, int page, byte[] password) throws IOException
      Split a PDF document in half at the specified page.
      The specified page will appear in the upper half.
      Parameters:
      input - the document to split
      output - the directory for the resulting files
      page - the page to split at
      password - the password to open the document with
      Returns:
      true if the both renaming operation succeed, false otherwise.
      Throws:
      IOException - if a file handling exception occurs
      Implementation Note:
      This method first writes the PDF documents to temporary .tmp files before renaming them to the destination file.
    • splitIntoPages

      public static boolean splitIntoPages(File input, File output, int pages) throws IOException
      Split a PDF document into N pages.
      Parameters:
      input - the document to split
      output - the directory for the resulting files
      pages - the number of pages each output file should contain
      Returns:
      true if all the renaming operation succeed, false otherwise.
      Throws:
      IOException - if a file handling exception occurs
      Implementation Note:
      This method first writes the PDF documents to temporary .tmp files before renaming them to the destination file.
    • splitIntoPages

      public static boolean splitIntoPages(File input, File output, int pages, byte[] password) throws IOException
      Split a PDF document into N pages.
      Parameters:
      input - the document to split
      output - the directory for the resulting files
      pages - the number of pages each output file should contain
      password - the password to open the document with
      Returns:
      true if all the renaming operation succeed, false otherwise.
      Throws:
      IOException - if a file handling exception occurs
      Implementation Note:
      This method first writes the PDF documents to temporary .tmp files before renaming them to the destination file.