You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2016/12/27 11:59:06 UTC
svn commit: r1776107 -
/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java
Author: lehmi
Date: Tue Dec 27 11:59:06 2016
New Revision: 1776107
URL: http://svn.apache.org/viewvc?rev=1776107&view=rev
Log:
PDFBOX-1662: remove text from FormXObjects as well
Modified:
pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java
Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java?rev=1776107&r1=1776106&r2=1776107&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java Tue Dec 27 11:59:06 2016
@@ -22,7 +22,10 @@ import org.apache.pdfbox.pdfparser.PDFSt
import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDStream;
+import org.apache.pdfbox.pdmodel.graphics.PDXObject;
+import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.contentstream.operator.Operator;
import java.io.File;
@@ -42,7 +45,7 @@ public final class RemoveAllText
*/
private RemoveAllText()
{
- //example class should not be instantiated
+ // example class should not be instantiated
}
/**
@@ -52,9 +55,9 @@ public final class RemoveAllText
*
* @throws IOException If there is an error parsing the document.
*/
- public static void main( String[] args ) throws IOException
+ public static void main(String[] args) throws IOException
{
- if( args.length != 2 )
+ if (args.length != 2)
{
usage();
}
@@ -63,44 +66,22 @@ public final class RemoveAllText
PDDocument document = null;
try
{
- document = PDDocument.load( new File(args[0]) );
- if( document.isEncrypted() )
+ document = PDDocument.load(new File(args[0]));
+ if (document.isEncrypted())
{
- System.err.println( "Error: Encrypted documents are not supported for this example." );
- System.exit( 1 );
+ System.err.println(
+ "Error: Encrypted documents are not supported for this example.");
+ System.exit(1);
}
- for( PDPage page : document.getPages() )
+ for (PDPage page : document.getPages())
{
- PDFStreamParser parser = new PDFStreamParser(page);
- parser.parse();
- List<Object> tokens = parser.getTokens();
- List<Object> newTokens = new ArrayList<Object>();
- for (Object token : tokens)
- {
- if( token instanceof Operator)
- {
- Operator op = (Operator)token;
- if( op.getName().equals( "TJ") || op.getName().equals( "Tj" ))
- {
- //remove the one argument to this operator
- newTokens.remove( newTokens.size() -1 );
- continue;
- }
- }
- newTokens.add( token );
- }
- PDStream newContents = new PDStream( document );
- OutputStream out = newContents.createOutputStream(COSName.FLATE_DECODE);
- ContentStreamWriter writer = new ContentStreamWriter( out );
- writer.writeTokens( newTokens );
- out.close();
- page.setContents( newContents );
+ removeAllTextTokens(page, document);
}
- document.save( args[1] );
+ document.save(args[1]);
}
finally
{
- if( document != null )
+ if (document != null)
{
document.close();
}
@@ -108,12 +89,84 @@ public final class RemoveAllText
}
}
+ private static void processResources(PDResources resources) throws IOException
+ {
+ Iterable<COSName> names = resources.getXObjectNames();
+ for (COSName name : names)
+ {
+ PDXObject xobject = resources.getXObject(name);
+ if (xobject instanceof PDFormXObject)
+ {
+ removeAllTextTokens((PDFormXObject) xobject);
+ }
+ }
+ }
+
+ private static void removeAllTextTokens(PDPage page, PDDocument document) throws IOException
+ {
+ PDFStreamParser parser = new PDFStreamParser(page);
+ parser.parse();
+ List<Object> tokens = parser.getTokens();
+ List<Object> newTokens = new ArrayList<Object>();
+ for (Object token : tokens)
+ {
+ if (token instanceof Operator)
+ {
+ String opname = ((Operator) token).getName();
+ if ("TJ".equals(opname) || "Tj".equals(opname))
+ {
+ // remove the one argument to this operator
+ newTokens.remove(newTokens.size() - 1);
+ continue;
+ }
+ }
+ newTokens.add(token);
+ }
+ PDStream newContents = new PDStream(document);
+ OutputStream out = newContents.createOutputStream(COSName.FLATE_DECODE);
+ ContentStreamWriter writer = new ContentStreamWriter(out);
+ writer.writeTokens(newTokens);
+ out.close();
+ page.setContents(newContents);
+ processResources(page.getResources());
+ }
+
+ private static void removeAllTextTokens(PDFormXObject xobject) throws IOException
+ {
+ PDStream stream = xobject.getContentStream();
+ PDFStreamParser parser = new PDFStreamParser(xobject);
+ parser.parse();
+ List<Object> tokens = parser.getTokens();
+ List<Object> newTokens = new ArrayList<Object>();
+ for (Object token : tokens)
+ {
+ if (token instanceof Operator)
+ {
+ Operator op = (Operator) token;
+ if (op.getName().equals("TJ") || op.getName().equals("Tj")
+ || op.getName().equals("'") || op.getName().equals("\""))
+ {
+ // remove the one argument to this operator
+ newTokens.remove(newTokens.size() - 1);
+ continue;
+ }
+ }
+ newTokens.add(token);
+ }
+ OutputStream out = stream.createOutputStream(COSName.FLATE_DECODE);
+ ContentStreamWriter writer = new ContentStreamWriter(out);
+ writer.writeTokens(newTokens);
+ out.close();
+ processResources(xobject.getResources());
+ }
+
/**
* This will print the usage for this document.
*/
private static void usage()
{
- System.err.println( "Usage: java " + RemoveAllText.class.getName() + " <input-pdf> <output-pdf>" );
+ System.err.println(
+ "Usage: java " + RemoveAllText.class.getName() + " <input-pdf> <output-pdf>");
}
}