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

Documents Java Discussion :

[iText] Seul le titre principal est ajouté


Sujet :

Documents Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    123
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2010
    Messages : 123
    Points : 137
    Points
    137
    Par défaut [iText] Seul le titre principal est ajouté
    Bonjour à tous,

    J'utilise iText pour générer un pdf. J'utilise les ColumnText pour éviter les orphelins. Mon problème c'est que seul mon premier titre s'affiche dans le document. Ensuite, les blocs de textes sont affichés mais pas les sous-titres respectifs...

    Si une âme charitable pouvait me venir en aide... ?

    D'avance merci !

    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
     
    private Document doc = new Document();
    	private boolean isUse = false;
    	private PdfWriter pWriter;
    	private PdfContentByte pdfCb;
    	private ColumnText ct;
    	private float position;
    	private int statut;
     
     
    	/** generate the PDF to the given OutputSteam */
    	public void generatePdf(Section rootSection, OutputStream outputStream) throws DocumentException, IOException {
    		// Bind the document and the outputSteam together.
    		this.pWriter = PdfWriter.getInstance(this.doc, outputStream);
     
    		if (!this.isUse){
    			this.doc.open();
    			//Create the column to avoid orphan
    			this.pdfCb = pWriter.getDirectContent();
    			this.ct = new ColumnText(this.pdfCb);
    			this.ct.setSimpleColumn(36, 36,	PageSize.A4.getWidth() - 36, PageSize.A4.getHeight() - 36, 18, Element.ALIGN_LEFT);
    			this.statut = ColumnText.START_COLUMN;
     
    			recursiveWalk(rootSection);
     
    			this.doc.close();
    			this.isUse = true;
    		} else{
    			throw new IllegalStateException("Instances of this class are like condoms : not thread safe and not reusable. It should be thrown away after use. ;-)");
    		}
    	}
     
     
    	/** Accumulates paragraphs for the sections (and its subsections) in this.doc 
             * @throws IOException */
    	private void recursiveWalk(Section currentSection) throws DocumentException, IOException{
     
    		// Create Title
    		Chunk pTitle = createTitleChunk(currentSection);
    		addTitle(pTitle); //Add Title
    		addBody(currentSection); //Add Body	
     
    		// Childs (recursive call)
    		for (Section sSection : currentSection.getSubSections()) {
    			recursiveWalk(sSection);
    		}
    	}
     
     
    	private void addBody(Section currentSection) throws IOException,
    			DocumentException {
     
    		CourseTextFormatter sectionFormatter = new CourseTextFormatter("", currentSection.getBody());
    		StyleSheet styleBody = new StyleSheet();
    		styleBody.loadStyle("p", "size", "8"); //Style of the body
    		List<Element> bodyElements; //Create List with the paragraph
    		bodyElements = HTMLWorker.parseToList(new StringReader(sectionFormatter.format()), styleBody); //Format text and apply style
     
    		for (Element element : bodyElements){
    			this.ct.addElement(element); //Add element
    			this.position = this.ct.getYLine(); //Adjust Y pointer
    			this.statut = this.ct.go(true);
     
    			if (!ColumnText.hasMoreText(this.statut)) {
    				this.ct.addElement(element);
    				this.ct.setYLine(this.position);
    				this.ct.go(false);
    				}
    				else {
    				this.doc.newPage();
    				this.ct.setYLine(PageSize.A4.getHeight() - 36);
    				this.ct.go();
    				}
     
    			this.doc.add(Chunk.NEWLINE);  //Add return to line
    		}
    	}
     
    	private void addTitle(Chunk pTitle) throws DocumentException {
    		this.position = this.ct.getYLine(); //Adjust Y pointer
    		this.ct.setYLine(this.position);
    		this.ct.addText(pTitle);
    		this.ct.go();
    		this.doc.add(Chunk.NEWLINE);
    		this.doc.add(Chunk.NEWLINE);
    	}

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    123
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2010
    Messages : 123
    Points : 137
    Points
    137
    Par défaut
    EDIT :

    J'ai résolu mon problème. Le fait est que je confondais le TextMode et me CompositMode. Désormais, tous mon texte s'affiche. Cependant, je n'arrive pas à garder les paragraphes ensembles... Pouvez-vous jeter un oeil à mon code et me dire ce qui foire ?

    D'avance merci !!!

    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
    /** Accumulates paragraphs for the sections (and its subsections) in this.doc 
             * @throws IOException */
    	private void recursiveWalk(Section currentSection) throws DocumentException, IOException{
     
    		Chunk pTitle = createTitleChunk(currentSection); // Create Title Style
    		addTitle(pTitle); //Add Title
    		addBody(currentSection); //Add Body
     
    		// Childs (recursive call)
    		for (Section sSection : currentSection.getSubSections()) {
    			recursiveWalk(sSection);
    		}
    	}
     
     
    	private void addBody(Section currentSection) throws IOException,
    			DocumentException {
     
    		String body = currentSection.getBody();
     
    		CourseTextFormatter sectionFormatter = new CourseTextFormatter("", body);
    		StyleSheet styleBody = new StyleSheet();
    		styleBody.loadStyle("p", "size", "8"); //Style of the body
    		List<Element> bodyElements; //Create List with the paragraph
    		bodyElements = HTMLWorker.parseToList(new StringReader(sectionFormatter.format()), styleBody); //Format text and apply style
     
    		for (Element element : bodyElements){
    			this.ct.addElement(element);
    			this.ct.go();
     
    			while (true){
    				this.status = this.ct.go();
     
    				if (!ColumnText.hasMoreText(this.status)){
    					break;
    				}
    				this.doc.newPage();
    				this.ct.setSimpleColumn(36, 36,	PageSize.A4.getWidth() - 36, PageSize.A4.getHeight() - 36, 18, Element.ALIGN_LEFT);
    			}
    		}
    	}
     
    	private void addTitle(Chunk pTitle) throws DocumentException {
    		this.ct.addElement(new Paragraph("\n"));
    		this.ct.addElement(new Paragraph(pTitle));
    		this.ct.addElement(new Paragraph("\n"));
    		this.ct.go();
    	}

Discussions similaires

  1. Seul le premier chiffre est enregistré à l'insertion
    Par franquis dans le forum Langage
    Réponses: 6
    Dernier message: 11/07/2009, 18h08
  2. Erreur lors du déploiement un "L" est ajouté
    Par piopium dans le forum Glassfish et Payara
    Réponses: 7
    Dernier message: 04/03/2009, 16h43
  3. Réponses: 3
    Dernier message: 07/07/2008, 16h32
  4. Seulement un élément est ajouté au groupBox
    Par phfle1 dans le forum Windows Forms
    Réponses: 1
    Dernier message: 12/06/2008, 00h07
  5. [DOM] Seul le dernier élément est affiché
    Par COLOMBAT dans le forum Format d'échange (XML, JSON...)
    Réponses: 3
    Dernier message: 21/12/2007, 18h12

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