This commit is contained in:
NicolasLACROIX 2019-11-01 19:27:14 +01:00
parent d53bf32b7c
commit d67f1c65cc
2 changed files with 174 additions and 200 deletions

View File

@ -0,0 +1,10 @@
package GenerateurPdf;
public class IncorrectFormatException extends Exception {
private static final long serialVersionUID = 1L;
public IncorrectFormatException(String errorMessage) {
super(errorMessage);
}
}

View File

@ -32,27 +32,19 @@ public class SubjectGenerator {
private int height; private int height;
private int width; private int width;
public SubjectGenerator(Config c, PDDocument doc, PDRectangle format) { public SubjectGenerator(Config c) {
this.config = c;
this.pdDocument = doc;
this.format = format;
this.height = (int) Math.floor(format.getHeight());
this.width = (int) Math.floor(format.getWidth());
}
public SubjectGenerator(Config c, int nbPages, PDRectangle format) {
this.config = c; this.config = c;
this.pdDocument = new PDDocument(); this.pdDocument = new PDDocument();
this.format = format; try {
this.height = (int) Math.floor(format.getHeight()); this.format = SubjectGenerator.getFormatFromString(c.getParam().get("PaperSize"));
this.width = (int) Math.floor(format.getWidth()); } catch (IncorrectFormatException ife) {
for (int i = 0; i < nbPages; i++) { System.out.println("Format non reconnu. Format défini par défaut sur A4");
this.format = PDRectangle.A4;
}
this.pdDocument.addPage(new PDPage(this.format)); this.pdDocument.addPage(new PDPage(this.format));
} this.height = (int) this.format.getHeight();
} this.width = (int) this.format.getWidth();
public SubjectGenerator() {
this(null, null, null);
} }
public Config getConfig() { public Config getConfig() {
@ -95,6 +87,27 @@ public class SubjectGenerator {
this.width = width; this.width = width;
} }
public static PDRectangle getFormatFromString(String format) throws IncorrectFormatException {
switch (format) {
case "A0":
return PDRectangle.A0;
case "A1":
return PDRectangle.A1;
case "A2":
return PDRectangle.A2;
case "A3":
return PDRectangle.A3;
case "A4":
return PDRectangle.A4;
case "A5":
return PDRectangle.A5;
case "A6":
return PDRectangle.A6;
default:
throw new IncorrectFormatException("Le format spécifié n'est pas supporté.");
}
}
public static void drawDotedLine(PDPageContentStream pdPageContentStream, int xi, int yi, int xf, int yf) public static void drawDotedLine(PDPageContentStream pdPageContentStream, int xi, int yi, int xf, int yf)
throws IOException { throws IOException {
pdPageContentStream.moveTo(xi, yi); pdPageContentStream.moveTo(xi, yi);
@ -102,7 +115,6 @@ public class SubjectGenerator {
pdPageContentStream.lineTo(i, yf); pdPageContentStream.lineTo(i, yf);
pdPageContentStream.moveTo(i + 2, yf); pdPageContentStream.moveTo(i + 2, yf);
} }
// contentStream.fill();
} }
public static void drawCircle(PDPageContentStream pdPageContentStream, int cx, int cy, int r) throws IOException { public static void drawCircle(PDPageContentStream pdPageContentStream, int cx, int cy, int r) throws IOException {
@ -116,7 +128,7 @@ public class SubjectGenerator {
pdPageContentStream.fill(); pdPageContentStream.fill();
} }
public static void writeText(PDPageContentStream pdPageContentStream, String text, int x, int y) public static void writeText(PDPageContentStream pdPageContentStream, String text, float x, float y)
throws IOException { throws IOException {
pdPageContentStream.beginText(); pdPageContentStream.beginText();
pdPageContentStream.newLineAtOffset(x, y); pdPageContentStream.newLineAtOffset(x, y);
@ -124,6 +136,10 @@ public class SubjectGenerator {
pdPageContentStream.endText(); pdPageContentStream.endText();
} }
public static float getStrLenWithFont(String text, PDFont font, int fontSize) throws IOException {
return (font.getStringWidth(text) / 1000) * fontSize;
}
public void generateFooter() { public void generateFooter() {
int nbTotal = this.pdDocument.getNumberOfPages(); int nbTotal = this.pdDocument.getNumberOfPages();
int count = 1; int count = 1;
@ -137,12 +153,9 @@ public class SubjectGenerator {
int fontSize = 10; int fontSize = 10;
pdPageContentStream.setFont(font, fontSize); pdPageContentStream.setFont(font, fontSize);
String footer = String.valueOf(count) + " / " + String.valueOf(nbTotal); String footer = String.valueOf(count) + " / " + String.valueOf(nbTotal);
float titleWidth = (font.getStringWidth(footer) / 1000) * fontSize; float titleWidth = SubjectGenerator.getStrLenWithFont(footer, font, fontSize);
float titleHeight = (font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000) * fontSize; float titleHeight = (font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000) * fontSize;
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, footer, (width - titleWidth) / 2, (35 - titleHeight));
pdPageContentStream.newLineAtOffset((width - titleWidth) / 2, (35 - titleHeight));
pdPageContentStream.showText(footer);
pdPageContentStream.endText();
pdPageContentStream.close(); pdPageContentStream.close();
count++; count++;
} }
@ -180,10 +193,8 @@ public class SubjectGenerator {
SubjectGenerator.drawCircle(pdPageContentStream, this.width - 20, 30, 5); // bottom right SubjectGenerator.drawCircle(pdPageContentStream, this.width - 20, 30, 5); // bottom right
// number (top of page) +n/n/nn+ // number (top of page) +n/n/nn+
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, "+" + digA + "/" + digB + "/" + digC + "+",
pdPageContentStream.newLineAtOffset((this.width / 2) + 86, this.height - 30); (this.width / 2) + 86, this.height - 30);
pdPageContentStream.showText("+" + digA + "/" + digB + "/" + digC + "+");
pdPageContentStream.endText();
pdPageContentStream.close(); pdPageContentStream.close();
@ -237,10 +248,7 @@ public class SubjectGenerator {
pdPageContentStream.addRect(this.width - 238, this.height - 196, 155, 50); // RECT pdPageContentStream.addRect(this.width - 238, this.height - 196, 155, 50); // RECT
// text // text
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, "Ecrivez votre Nom", this.width - 236, this.height - 156);
pdPageContentStream.newLineAtOffset(this.width - 236, this.height - 156);
pdPageContentStream.showText("Ecrivez votre Nom");
pdPageContentStream.endText();
// dot lines // dot lines
// TODO: Enleve car difficulte d'ocr du fait de la presence des pointilles // TODO: Enleve car difficulte d'ocr du fait de la presence des pointilles
@ -275,17 +283,12 @@ public class SubjectGenerator {
// num rectangle // num rectangle
pdPageContentStream.addRect(this.width / 4, this.height - 146, 150, 40); pdPageContentStream.addRect(this.width / 4, this.height - 146, 150, 40);
pdPageContentStream.beginText(); String numEtudCons = Config.clearString(this.config.getParam().get("MarkField"));
pdPageContentStream.newLineAtOffset((this.width / 4) + 2, this.height - 115); SubjectGenerator.writeText(pdPageContentStream, numEtudCons, (this.width / 4) + 2, this.height - 115);
pdPageContentStream.showText("Ecrivez votre Numéro d'étudiant");
pdPageContentStream.endText();
// note rectangle // note rectangle
pdPageContentStream.addRect(this.width / 4, this.height - 200, 150, 40); pdPageContentStream.addRect(this.width / 4, this.height - 200, 150, 40);
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, "Note", (this.width / 4) + 2, this.height - 170);
pdPageContentStream.newLineAtOffset((this.width / 4) + 2, this.height - 170);
pdPageContentStream.showText("Note");
pdPageContentStream.endText();
pdPageContentStream.moveTo((this.width / 4) + 75, this.height - 160); pdPageContentStream.moveTo((this.width / 4) + 75, this.height - 160);
pdPageContentStream.lineTo((this.width / 4) + 75, this.height - 200); pdPageContentStream.lineTo((this.width / 4) + 75, this.height - 200);
@ -318,17 +321,13 @@ public class SubjectGenerator {
// draw first range of rectangles // draw first range of rectangles
pdPageContentStream.addRect(80 + (22 * i), this.height - 107, 11, 11); pdPageContentStream.addRect(80 + (22 * i), this.height - 107, 11, 11);
// write first range of numbers // write first range of numbers
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, String.valueOf(i), 80 + (22 * i) + 12,
pdPageContentStream.newLineAtOffset(80 + (22 * i) + 12, this.height - 105); this.height - 105);
pdPageContentStream.showText(String.valueOf(i));
pdPageContentStream.endText();
// draw second range of rectangles // draw second range of rectangles
pdPageContentStream.addRect(80 + (22 * i), this.height - 124, 11, 11); pdPageContentStream.addRect(80 + (22 * i), this.height - 124, 11, 11);
// write second range of numbers // write second range of numbers
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, String.valueOf(i), 80 + (22 * i) + 12,
pdPageContentStream.newLineAtOffset(80 + (22 * i) + 12, this.height - 122); this.height - 122);
pdPageContentStream.showText(String.valueOf(i));
pdPageContentStream.endText();
} }
pdPageContentStream.close(); pdPageContentStream.close();
@ -338,7 +337,6 @@ public class SubjectGenerator {
} }
public void generateHeader() { public void generateHeader() {
// TODO: change text considering config data
try { try {
PDPage page = this.pdDocument.getPage(0); PDPage page = this.pdDocument.getPage(0);
@ -358,53 +356,34 @@ public class SubjectGenerator {
// Sujet // Sujet
// center text : https://stackoverflow.com/a/6531362 // center text : https://stackoverflow.com/a/6531362
String subject = "Exemple PT S3T : de 1970 à lan 2000, 30 ans dhistoire"; // String subject = "Exemple PT S3T : de 1970 à lan 2000, 30 ans dhistoire";
String subject = Config.clearString(this.config.getParam().get("Title"));
int fontSize = 12; int fontSize = 12;
pdPageContentStream.setFont(font, fontSize); pdPageContentStream.setFont(font, fontSize);
float titleWidth = (font.getStringWidth(subject) / 1000) * fontSize; float titleWidth = SubjectGenerator.getStrLenWithFont(subject, font, fontSize);
float titleHeight = (font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000) * fontSize; float titleHeight = (font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000) * fontSize;
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, subject, (this.width - titleWidth) / 2,
pdPageContentStream.newLineAtOffset((this.width - titleWidth) / 2, (this.height - 57 - titleHeight)); (this.height - 57 - titleHeight));
pdPageContentStream.showText(subject);
pdPageContentStream.endText();
String subtitle = "Tricherie : Toutes consultations de sources numériques sont interdites !!";
titleWidth = (font.getStringWidth(subtitle) / 1000) * fontSize;
pdPageContentStream.beginText();
pdPageContentStream.newLineAtOffset((this.width - titleWidth) / 2, (this.height - 71 - titleHeight));
pdPageContentStream.showText(subtitle);
pdPageContentStream.endText();
// String subtitle = "Tricherie : Toutes consultations de sources numériques
// sont interdites !!";
String subtitle = Config.clearString(this.config.getParam().get("Presentation"));
titleWidth = SubjectGenerator.getStrLenWithFont(subtitle, font, fontSize);
SubjectGenerator.writeText(pdPageContentStream, subtitle, (this.width - titleWidth) / 2,
(this.height - 71 - titleHeight));
fontSize = 10; fontSize = 10;
// 1st line // 1st line
pdPageContentStream.setFont(font, fontSize); pdPageContentStream.setFont(font, fontSize);
String numConsignePart1 = " Veuillez coder votre numéro"; // String numConsignePart1 = " Veuillez coder votre numéro";
int heightOffset = this.height - 115;
titleWidth = (font.getStringWidth(numConsignePart1) / 1000) * fontSize; String numConsigne = Config.clearString(this.config.getParam().get("StudentField"));
pdPageContentStream.beginText();
pdPageContentStream.newLineAtOffset(this.width - 224, this.height - 94 - titleHeight);
pdPageContentStream.showText(numConsignePart1);
pdPageContentStream.endText();
// 2nd line
String numConsignePart2 = " détudiant ci-contre et écrire votre nom";
titleWidth = (font.getStringWidth(numConsignePart2) / 1000) * fontSize;
pdPageContentStream.beginText();
pdPageContentStream.newLineAtOffset(this.width - 244, this.height - 105 - titleHeight);
pdPageContentStream.showText(numConsignePart2);
pdPageContentStream.endText();
// 3rd line
String numConsignePart3 = " dans la case ci-dessous.";
titleWidth = (font.getStringWidth(numConsignePart3) / 1000) * fontSize;
pdPageContentStream.beginText();
pdPageContentStream.newLineAtOffset(this.width - 244, this.height - 118 - titleHeight);
pdPageContentStream.showText(numConsignePart3);
pdPageContentStream.endText();
for (String consigne : SubjectGenerator.setTextOnMultLines(numConsigne, this.width - 200, this.pdDocument,
font, fontSize)) {
SubjectGenerator.writeText(pdPageContentStream, consigne, this.width - 238, heightOffset);
heightOffset -= 10;
}
pdPageContentStream.close(); pdPageContentStream.close();
this.generateNumEtudAreaBis(); this.generateNumEtudAreaBis();
@ -425,7 +404,6 @@ public class SubjectGenerator {
int qIndex = 1; int qIndex = 1;
int heightOffset = 265; int heightOffset = 265;
int pageIndex = 0; int pageIndex = 0;
// System.out.println(this.config.getQuestions());
// pour chaque question // pour chaque question
ArrayList<Question> questions = this.config.getQuestions(); ArrayList<Question> questions = this.config.getQuestions();
// si les questions doivent etre melangees // si les questions doivent etre melangees
@ -433,7 +411,7 @@ public class SubjectGenerator {
Collections.shuffle(questions); Collections.shuffle(questions);
} }
for (Question q : questions) { for (Question q : questions) {
q.setTitre(q.getTitre().replace("\n", "")); // /\ TODO: must be done in Config /\ q.setTitre(q.getTitre().replace("\n", " ")); // /\ TODO: must be done in Config /\
PDPage page = this.pdDocument.getPage(pageIndex); PDPage page = this.pdDocument.getPage(pageIndex);
if (q instanceof QuestionBoite) { if (q instanceof QuestionBoite) {
this.generateOpenQ((QuestionBoite) q, qIndex, this.pdDocument, page, 25, heightOffset, isCorrected); this.generateOpenQ((QuestionBoite) q, qIndex, this.pdDocument, page, 25, heightOffset, isCorrected);
@ -446,6 +424,7 @@ public class SubjectGenerator {
// si on depasse la longeur de la page // si on depasse la longeur de la page
if (heightOffset > (this.height - 10)) { if (heightOffset > (this.height - 10)) {
pageIndex++; // on va a la page suivante pageIndex++; // on va a la page suivante
this.pdDocument.addPage(new PDPage(this.format)); // on cree une nouvelle page
heightOffset = 55; // on se place en haut de la page heightOffset = 55; // on se place en haut de la page
} }
} }
@ -471,34 +450,22 @@ public class SubjectGenerator {
PDFont font = PDType1Font.TIMES_ROMAN; PDFont font = PDType1Font.TIMES_ROMAN;
int fontSize = 10; int fontSize = 10;
pdPageContentStream.setFont(font, fontSize); pdPageContentStream.setFont(font, fontSize);
float titleLength = (font.getStringWidth(q.getTitre()) / 1000) * fontSize; float titleLength = SubjectGenerator.getStrLenWithFont(q.getTitre(), font, fontSize);
// System.out.println(qIndex + ". " + q.getTitre() + " - Width: " + titleLength
// + "/" + this.width + " - " + this.height + " - NbRep: " +
// q.getReponses().size() + "\n");
// Titre // Titre
// Si titre plus long que largeur page -> mise sur plusieurs lignes // Si titre plus long que largeur page -> mise sur plusieurs lignes
// TODO: revoir structure (factorisation possible)
if (titleLength > (this.width - 20)) { if (titleLength > (this.width - 20)) {
pdPageContentStream.beginText(); // TODO: replace Q by config data
pdPageContentStream.newLineAtOffset(widthOffset, this.height - heightOffset); SubjectGenerator.writeText(pdPageContentStream, "Q." + qIndex + " - ", widthOffset,
pdPageContentStream.showText("Q." + qIndex + " -"); this.height - heightOffset);
pdPageContentStream.endText();
for (String line : setTextOnMultLines(q.getTitre(), widthOffset, pdDocument, font, fontSize)) { for (String line : setTextOnMultLines(q.getTitre(), widthOffset, pdDocument, font, fontSize)) {
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, line, widthOffset + 22, this.height - heightOffset);
pdPageContentStream.newLineAtOffset(widthOffset + 22, this.height - heightOffset);
pdPageContentStream.showText(line);
pdPageContentStream.endText();
// System.out.println("\n" + line + "\n");
heightOffset += 20; heightOffset += 20;
} }
} else { } else {
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, "Q." + qIndex + " - " + q.getTitre(), widthOffset,
pdPageContentStream.newLineAtOffset(widthOffset, this.height - heightOffset); this.height - heightOffset);
pdPageContentStream.showText("Q." + qIndex + " - " + q.getTitre());
pdPageContentStream.endText();
} }
ArrayList<Reponse> reponses = q.getReponses(); ArrayList<Reponse> reponses = q.getReponses();
@ -508,15 +475,9 @@ public class SubjectGenerator {
} }
// pour chaque reponse // pour chaque reponse
for (Reponse r : reponses) { for (Reponse r : reponses) {
/**
* if (heightOffset > (this.height - 10)) { pageIndex++; // on va a la page
* suivante heightOffset = 55; // on se place en haut de la page }
*/
// on ecrit la reponse // on ecrit la reponse
pdPageContentStream.beginText(); SubjectGenerator.writeText(pdPageContentStream, r.getIntitule(), widthOffset + 40,
pdPageContentStream.newLineAtOffset(widthOffset + 40, this.height - heightOffset - 15); this.height - heightOffset - 15);
pdPageContentStream.showText(r.getIntitule());
pdPageContentStream.endText();
// on ajoute une zone pour cocher (carre) // on ajoute une zone pour cocher (carre)
pdPageContentStream.addRect(widthOffset + 20, this.height - heightOffset - 16, 10, 10); pdPageContentStream.addRect(widthOffset + 20, this.height - heightOffset - 16, 10, 10);
// si l'on souhait generer le corrige // si l'on souhait generer le corrige
@ -532,19 +493,8 @@ public class SubjectGenerator {
} }
heightOffset += 17.5; heightOffset += 17.5;
} }
pdPageContentStream.close(); pdPageContentStream.close();
/*
* pdPageContentStream.beginText(); pdPageContentStream.newLineAtOffset(80 + (22
* * i) + 12, height - 105); pdPageContentStream.showText(String.valueOf(i));
* pdPageContentStream.endText(); // draw second range of rectangles
* pdPageContentStream.addRect(80 + (22 * i), height - 124, 11, 11); // write
* second range of numbers pdPageContentStream.beginText();
* pdPageContentStream.newLineAtOffset(80 + (22 * i) + 12, height - 122);
* pdPageContentStream.showText(String.valueOf(i));
* pdPageContentStream.endText();
*/
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
@ -558,32 +508,44 @@ public class SubjectGenerator {
PDFont font = PDType1Font.TIMES_ROMAN; PDFont font = PDType1Font.TIMES_ROMAN;
int fontSize = 10; int fontSize = 10;
pdPageContentStream.setFont(font, fontSize); pdPageContentStream.setFont(font, fontSize);
float titleLength = (font.getStringWidth(q.getTitre()) / 1000) * fontSize; float titleLength = SubjectGenerator.getStrLenWithFont(q.getTitre(), font, fontSize);
float lastLineLenght = titleLength; float lastLineLenght = titleLength;
if (titleLength > (this.width - 20)) { if (titleLength > (this.width - 20)) {
SubjectGenerator.writeText(pdPageContentStream, "Q." + qIndex + " -", widthOffset, SubjectGenerator.writeText(pdPageContentStream, "Q." + qIndex + " - ", widthOffset,
this.height - heightOffset); this.height - heightOffset);
System.out.println("\nTITRE : " + q.getTitre() + "\n");
for (String line : setTextOnMultLines(q.getTitre(), widthOffset, pdDocument, font, fontSize)) { for (String line : setTextOnMultLines(q.getTitre(), widthOffset, pdDocument, font, fontSize)) {
SubjectGenerator.writeText(pdPageContentStream, line, widthOffset + 22, this.height - heightOffset); SubjectGenerator.writeText(pdPageContentStream, line, widthOffset + 22, this.height - heightOffset);
heightOffset += 20; heightOffset += 20;
lastLineLenght = (font.getStringWidth(line) / 1000) * fontSize; lastLineLenght = SubjectGenerator.getStrLenWithFont(line, font, fontSize);
} }
} else { } else {
SubjectGenerator.writeText(pdPageContentStream, "Q." + qIndex + " - " + q.getTitre(), widthOffset, SubjectGenerator.writeText(pdPageContentStream, "Q." + qIndex + " - " + q.getTitre(), widthOffset,
this.height - heightOffset); this.height - heightOffset);
heightOffset += 20;
} }
// on cherche a savoir a partir d'ou on peut mettre le texte pour etre le plus a
// droite possible en fonction du nombre de reponses associees
int maxX = this.width - widthOffset;
for (Reponse r : q.getReponses()) { for (Reponse r : q.getReponses()) {
if ((titleLength + 16) > (this.width - 20)) { maxX -= SubjectGenerator.getStrLenWithFont(r.getIntitule(), font, fontSize);
SubjectGenerator.writeText(pdPageContentStream, r.getIntitule(), widthOffset + 20, // qu'occupe la case a
this.height - heightOffset - 15); // cocher associee
pdPageContentStream.addRect(widthOffset + 20, this.height - heightOffset - 16, 10, 10); }
// on affiche les reponses et les cases a cocher associees
// int varWidthOffset = widthOffset;
for (Reponse r : q.getReponses()) {
if ((lastLineLenght + 5) > maxX) {
pdPageContentStream.addRect(maxX, this.height - heightOffset, 10, 10);
SubjectGenerator.writeText(pdPageContentStream, r.getIntitule(), maxX + 12,
this.height - heightOffset);
} else { } else {
SubjectGenerator.writeText(pdPageContentStream, r.getIntitule(), pdPageContentStream.addRect(maxX, (this.height - heightOffset) + 20, 10, 10);
widthOffset + (int) lastLineLenght + 15, this.height - heightOffset - 15); SubjectGenerator.writeText(pdPageContentStream, r.getIntitule(), maxX + 12,
pdPageContentStream.addRect(widthOffset + (int) lastLineLenght, this.height - heightOffset - 16, 10, (this.height - heightOffset) + 20);
10);
} }
if (isCorrected) { if (isCorrected) {
// si c'est une bonne reponse // si c'est une bonne reponse
@ -595,19 +557,22 @@ public class SubjectGenerator {
} else { } else {
pdPageContentStream.stroke(); // carre vide pdPageContentStream.stroke(); // carre vide
} }
maxX += SubjectGenerator.getStrLenWithFont(r.getIntitule(), font, fontSize) + 20;
} }
heightOffset += 17.5; heightOffset += 17.5;
// zone d'ecriture (rectangle) // zone d'ecriture (rectangle)
// TODO: ameliorer
int widthMargin = 60; int widthMargin = 60;
int linesLenght = q.getNbligne() * 20; // 20 = taille d'une ligne int linesLenght = q.getNbligne() * 20; // 20 = taille d'une ligne
int yLenght = (this.width - widthOffset - widthMargin); int yLenght = (this.width - widthMargin);
pdPageContentStream.addRect(widthOffset + 20, this.height - heightOffset - 16, yLenght, linesLenght); pdPageContentStream.addRect(widthOffset + 20, this.height - heightOffset - 10, yLenght, linesLenght);
int yCursor = this.height - heightOffset - 16; int yCursor = this.height - heightOffset - 16;
for (int i = 0; i < q.getNbligne(); i++) { for (int i = 0; i < q.getNbligne(); i++) {
yCursor = this.height - heightOffset - 16 - (i * 2); // on descends y de la taille d'une ligne yCursor = this.height - heightOffset - 16 - (i * 2); // on descends y de la taille d'une ligne
// SubjectGenerator.drawDotedLine(pdPageContentStream, xi, widthOffset + 30, xf, // SubjectGenerator.drawDotedLine(pdPageContentStream, widthOffset + 23,
// yLenght); // this.height - heightOffset - (20 * i), yLenght - 23, this.height -
// heightOffset - (20 * i));
} }
pdPageContentStream.close(); pdPageContentStream.close();
@ -618,61 +583,60 @@ public class SubjectGenerator {
public static ArrayList<String> setTextOnMultLines(String text, int widthOffset, PDDocument pdDocument, PDFont font, public static ArrayList<String> setTextOnMultLines(String text, int widthOffset, PDDocument pdDocument, PDFont font,
int fontSize) { int fontSize) {
// TODO: voir si l'on laisse en static ou non
float subStrLength = 0;
int width = (int) pdDocument.getPage(0).getMediaBox().getWidth(); int width = (int) pdDocument.getPage(0).getMediaBox().getWidth();
ArrayList<String> lines = new ArrayList<String>(); ArrayList<String> lines = new ArrayList<>();
int widthMargin = 60;
int lastSpace = -2;
int spaceIndex = 0;
while (text.length() > 0) {
// System.out.println("1. Si: " + spaceIndex + " - Ls: " + lastSpace + " TxtL: "
// + text.length());
spaceIndex = text.indexOf(' ', lastSpace + 2);
// System.out.println("2. Si: " + spaceIndex + " - Ls: " + lastSpace + " : " +
// text + "\n");
String subStr = "";
// System.out.println("1. SubL: " + subStrLength + " - W: " + (width -
// widthOffset));
if (spaceIndex == -1) { if (text.isBlank()) {
lines.add(text); // si le texte est une chaine vide ou ne contenant que des espaces
text = ""; return lines;
} else {
subStr = text.substring(0, spaceIndex);
} }
int widthMargin = 60;
int textSize = 0;
try { try {
subStrLength = (font.getStringWidth(subStr) / 1000) * fontSize; textSize = (int) SubjectGenerator.getStrLenWithFont(text, font, fontSize); // taille totale du texte
} catch (IOException ioe) { } catch (IOException ioe) {
System.err.print(ioe.getMessage()); ioe.printStackTrace();
} }
// System.out.println("2. SubL: " + subStrLength + " - W: " + (width - int availableWidth = width - widthOffset - widthMargin; // largeur disponible (marge enlevee)
// widthOffset)); int index = 0;
if (subStrLength > (width - widthOffset - widthMargin)) { while (textSize > availableWidth) {
if (lastSpace > 0) { // tant que la taille du texte restant depasse en largeur
lastSpace = spaceIndex; try {
while (SubjectGenerator.getStrLenWithFont(text.substring(0, index), font, fontSize) < availableWidth) {
// tant que la taille de la sous chaine de caractere ne depasse pas
index++;
System.out.println(index);
} }
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (text.substring(0, index).endsWith(" ") || text.substring(0, index).endsWith("-")
|| text.substring(index, text.length() - 1).startsWith(" ")
|| text.substring(index, text.length() - 1).startsWith("-")) {
// System.out.println("[IF] SubL: " + subStrLength + " - W: " + (width - lines.add(text.substring(0, index));
// widthOffset)); text = text.substring(index);
subStr = text.substring(0, lastSpace); } else if (text.substring(0, index - 1).endsWith(" ") || text.substring(0, index - 1).endsWith("-")) {
lines.add(subStr); lines.add(text.substring(0, index - 1));
text = text.substring(lastSpace).trim(); text = text.substring(index - 1);
lastSpace = -1;
} else if (spaceIndex == lastSpace) {
lines.add(text);
text = "";
// System.out.println("LTxt: " + text.length());
} else { } else {
lastSpace = spaceIndex; // sinon on coupe le mot
} lines.add(text.substring(0, index - 1) + "-");
text = text.substring(index - 1);
} }
try {
textSize = (int) SubjectGenerator.getStrLenWithFont(text, font, fontSize);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
lines.add(text);
return lines; return lines;
} }
@ -686,20 +650,20 @@ public class SubjectGenerator {
} }
public static void main(String args[]) throws IOException { public static void main(String args[]) throws IOException {
Config c = new Config("E:\\source.txt"); // devra etre fait dans Exec
c.readConfig(); Config config = new Config("E:\\source.txt");
config.readConfig();
// -------------------------
// instanciate a new SubjectGenerator // instanciate a new SubjectGenerator
// TODO: change PDRectangle.A4 to the format specified in the config SubjectGenerator subjectGenerator = new SubjectGenerator(config);
// TODO: number of page = automatic
SubjectGenerator subjectGenerator = new SubjectGenerator(c, 5, PDRectangle.A4);
// ATTENTION : generateBody est place en premier car c'est elle qui va generer
// le nombre de page suffisant
subjectGenerator.generateBody(false); // false = on genere le sujet, pas le corrige
subjectGenerator.generateHeader(); subjectGenerator.generateHeader();
subjectGenerator.generateMarks(); subjectGenerator.generateMarks();
// subjectGenerator.generateNumEtudAreaBis();
// subjectGenerator.generateNameArea();
subjectGenerator.generateFooter(); subjectGenerator.generateFooter();
subjectGenerator.generateBody(false); // false = on genere le sujet, pas le corrige
subjectGenerator.save("E:\\testPDFMarks.pdf"); subjectGenerator.save("E:\\testPDFMarks.pdf");