Package org.jpedal.manipulator
Class PdfManipulator
java.lang.Object
org.jpedal.manipulator.PdfManipulator
PDF Manipulator allows for editing PDF documents.
This class supports in memory editing and method chaining which reduces the number of writes to disk.
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddPage
(int page, float[] mediaBox) Inserts a new blank page, before the specified page.addPage
(PageRanges pages, float[] mediaBox) Inserts a new blank page, before the specified page.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.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.apply()
Applies all the added manipulations, in the order that they were addedCloses and frees the currently loaded document.int
Returns the number of pages in the document.float[]
getPageCropBox
(int page) Returns the crop box of the specified page.List
<float[]> getPageCropBox
(PageRanges pages) Returns the crop box of the specified page.float[]
getPageMediaBox
(int page) Returns the media box of the specified page.List
<float[]> getPageMediaBox
(PageRanges pages) Returns the media box of the specified page.isolatePage
(int page) Removes all pages from the document except the pages in the specified page range.isolatePage
(PageRanges pages) Removes all pages from the document except the pages in the specified page range.loadDocument
(byte[] input) Load a new PDF document into memory from abyte[]
.loadDocument
(File input) Load a new PDF document into memory from aFile
.static void
Perform a single manipulation operation on a PDF filenewDocument
(float[] mediaBox) Create a new PDF document with a single blank page.removePage
(int page) Deletes the specified pages.removePage
(PageRanges pages) Deletes the specified pages.reset()
Clears the list of added manipulationsscalePage
(int page, float scaleX, float scaleY) Scales the specified page.scalePage
(PageRanges pages, float scaleX, float scaleY) Scales the specified page.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.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.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.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.static boolean
splitInHalf
(File input, File output, int page) Split a PDF document in half at the specified page.static boolean
splitIntoPages
(File input, File output, int pages) Split a PDF document into N pages.byte[]
Writes the currently loaded PDF document, with any modifications applied, to abyte[]
.boolean
writeDocument
(File output) Writes the currently loaded PDF document, with any modifications applied, to aFile
.
-
Constructor Details
-
PdfManipulator
public PdfManipulator()Instantiate a new PDF Manipulator
-
-
Method Details
-
main
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
Load a new PDF document into memory from aFile
.
Replaces the currently loaded document, you should callcloseDocument()
before calling this method.- Parameters:
input
- the document to load- Throws:
IOException
- if a file handling exception occurs
-
loadDocument
Load a new PDF document into memory from abyte[]
.
Replaces the currently loaded document, you should callcloseDocument()
before calling this method.- Parameters:
input
- the document to load- Throws:
IOException
- if a document loading exception occurs
-
newDocument
Create a new PDF document with a single blank page.
Replaces the currently loaded document, you should callcloseDocument()
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
Writes the currently loaded PDF document, with any modifications applied, to aFile
.
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
Writes the currently loaded PDF document, with any modifications applied, to abyte[]
.- Returns:
- the resultant document
- Throws:
IOException
- if a document writing exception occurs
-
closeDocument
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
Applies all the added manipulations, in the order that they were added- Throws:
IOException
- if the document cannot be processed
-
reset
Clears the list of added manipulations -
addPage
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]. SeePaperSize
for common paper sizes.
-
addPage
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]. SeePaperSize
for common paper sizes.
-
removePage
Deletes the specified pages.- Parameters:
pages
- the pages to delete in the new file
-
removePage
Deletes the specified pages.- Parameters:
page
- the pages to delete in the new file
-
getPageMediaBox
Returns the media box of the specified page.- Parameters:
pages
- the pages to query- Returns:
- the media box as a
List
offloat[]
- Throws:
IOException
- if a file handling exception occurs
-
getPageMediaBox
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
Returns the crop box of the specified page.- Parameters:
pages
- the pages to query- Returns:
- the crop box as a
List
offloat[]
- Throws:
IOException
- if a file handling exception occurs
-
getPageCropBox
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
Returns the number of pages in the document.- Returns:
- the number of pages
- Throws:
IOException
- if a file handling exception occurs
-
scalePage
Scales the specified page.- Parameters:
pages
- the pages to scalescaleX
- the horizontal scaling factor. 100% is 1.0fscaleY
- the vertical scaling factor. 100% is 1.0f
-
scalePage
Scales the specified page.- Parameters:
page
- the page to scalescaleX
- the horizontal scaling factor. 100% is 1.0fscaleY
- 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 scalescaleX
- 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 usinggetPageMediaBox(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 usinggetPageMediaBox(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 scaledscaleX
- 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 usinggetPageMediaBox(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 usinggetPageMediaBox(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 scalescaleX
- 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. SeeScalePageContent
-
scalePageContent
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 scalescaleX
- 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. SeeScalePageContent
-
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 ontext
- the text to drawx
- the horizontal starting position of the texty
- the vertical starting position of the textbaseFontName
- the name of the base font to use. SeeBaseFont
fontSize
- the font size of the textr
- the red color componentg
- the green color componentb
- 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 ontext
- the text to drawx
- the horizontal starting position of the texty
- the vertical starting position of the textbaseFontName
- the name of the base font to use. SeeBaseFont
fontSize
- the font size of the textr
- the red color componentg
- the green color componentb
- the blue color component
-
isolatePage
Removes all pages from the document except the pages in the specified page range.- Parameters:
pages
- the pages to keep
-
isolatePage
Removes all pages from the document except the pages in the specified page range.- Parameters:
page
- the pages to keep
-
splitInHalf
Split a PDF document in half at the specified page.
The specified page will appear in the upper half.- Parameters:
input
- the document to splitoutput
- the directory for the resulting filespage
- 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
Split a PDF document into N pages.- Parameters:
input
- the document to splitoutput
- the directory for the resulting filespages
- 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.
-