IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Fusion document PDF


Sujet :

Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2014
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2014
    Messages : 26
    Points : 27
    Points
    27
    Par défaut Fusion document PDF
    Bonjour,
    Je suis entrain d'essayer de fusionner les documents existants sous différents repertoire mais le console m'affiche
    java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(Unknown Source)
    at com.itextpdf.text.io.StreamUtil.inputStreamToArray(StreamUtil.java:71)
    at com.itextpdf.text.io.RandomAccessSourceFactory.createSource(RandomAccessSourceFactory.java:146)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:350)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:370)
    at test.Ms.concatPDFs(Ms.java:72)
    at test.Ms.main(Ms.java:42)
    at test.List.main(List.java:22)
    fin traitement
    ce message est affiché beaucoup de fois a cause du boucle existante dans la classe List
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
     
    import java.io.File;
    import java.io.IOException;
     
     
    public class List {
    	public static void main(String[] args) throws IOException {
    		String path = "D:\\AttijariGenerator\\fichiers temporaires\\dos";
    		String[] ListeD = listerCont(path);
     
    		System.out.println(ListeD[1]);
    		for (int i =0;i<ListeD.length;i++){
    			ListeD[i]=path.concat("\\"+ListeD[i]);
    			System.out.println(ListeD[i]);
     
    		}
     
    		for (int i =0;i<ListeD.length;i++){
     
    		Ms.main(ListeD[i],i);
    	}
    }
     
     
     
    	public static String[] listerCont(String t ){
     
    	        	File f=new File(t);
    		        String[] liste=f.list();
    				return liste;
    			}
    }
    et finalement voila la classe chargé de la fusion
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
     
    package test;
     
     
     
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
     
    import com.itextpdf.text.Document;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfImportedPage;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfWriter;
     
     
     
    public class Ms {
     
        public static void main(String path ,int n) throws FileNotFoundException {
     
        	String[] R = lister(path);
        	 for (int i =0;i<R.length;i++){
        		 R[i]=path.concat("\\"+R[i]);
        	 }
     
                List<InputStream> pdfs = new ArrayList<InputStream>();
     
     
                for (int i =0;i<R.length;i++){
     
                pdfs.add(new FileInputStream(R[i]));                     
                OutputStream output = new FileOutputStream(path+n+".pdf");
                Ms.concatPDFs(pdfs, output, true);
                System.out.println("fin traitement");
        	}
     
        }
     
     
     
        public static String[] lister(String path) {
        	File f=new File(path);
            String[] liste=f.list();
    		return liste;
     
    	}
     
     
     
    	public static void concatPDFs(List<InputStream> streamOfPDFFiles,
                OutputStream outputStream, boolean paginate) {
     
            Document document = new Document();
            try {
                List<InputStream> pdfs = streamOfPDFFiles;
                List<PdfReader> readers = new ArrayList<PdfReader>();
                int totalPages = 0;
                Iterator<InputStream> iteratorPDFs = pdfs.iterator();
     
                // Create Readers for the pdfs
                while (iteratorPDFs.hasNext()) {
                    InputStream pdf = iteratorPDFs.next();
                    PdfReader pdfReader = new PdfReader(pdf);
                    readers.add(pdfReader);
                    totalPages += pdfReader.getNumberOfPages();
                }
                // Create a writer for the outputstream
                PdfWriter writer = PdfWriter.getInstance(document, outputStream);
     
                document.open();
                BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                        BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
                // data
     
                PdfImportedPage page;
                int currentPageNumber = 0;
                int pageOfCurrentReaderPDF = 0;
                Iterator<PdfReader> iteratorPDFReader = readers.iterator();
     
                // Loop through the PDF files and add to the output.
                while (iteratorPDFReader.hasNext()) {
                    PdfReader pdfReader = iteratorPDFReader.next();
     
                    // Creer new page 
                    while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                        document.newPage();
                        pageOfCurrentReaderPDF++;
                        currentPageNumber++;
                        page = writer.getImportedPage(pdfReader,
                                pageOfCurrentReaderPDF);
                        cb.addTemplate(page, 0, 0);
     
                        // Code pour pagination.
                        if (paginate) {
                            cb.beginText();
                            cb.setFontAndSize(bf, 9);
                            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                                    + currentPageNumber + " of " + totalPages, 520,
                                    5, 0);
                            cb.endText();
                        }
                    }
                    pageOfCurrentReaderPDF = 0;
                }
     
                document.close();
     
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (document.isOpen())
                    document.close();
                try {
                    if (outputStream != null)
                        outputStream.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }
    merci de m'aider

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Tu appelle la méthode concat pour chaque PDF et donc à chaque itération tu ferme les stream présents dans la variable pdf. Du coup à l'itération suivant ils sont fermé et tu as ton erreur. Il me semble que ce que tu cherche à faire c'est la concaténation à la fin, pas pour chaque pdf. Donc au final à écrirececi:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    for (int i =0;i<R.length;i++){
                pdfs.add(new FileInputStream(R[i]));                     
    }
    OutputStream output = new FileOutputStream(path+n+".pdf");
    Ms.concatPDFs(pdfs, output, true);
    System.out.println("fin traitement");
    Et non pas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for (int i =0;i<R.length;i++){
     
                pdfs.add(new FileInputStream(R[i]));                     
                OutputStream output = new FileOutputStream(path+n+".pdf");
                Ms.concatPDFs(pdfs, output, true);
                System.out.println("fin traitement");
    }

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2014
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2014
    Messages : 26
    Points : 27
    Points
    27
    Par défaut
    Merci enormement, c'est une faute vraiment débile !! désolé pour le dérangement

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fusion de document pdf
    Par android59 dans le forum C#
    Réponses: 2
    Dernier message: 11/06/2013, 08h59
  2. [iText] Fusion de PDF
    Par seb55555 dans le forum Documents
    Réponses: 6
    Dernier message: 17/06/2010, 11h51
  3. Générer un document PDF
    Par link39001 dans le forum ASP
    Réponses: 5
    Dernier message: 11/05/2005, 20h39
  4. chargement et impression d'un document pdf
    Par waldo2188 dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 15/02/2005, 11h28
  5. Affichage de documents PDF
    Par Dajon dans le forum C++Builder
    Réponses: 5
    Dernier message: 10/10/2002, 11h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo