r337 - in testgenerationpdf/trunk: . src src/main src/main/java src/main/java/org src/main/java/org/codelutin src/main/java/org/codelutin/itext src/main/java/org/codelutin/pdfbox src/main/java/org/codelutin/yahp src/main/resources
Author: echatellier Date: 2009-11-26 18:16:44 +0100 (Thu, 26 Nov 2009) New Revision: 337 Added: testgenerationpdf/trunk/pom.xml testgenerationpdf/trunk/src/ testgenerationpdf/trunk/src/main/ testgenerationpdf/trunk/src/main/java/ testgenerationpdf/trunk/src/main/java/org/ testgenerationpdf/trunk/src/main/java/org/codelutin/ testgenerationpdf/trunk/src/main/java/org/codelutin/itext/ testgenerationpdf/trunk/src/main/java/org/codelutin/itext/iTextTest.java testgenerationpdf/trunk/src/main/java/org/codelutin/pdfbox/ testgenerationpdf/trunk/src/main/java/org/codelutin/pdfbox/TestPDFBox.java testgenerationpdf/trunk/src/main/java/org/codelutin/yahp/ testgenerationpdf/trunk/src/main/java/org/codelutin/yahp/YahpTest.java testgenerationpdf/trunk/src/main/resources/ testgenerationpdf/trunk/src/main/resources/index.html Modified: testgenerationpdf/trunk/ Log: Ajout des tests de generation de pdf (itext, pdfbox, yahp) Property changes on: testgenerationpdf/trunk ___________________________________________________________________ Added: svn:ignore + .settings .classpath target .project Added: testgenerationpdf/trunk/pom.xml =================================================================== --- testgenerationpdf/trunk/pom.xml (rev 0) +++ testgenerationpdf/trunk/pom.xml 2009-11-26 17:16:44 UTC (rev 337) @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.codelutin</groupId> + <artifactId>testpdfbox</artifactId> + <version>1.0.0</version> + <packaging>jar</packaging> + + <name>Test pdf box</name> + <description>Test pdf box.</description> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputDirectory>UTF-8</project.reporting.outputDirectory> + <maven.compiler.source>1.6</maven.compiler.source> + <maven.compiler.target>1.6</maven.compiler.target> + </properties> + + <dependencies> + <dependency> + <groupId>pdfbox</groupId> + <artifactId>pdfbox</artifactId> + <version>0.7.3</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.6</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + <version>1.1.1</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>com.lowagie</groupId> + <artifactId>itext</artifactId> + <version>2.1.7</version> + </dependency> + </dependencies> +</project> Added: testgenerationpdf/trunk/src/main/java/org/codelutin/itext/iTextTest.java =================================================================== --- testgenerationpdf/trunk/src/main/java/org/codelutin/itext/iTextTest.java (rev 0) +++ testgenerationpdf/trunk/src/main/java/org/codelutin/itext/iTextTest.java 2009-11-26 17:16:44 UTC (rev 337) @@ -0,0 +1,122 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.itext; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.PageSize; +import com.lowagie.text.html.HtmlParser; +import com.lowagie.text.pdf.AcroFields; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfStamper; +import com.lowagie.text.pdf.PdfWriter; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class iTextTest { + + /** + * This will open a PDF and replace a string if it finds it. + * <br /> + * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) { + iTextTest app = new iTextTest(); + try { + //app.doIt( "/home/chatellier/tmp/test.pdf", "/home/chatellier/tmp/test2.pdf", "username", "eric"); + //app.doIt("/tmp/RECAP-ADHESION..pdf", + // "/home/chatellier/tmp/test3.pdf", "NOM", "eric"); + app.doItHtml(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void doIt(String inputFile, String outputFile, String strToFind, + String message) throws IOException, DocumentException { + // Read PDF template + PdfReader pdfRd = new PdfReader(inputFile); + PdfStamper stamp = new PdfStamper(pdfRd, new FileOutputStream( + outputFile)); + + // Set form Text Fields + // Get Text Fields from PDF file + AcroFields fields = stamp.getAcroFields(); + + System.out.println(fields.getFields()); + // Get template + String formText = fields.getField("formText"); + + // Replace template with data + formText = formText.replace("[nom]", "eee"); + formText = formText.replace("[prenom]", "bbb"); + formText = formText.replace("[date]", "ddd"); + + // Set back the Text Field + fields.setField("formText", formText); + + // Close stamp + stamp.close(); + } + + public void doItHtml() throws FileNotFoundException, DocumentException { + Document doc = new Document(PageSize.A4, 50, 50, 108, 80);// cuatro + + OutputStream SalidaPdf = new FileOutputStream("/tmp/test.pdf"); + + PdfWriter writer = PdfWriter.getInstance(doc, SalidaPdf); + //writer.setPageEvent(this); + + doc.open(); + doc.resetPageCount(); + + try { + + HtmlParser.parse(doc, new FileInputStream("/home/chatellier/tmp/lm/opti2/index.html")); + // doc is the file to create and FileInputStream is the file + + // with the HTML code + + } catch (Exception e) { + + e.printStackTrace(); + + System.out.println("error parse a pdf" + e.toString()); + + } + + doc.close(); + } + +} Property changes on: testgenerationpdf/trunk/src/main/java/org/codelutin/itext/iTextTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: testgenerationpdf/trunk/src/main/java/org/codelutin/pdfbox/TestPDFBox.java =================================================================== --- testgenerationpdf/trunk/src/main/java/org/codelutin/pdfbox/TestPDFBox.java (rev 0) +++ testgenerationpdf/trunk/src/main/java/org/codelutin/pdfbox/TestPDFBox.java 2009-11-26 17:16:44 UTC (rev 337) @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.codelutin.pdfbox; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; + +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSString; +import org.pdfbox.exceptions.COSVisitorException; +import org.pdfbox.exceptions.CryptographyException; +import org.pdfbox.exceptions.InvalidPasswordException; +import org.pdfbox.pdfparser.PDFStreamParser; +import org.pdfbox.pdfwriter.ContentStreamWriter; +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDPage; +import org.pdfbox.pdmodel.common.PDStream; +import org.pdfbox.util.PDFOperator; + + +/** + * This is an example that will replace a string in a PDF with a new one. + * + * The example is taken from the pdf file format specification. + * + * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> + * @version $Revision$ + */ +public class TestPDFBox +{ + /** + * Constructor. + */ + public TestPDFBox() + { + super(); + } + + /** + * Locate a string in a PDF and replace it with a new string. + * + * @param inputFile The PDF to open. + * @param outputFile The PDF to write to. + * @param strToFind The string to find in the PDF document. + * @param message The message to write in the file. + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error writing the PDF. + */ + public void doIt( String inputFile, String outputFile, String strToFind, String message) + throws IOException, COSVisitorException + { + // the document + PDDocument doc = null; + try + { + doc = PDDocument.load( inputFile); + if( doc.isEncrypted() ) + { + try + { + System.out.println("doc decrypt"); + doc.decrypt( "" ); + } + catch( InvalidPasswordException e ) + { + System.err.println( "Error: Document is encrypted with a password." ); + System.exit( 1 ); + } catch (CryptographyException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + List pages = doc.getDocumentCatalog().getAllPages(); + for( int i=0; i<pages.size(); i++ ) + { + System.out.println("page" + i); + PDPage page = (PDPage)pages.get( i ); + PDStream contents = page.getContents(); + PDFStreamParser parser = new PDFStreamParser(contents.getStream() ); + parser.parse(); + List tokens = parser.getTokens(); + for( int j=0; j<tokens.size(); j++ ) + { + + Object next = tokens.get( j ); + if( next instanceof PDFOperator ) + { + PDFOperator op = (PDFOperator)next; + System.out.println("PDFOperator = " + op.getOperation()); + //Tj and TJ are the two operators that display + //strings in a PDF + if( op.getOperation().equals( "Tj" ) ) + { + //Tj takes one operator and that is the string + //to display so lets update that operator + COSString previous = (COSString)tokens.get( j-1 ); + String string = previous.getString(); + string = string.replaceFirst( strToFind, message ); + previous.reset(); + previous.append( string.getBytes() ); + } + else if( op.getOperation().equals( "TJ" ) ) + { + COSArray previous = (COSArray)tokens.get( j-1 ); + //System.out.println("previous = " + previous.toString()); + + StringBuffer buffer = new StringBuffer(); + for( int k=0; k<previous.size(); k++ ) + { + Object arrElement = previous.getObject( k ); + if( arrElement instanceof COSString ) + { + COSString cosString = (COSString)arrElement; + String string = cosString.getString(); + //string = string.replaceFirst( strToFind, message ); + //cosString.reset(); + //cosString.append( string.getBytes()); + buffer.append(string); + //System.out.println("after = " + string + "\n"); + } + } + + String s = buffer.toString(); + s = s.replaceFirst( strToFind, message ); + System.out.println("Buffer = " + s); + + // replace : + COSArray a = new COSArray(); + a.add(new COSString(s)); + tokens.set(j- 1, a); + } + } + } + //now that the tokens are updated we will replace the + //page content stream. + PDStream updatedStream = new PDStream(doc); + OutputStream out = updatedStream.createOutputStream(); + ContentStreamWriter tokenWriter = new ContentStreamWriter(out); + tokenWriter.writeTokens( tokens ); + page.setContents( updatedStream ); + } + doc.save( outputFile ); + } + finally + { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will open a PDF and replace a string if it finds it. + * <br /> + * see usage() for commandline + * + * @param args Command line arguments. + */ + public static void main(String[] args) + { + TestPDFBox app = new TestPDFBox(); + try + { + //app.doIt( "/home/chatellier/tmp/test.pdf", "/home/chatellier/tmp/test2.pdf", "username", "eric"); + app.doIt( "/home/chatellier/projets/leroymerlin/docs/preadhesion/Doc-Preadhesion/Mail-Confirmation.pdf", "/home/chatellier/tmp/test3.pdf", "NOM", "eric"); + } + catch (Exception e) + { + e.printStackTrace(); + } + } +} + Property changes on: testgenerationpdf/trunk/src/main/java/org/codelutin/pdfbox/TestPDFBox.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: testgenerationpdf/trunk/src/main/java/org/codelutin/yahp/YahpTest.java =================================================================== --- testgenerationpdf/trunk/src/main/java/org/codelutin/yahp/YahpTest.java (rev 0) +++ testgenerationpdf/trunk/src/main/java/org/codelutin/yahp/YahpTest.java 2009-11-26 17:16:44 UTC (rev 337) @@ -0,0 +1,90 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.yahp; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.allcolor.yahp.converter.CYaHPConverter; +import org.allcolor.yahp.converter.IHtmlToPdfTransformer; +import org.allcolor.yahp.converter.IHtmlToPdfTransformer.CConvertException; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class YahpTest { + + /** + * @param args + * @throws Exception + */ + public static void main(String[] args) throws Exception { + // TODO Auto-generated method stub + new YahpTest().doIt(); + } + + /** + * @throws CConvertException + * @throws IOException + * + */ + private void doIt() throws CConvertException, IOException { + + // new converter + CYaHPConverter converter = new CYaHPConverter(); + // save pdf in outfile + File fout = new File("/tmp/test2.pdf"); + FileOutputStream out = new FileOutputStream(fout); + // contains configuration properties + Map properties = new HashMap(); + // list containing header/footer + List headerFooterList = new ArrayList(); + // add header/footer + //headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter( + // "<table width=\"100%\"><tbody><tr><td align=\"left\">"+ + // "Generated with YaHPConverter.</td><td align=\"right\">Page <pagenumber>/<"+ + // "pagecount></td></tr></tbody></table>", + // IHtmlToPdfTransformer.CHeaderFooter.HEADER)); + //headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter( + // "© 2009 Quentin Anciaux", + // IHtmlToPdfTransformer.CHeaderFooter.FOOTER)); + properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, + IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER); + //properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, fontPath); + converter.convertToPdf(new File("src/main/resources/index.html").toURI().toURL(), + IHtmlToPdfTransformer.A4P, headerFooterList, out, + properties); + out.flush(); + out.close(); + + + } + +} Property changes on: testgenerationpdf/trunk/src/main/java/org/codelutin/yahp/YahpTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: testgenerationpdf/trunk/src/main/resources/index.html =================================================================== --- testgenerationpdf/trunk/src/main/resources/index.html (rev 0) +++ testgenerationpdf/trunk/src/main/resources/index.html 2009-11-26 17:16:44 UTC (rev 337) @@ -0,0 +1,16 @@ +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +<title>Test pdf yahp</title> +<style> +img { + display: block; +} +</style> +</head> + +<body> +C'était un bel été<br /> +Il pleuvait ! +</body> +</html>
participants (1)
-
echatellier@users.nuiton.org