📄 convertcolorspace.java
字号:
editedPageTokens.add( new COSFloat( cmyk[0] ) );
editedPageTokens.add( new COSFloat( cmyk[1] ) );
editedPageTokens.add( new COSFloat( cmyk[2] ) );
editedPageTokens.add( new COSFloat( cmyk[3] ) );
}
private static float[] convertRGBToCMYK( float red, float green, float blue )
{
//
// RGB->CMYK from From
// http://en.wikipedia.org/wiki/Talk:CMYK_color_model
//
float c = 1.0f - red;
float m = 1.0f - green;
float y = 1.0f - blue;
float k = 1.0f;
k = Math.min( Math.min( Math.min( c,k ), m), y );
c = ( c - k ) / ( 1 - k );
m = ( m - k ) / ( 1 - k );
y = ( y - k ) / ( 1 - k );
return new float[] { c,m,y,k};
}
private static int[] stringToIntArray( String string )
{
String[] ints = string.split( "," );
int[] retval = new int[ints.length];
for( int i=0; i<ints.length; i++ )
{
retval[i] = Integer.parseInt( ints[i] );
}
return retval;
}
/**
* Infamous main method.
*
* @param args Command line arguments, should be one and a reference to a file.
*
* @throws Exception If there is an error parsing the document.
*/
public static void main( String[] args ) throws Exception
{
String password = "";
String inputFile = null;
String outputFile = null;
String destColorspace = "CMYK";
Pattern colorEquivalentPattern = Pattern.compile(
"^(.*):\\((.*)\\)" +
"=(.*):\\((.*)\\)$");
Matcher colorEquivalentMatcher = null;
//key= value=java.awt.Color
Hashtable colorEquivalents = new Hashtable();
for( int i=0; i<args.length; i++ )
{
if( args[i].equals( PASSWORD ) )
{
i++;
if( i >= args.length )
{
usage();
}
password = args[i];
}
if( args[i].equals( DEST_COLORSPACE ) )
{
i++;
if( i >= args.length )
{
usage();
}
destColorspace = args[i];
}
if(args[i].equals( CONVERSION ) )
{
i++;
if( i >= args.length )
{
usage();
}
colorEquivalentMatcher = colorEquivalentPattern.matcher(args[i]);
if(!colorEquivalentMatcher.matches())
{
usage();
}
String srcColorSpace = colorEquivalentMatcher.group(1);
String srcColorvalues = colorEquivalentMatcher.group(2);
String destColorSpace = colorEquivalentMatcher.group(3);
String destColorvalues = colorEquivalentMatcher.group(4);
ConvertColorspace.ColorSpaceInstance source = new ColorSpaceInstance();
source.colorspace = srcColorSpace;
source.colorspaceValues = stringToIntArray( srcColorvalues );
ColorSpaceInstance dest = new ColorSpaceInstance();
dest.colorspace = destColorSpace;
dest.colorspaceValues = stringToIntArray( destColorvalues );
colorEquivalents.put(source, dest);
}
else
{
if( inputFile == null )
{
inputFile = args[i];
}
else
{
outputFile = args[i];
}
}
}
if( inputFile == null )
{
usage();
}
if( outputFile == null || outputFile.equals(inputFile))
{
usage();
}
PDDocument doc = null;
try
{
doc = PDDocument.load( inputFile );
if( doc.isEncrypted() )
{
try
{
doc.decrypt( password );
}
catch( InvalidPasswordException e )
{
if( !password.equals( "" ) )//they supplied the wrong password
{
System.err.println( "Error: The supplied password is incorrect." );
System.exit( 2 );
}
else
{
//they didn't suppply a password and the default of "" was wrong.
System.err.println( "Error: The document is encrypted." );
usage();
}
}
}
ConvertColorspace converter = new ConvertColorspace();
converter.replaceColors(doc, colorEquivalents, destColorspace );
doc.save( outputFile );
}
finally
{
if( doc != null )
{
doc.close();
}
}
}
/**
* This will print the usage requirements and exit.
*/
private static void usage()
{
System.err.println( "Usage: java org.pdfbox.ConvertColorspace [OPTIONS] <PDF Input file> "
+"<PDF Output File>\n" +
" -password <password> Password to decrypt document\n" +
" -equiv <color equivalent> Color equivalent to use for conversion.\n" +
" -destColorspace <color equivalent> The destination colorspace, CMYK is the only '" +
"supported colorspace." +
" \n" +
" The equiv format is : <source colorspace>:(colorspace value)=<dest colorspace>:(colorspace value)" +
" This option can be used as many times as necessary\n" +
" The supported equiv colorspaces are RGB and CMYK.\n" +
" RGB color values are integers between 0 and 255" +
" CMYK color values are integer between 0 and 100.\n" +
" Example: java org.pdfbox.ConvertColorspace -equiv RGB:(255,0,0)=CMYK(0,99,100,0) input.pdf output.pdf\n" +
" <PDF Input file> The PDF document to use\n" +
" <PDF Output file> The PDF file to write the result to. Must be different of input file\n"
);
System.exit( 1 );
}
/**
*
*
*/
private static class ColorSpaceInstance
{
private String colorspace = null;
private int[] colorspaceValues = null;
/**
* {@inheritDoc}
*/
public int hashCode()
{
int code = colorspace.hashCode();
for( int i=0; i<colorspaceValues.length; i++ )
{
code += colorspaceValues[i];
}
return code;
}
/**
* {@inheritDoc}
*/
public boolean equals( Object o )
{
boolean retval = false;
if( o instanceof ColorSpaceInstance )
{
ColorSpaceInstance other = (ColorSpaceInstance)o;
if( this.colorspace.equals( other.colorspace ) &&
colorspaceValues.length == other.colorspaceValues.length )
{
retval = true;
for( int i=0; i<colorspaceValues.length && retval; i++ )
{
retval = retval && colorspaceValues[i] == other.colorspaceValues[i];
}
}
}
return retval;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -