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
    • 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
    • getPageMediaBox

      public List<float[]> getPageMediaBox(PageRanges pages) throws IOException
      Returns the media box of the specified page.
      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
    • 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
    • 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
    • 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.
    • 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.