OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 57 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 25
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.generationDoc;
14
 package org.openconcerto.erp.generationDoc;
15
 
15
 
-
 
16
import org.openconcerto.utils.ExceptionHandler;
-
 
17
 
16
import java.awt.Graphics2D;
18
import java.awt.Graphics2D;
17
import java.io.File;
19
import java.io.File;
18
import java.io.FileOutputStream;
20
import java.io.FileOutputStream;
19
import java.io.FilenameFilter;
21
import java.io.FilenameFilter;
20
import java.io.IOException;
22
import java.io.IOException;
Line 34... Line 36...
34
import com.lowagie.text.pdf.PdfContentByte;
36
import com.lowagie.text.pdf.PdfContentByte;
35
import com.lowagie.text.pdf.PdfWriter;
37
import com.lowagie.text.pdf.PdfWriter;
36
 
38
 
37
public class SheetUtils {
39
public class SheetUtils {
38
 
40
 
39
    static SheetUtils instance;
-
 
40
 
-
 
41
    public static SheetUtils getInstance() {
41
    public static File convertToOldFile(String fileName, File pathDest, File fDest) {
42
        if (instance == null) {
-
 
43
            instance = new SheetUtils();
-
 
44
        }
-
 
45
        return instance;
42
        // FIXME: !!!!!!!!
46
    }
-
 
47
 
-
 
48
    public File convertToOldFile(String fileName, File pathDest, File fDest) {
-
 
49
        return convertToOldFile(fileName, pathDest, fDest, ".ods");
43
        return convertToOldFile(fileName, pathDest, fDest, ".ods");
50
    }
44
    }
51
 
45
 
52
    /**
46
    /**
53
     * Déplace le fichier, si il existe, dans le répertoire.
47
     * Déplace le fichier, si il existe, dans le répertoire.
Line 55... Line 49...
55
     * @param fileName nom du fichier sans extension
49
     * @param fileName nom du fichier sans extension
56
     * @param pathDest
50
     * @param pathDest
57
     * @param fDest
51
     * @param fDest
58
     * @return
52
     * @return
59
     */
53
     */
60
    public File convertToOldFile(String fileName, File pathDest, File fDest, String extension) {
54
    public static File convertToOldFile(String fileName, File pathDest, File fDest, String extension) {
61
        if (fDest.exists()) {
55
        if (fDest.exists()) {
62
            int i = 0;
56
            int i = 0;
63
            String destName = fileName;
57
            String destName = fileName;
64
            File pathOld = new File(pathDest, "Historique");
58
            File pathOld = new File(pathDest, "Historique");
65
            pathOld.mkdirs();
59
            pathOld.mkdirs();
Line 90... Line 84...
90
            fDest = new File(pathDest, fileName + extension);
84
            fDest = new File(pathDest, fileName + extension);
91
        }
85
        }
92
        return fDest;
86
        return fDest;
93
    }
87
    }
94
 
88
 
95
    public List<File> getHistorique(final String fileName, File pathDest) {
89
    public static List<File> getHistorique(final String fileName, File pathDest) {
96
        File pathOld = new File(pathDest, "Historique");
90
        File pathOld = new File(pathDest, "Historique");
97
        File[] files = pathOld.listFiles(new FilenameFilter() {
91
        File[] files = pathOld.listFiles(new FilenameFilter() {
98
 
92
 
99
            @Override
93
            @Override
100
            public boolean accept(File dir, String name) {
94
            public boolean accept(File dir, String name) {
Line 116... Line 110...
116
            }
110
            }
117
        });
111
        });
118
        return result;
112
        return result;
119
    }
113
    }
120
 
114
 
121
    public static void main(String[] args) {
-
 
122
        final OpenDocument doc = new OpenDocument(new File("E:/Facture_FACT1108-5785.ods"));
115
    public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
123
        try {
-
 
124
            SheetUtils.getInstance().convert2PDF(doc, new File("E:/test"), "test");
-
 
125
        } catch (Exception exn) {
-
 
126
            // TODO Bloc catch auto-généré
-
 
127
            exn.printStackTrace();
-
 
128
        }
-
 
129
    }
-
 
130
 
116
 
131
    public void convert2PDF(final OpenDocument doc, final File f, final String fileName) throws Exception {
-
 
132
 
-
 
133
        SwingUtilities.invokeLater(new Runnable() {
117
        SwingUtilities.invokeAndWait(new Runnable() {
134
            @Override
118
            @Override
135
            public void run() {
119
            public void run() {
136
                // TODO Raccord de méthode auto-généré
-
 
137
 
-
 
138
                // Open the PDF document
120
                // Open the PDF document
139
                Document document = new Document(PageSize.A4, 50, 50, 50, 50);
121
                Document document = new Document(PageSize.A4, 50, 50, 50, 50);
140
                File outFile = new File(f.getParent(), fileName + ".pdf");
-
 
141
                FileOutputStream fileOutputStream;
-
 
142
 
122
 
143
                try {
123
                try {
144
 
124
 
145
                    fileOutputStream = new FileOutputStream(outFile);
125
                    FileOutputStream fileOutputStream = new FileOutputStream(pdfFileToCreate);
146
 
126
 
147
                    // Create the writer
127
                    // Create the writer
148
                    PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
128
                    PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
149
                    writer.setPdfVersion(PdfWriter.VERSION_1_6);
129
                    writer.setPdfVersion(PdfWriter.VERSION_1_6);
150
                    writer.setFullCompression();
130
                    writer.setFullCompression();
151
                    // writer.setPageEmpty(true);
-
 
152
 
131
 
153
                    document.open();
132
                    document.open();
154
                    // System.out.println(writer.getPageNumber());
-
 
155
                    // // Create a template and a Graphics2D object
-
 
156
                    // com.itextpdf.text.Rectangle pageSize = document.getPageSize();
-
 
157
                    // int w = (int) (pageSize.getWidth() * 0.9);
-
 
158
                    // int h = (int) (pageSize.getHeight() * 0.95);
-
 
159
 
133
 
160
                    PdfContentByte cb = writer.getDirectContent();
134
                    PdfContentByte cb = writer.getDirectContent();
161
 
135
 
162
                    // Configure the renderer
136
                    // Configure the renderer
163
                    ODTRenderer renderer = new ODTRenderer(doc);
137
                    ODTRenderer renderer = new ODTRenderer(doc);
Line 188... Line 162...
188
                    // Close the PDF document
162
                    // Close the PDF document
189
                    document.close();
163
                    document.close();
190
                    // writer.close();
164
                    // writer.close();
191
                    fileOutputStream.close();
165
                    fileOutputStream.close();
192
 
166
 
193
                } catch (Exception exn) {
167
                } catch (Exception originalExn) {
194
                    // TODO Bloc catch auto-généré
168
                    ExceptionHandler.handle("Impossible de créer le PDF " + pdfFileToCreate.getAbsolutePath(), originalExn);
195
                    exn.printStackTrace();
-
 
196
                }
169
                }
197
            }
170
            }
198
        });
171
        });
199
    }
172
    }
-
 
173
 
-
 
174
    /**
-
 
175
     * Get a new file with an other extension
-
 
176
     * 
-
 
177
     * @param the file (ex: Test.ods)
-
 
178
     * @param the extension (ex: pdf)
-
 
179
     * */
-
 
180
    static File getFileWithExtension(File file, String extension) {
-
 
181
        if (!extension.startsWith(".")) {
-
 
182
            extension = "." + extension;
-
 
183
        }
-
 
184
        String name = file.getName();
-
 
185
        int i = name.lastIndexOf(".");
-
 
186
        name = name.substring(0, i) + extension;
-
 
187
        final File f = new File(file.getParent(), name);
-
 
188
        return f;
-
 
189
    }
200
}
190
}