2 * To change this template, choose Tools | Templates
\r
3 * and open the template in the editor.
\r
8 import java.io.IOException;
\r
9 import java.io.OutputStream;
\r
10 import java.math.BigInteger;
\r
11 import java.util.List;
\r
12 import java.util.Random;
\r
13 import org.apache.pdfbox.cos.COSArray;
\r
14 import org.apache.pdfbox.cos.COSInteger;
\r
15 import org.apache.pdfbox.cos.COSString;
\r
16 import org.apache.pdfbox.exceptions.COSVisitorException;
\r
17 import org.apache.pdfbox.pdfparser.PDFStreamParser;
\r
18 import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
\r
19 import org.apache.pdfbox.pdmodel.PDDocument;
\r
20 import org.apache.pdfbox.pdmodel.PDPage;
\r
21 import org.apache.pdfbox.pdmodel.common.PDStream;
\r
22 import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
\r
23 import org.apache.pdfbox.pdmodel.font.PDFont;
\r
24 import org.apache.pdfbox.pdmodel.font.PDType1Font;
\r
25 import org.apache.pdfbox.util.PDFOperator;
\r
26 import org.apache.pdfbox.util.PDFTextStripper;
\r
27 import org.apache.pdfbox.util.TextPosition;
\r
31 * @author bitar ahmad
\r
33 public class Final5 extends PDFTextStripper{
\r
34 private static String v1;
\r
35 private static String s;
\r
36 private static final String ZEROS = String.format("%064d", 0);
\r
37 private static String kl;
\r
38 private static double[] table1;
\r
39 private static int texte_length;
\r
40 private static int i;
\r
41 private static double[][] signal;
\r
42 private static double jjj;
\r
43 private static PDPageContentStream content;
\r
44 private static double fff;
\r
45 private static PDType1Font font;
\r
46 private static int iii;
\r
47 private static int kkk;
\r
48 private static String texte;
\r
49 private static double[][] table3;
\r
50 private static double[][] hh1;
\r
51 private static double[][] u2;
\r
53 public Final5() throws IOException {
\r
54 super.setSortByPosition(true);
\r
59 * @param args the command line arguments
\r
61 public static void main(String[] args) throws IOException, COSVisitorException {
\r
62 Final5 printer = new Final5();
\r
65 //Génération du message secret
\r
68 String message= "UFC";
\r
69 char myA [] = new char[message.length()];
\r
70 String x []=new String[message.length()];
\r
74 //Random r = new Random();
\r
75 //int valeurmin= 357;
\r
76 //int valeurmax= 400;
\r
77 // int difference = valeurmax-valeurmin;
\r
78 //difference=difference+1;
\r
79 //int valeur = valeurmin + r.nextInt( difference);
\r
80 //float valeur1= (float)valeur;
\r
81 //double mu= valeur1/100;
\r
84 //x1=(mu*x0)*(1-x0);
\r
86 //if(message.length()<=4){
\r
88 Random r = new Random();
\r
89 for (int i=0; i<message.length()*8; i++){
\r
91 if(r.nextBoolean())x1=1;
\r
95 //s = Long.toBinaryString(Double.doubleToLongBits(x1));
\r
96 //s = ZEROS.substring(s.length()) + s;
\r
97 for (int i=0; i<message.length(); i++){
\r
98 myA[i]=message.charAt(i);
\r
99 x[i]=String.format("%8s", Integer.toBinaryString(myA[i])).replace(" ", "0");
\r
100 v1=v1.concat(x[i]);
\r
102 //v1=String.format("%"+(s.length())+"s", v1).replace(" ", "0");
\r
103 System.out.println(s);
\r
104 System.out.println(v1);
\r
105 System.out.println(s.length());
\r
106 System.out.println(v1.length());
\r
108 //for (int i=0; i<message.length(); i++){
\r
109 // myA[i]=message.charAt(i);
\r
110 // x[i]=String.format("%16s", Integer.toBinaryString(myA[i])).replace(" ", "0");
\r
111 //v1=v1.concat(x[i]);
\r
113 //s = String.format("%"+v1.length()+"s", Long.toBinaryString(Double.doubleToLongBits(x1))).replace(" ", "0");
\r
114 //System.out.println(v1);
\r
115 //System.out.println(s);
\r
116 //System.out.println(s.length());
\r
120 BigInteger no1 = new BigInteger(v1,2);
\r
121 BigInteger no2 = new BigInteger(s,2);
\r
124 kl = no3.toString(2);
\r
125 while (kl.length() <v1.length()){
\r
126 kl=0+kl; //secret message
\r
128 System.out.println(kl);
\r
129 System.out.println(kl.length());
\r
134 doc= PDDocument.load("violin.pdf");
\r
135 List pages = doc.getDocumentCatalog().getAllPages();
\r
136 PDPage page = (PDPage) pages.get(0);
\r
138 PDFTextStripper stripper =new PDFTextStripper();
\r
139 texte=stripper.getText(doc);
\r
142 texte_length=texte.length();
\r
143 System.out.println(texte);
\r
144 System.out.println("la taille du texte est: "+texte_length);
\r
146 //crátion de deux tableaux
\r
147 table1=new double[texte_length*2];
\r
148 PDStream contents = page.getContents();
\r
152 PDFStreamParser parser = new PDFStreamParser(contents.getStream());
\r
154 List tokens = parser.getTokens();
\r
155 for (int o = 0; o < tokens.size(); o++)
\r
157 Object next = tokens.get(o);
\r
158 if (next instanceof PDFOperator) {
\r
159 PDFOperator op = (PDFOperator) next;
\r
160 if (op.getOperation().equals("Tj"))
\r
162 COSString previous = (COSString) tokens.get(o - 1);
\r
163 String string = previous.getString();
\r
165 tokens.set(o-1, COSInteger.get(3));
\r
166 tokens.set(o, PDFOperator.getOperator("Tr"));
\r
167 tokens.add(++o, new COSString(string));
\r
168 tokens.add(++o, PDFOperator.getOperator("Tj"));
\r
169 tokens.add(++o, COSInteger.get(0));
\r
170 tokens.add(++o, PDFOperator.getOperator("Tr"));
\r
171 tokens.add(++o, new COSString(""));
\r
172 tokens.add(++o, PDFOperator.getOperator("Tj"));
\r
173 }else if(op.getOperation().equals("TJ")){
\r
174 COSArray previous = (COSArray) tokens.get(o - 1);
\r
176 tokens.set(o-1, COSInteger.get(3));
\r
177 tokens.set(o, PDFOperator.getOperator("Tr"));
\r
178 tokens.add(++o, previous);
\r
179 tokens.add(++o, PDFOperator.getOperator("TJ"));
\r
180 tokens.add(++o, COSInteger.get(0));
\r
181 tokens.add(++o, PDFOperator.getOperator("Tr"));
\r
186 PDStream updatedStream = new PDStream(doc);
\r
187 OutputStream out = updatedStream.createOutputStream();
\r
188 ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
\r
189 tokenWriter.writeTokens(tokens);
\r
190 page.setContents(updatedStream);
\r
200 int L=39; //fixed value (n/k)
\r
202 if (contents != null) {
\r
204 table1[0]=(float)15.0;
\r
205 System.out.println(table1[0]);
\r
206 printer.processStream(page, page.findResources(), page.getContents().getStream());
\r
209 signal= new double[kl.length()*L*2][1];
\r
211 while(k<kl.length()*L*2){
\r
212 signal[k][0]=table1[k];
\r
220 float delta=(float)10;
\r
222 float d1= +delta/4;
\r
223 // float alpha=(float)0.5;
\r
226 double v[]= new double[L];
\r
227 double v_carre[]= new double[L];
\r
228 double u[]= new double[L];
\r
230 for(int y=0;y<L;y++){
\r
231 Random r1 = new Random();
\r
232 int valeurmin1= -50;
\r
233 int valeurmax1= +50;
\r
234 int difference1 = valeurmax1-valeurmin1;
\r
235 difference1=difference1+1;
\r
236 int valeur2 = valeurmin1 + r1.nextInt( difference1);
\r
237 float valeur3= (float)valeur2;
\r
239 System.out.println(v[y]);
\r
240 v_carre[y]=v[y]*v[y];
\r
241 somme= somme+v_carre[y];
\r
242 //System.out.println(v_carre[y]);
\r
247 double squart= Math.sqrt(somme);
\r
248 //System.out.println(squart);
\r
250 for(int y=0;y<L;y++){
\r
254 double[][] p = new double[1][u.length];
\r
255 for(int i = 0; i < u.length; i++) {
\r
264 //double table3[]= new double[L];
\r
265 table3= new double[signal.length][1];
\r
266 u2= new double[u.length][1];
\r
268 for(int ii=0;ii<kl.length();ii++){
\r
270 if(kl.charAt(ii)=='0'){
\r
271 hh1=new double[1][1];
\r
273 for(int uu1=0;uu1<L;uu1++){
\r
274 hh1[0][0]+= (signal[kk][0]*p[0][uu1]);
\r
278 double cc1=Math.round(((hh1[0][0])+d0)/delta);
\r
279 double cc2=cc1*delta -d0;
\r
280 double cc3=(hh1[0][0]);
\r
282 double cc4= cc2-cc3;
\r
284 for(int yy1=0;yy1<L;yy1++){
\r
285 u2[yy1][0]=cc4*u[yy1];
\r
288 for(int yy1=0;yy1<L;yy1++){
\r
289 table3[bb][0]=signal[rr][0]+u2[yy1][0];
\r
290 table3[bb+1][0]=signal[rr+1][0];
\r
299 }else if(kl.charAt(ii)=='1'){
\r
300 hh1=new double[1][1];
\r
302 for(int uu1=0;uu1<L;uu1++){
\r
303 hh1[0][0]+= (signal[kk][0]*p[0][uu1]);
\r
307 double cc1=Math.round(((hh1[0][0])+d1)/delta);
\r
308 double cc2=cc1*delta -d1;
\r
309 double cc3=(hh1[0][0]);
\r
311 double cc4= cc2-cc3;
\r
313 for(int yy1=0;yy1<L;yy1++){
\r
314 u2[yy1][0]=cc4*u[yy1];
\r
317 for(int yy1=0;yy1<L;yy1++){
\r
318 table3[bb][0]=signal[rr][0]+u2[yy1][0];
\r
319 table3[bb+1][0]=signal[rr+1][0];
\r
334 for (int tt=0; tt<signal.length;tt++){
\r
335 System.out.println(signal[tt][0]);
\r
337 System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
\r
338 for (int tt=0; tt<table3.length;tt++){
\r
339 System.out.println(table3[tt][0]);
\r
344 //processus d'extraction:
\r
346 //String extract_message="";
\r
348 //while (rr1<table3.length){
\r
351 //double cc1a=Math.round(table3[rr1][0]*p[0][0] + table3[rr1+1][0]*p[0][1]+d0 / delta);
\r
352 //double cc2a=cc1a*delta -d0;
\r
353 //double cc3a=table3[rr1][0]*p[0][0]+table3[rr1+1][0]*p[0][1];
\r
354 //double cc4a=cc3a-cc2a;
\r
355 //double cc5a= Math.sqrt(cc4a*cc4a);
\r
360 //double cc1b=Math.round(table3[rr1][0]*p[0][0] +table3[rr1+1][0]*p[0][1]+d1/delta);
\r
361 //double cc2b=cc1b*delta -d1;
\r
362 //double cc3b=table3[rr1][0]*p[0][0]+table3[rr1+1][0]*p[0][1];
\r
363 //double cc4b=cc3b-cc2b;
\r
364 //double cc5b= Math.sqrt(cc4b*cc4b);
\r
369 // extract_message= extract_message+'0';
\r
370 //}else if (cc5a>cc5b){
\r
371 // extract_message= extract_message+'1';
\r
376 //System.out.println("le messag extrait est"+extract_message);
\r
389 font= PDType1Font.HELVETICA;
\r
392 content = new PDPageContentStream(doc,page,true,true);
\r
393 PDFont font1 = PDType1Font.HELVETICA;
\r
394 content.setFont(font1, 11);
\r
395 TestRewriter rewriter = new TestRewriter(content);
\r
396 rewriter.processStream(page, page.findResources(), page.getContents().getStream());
\r
400 System.out.println(iii);
\r
403 int hhh=kl.length()*2;
\r
404 System.out.println(texte.charAt(uuu));
\r
405 while (uuu<texte.length()){
\r
408 content = new PDPageContentStream(doc,page,true,true);
\r
409 content.setFont(font, 11);
\r
410 content.beginText();
\r
411 fff = 400-table1[hhh+1];
\r
412 content.appendRawCommands(""+table1[hhh]+" "+fff+" Td");
\r
413 content.appendRawCommands("("+texte.charAt(uuu)+")"+" Tj\n");
\r
423 doc.save("modified_violin.pdf");
\r
436 * @param text The text to be processed
\r
438 /* this is questionable, not sure if needed... */
\r
439 public void processTextPosition(TextPosition text) {
\r
443 System.out.println("String[" + text.getXDirAdj() + ","
\r
444 + text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale="
\r
445 + text.getXScale() + " height=" + text.getHeightDir() + " space="
\r
446 + text.getWidthOfSpace() + " width="
\r
447 + text.getWidthDirAdj() + "]" + text.getCharacter());
\r
450 table1[i]=text.getXDirAdj();
\r
451 System.out.println(table1[i]);
\r
453 table1[i]=text.getYDirAdj();
\r
454 System.out.println(table1[i]);
\r
458 table1[i]=text.getYDirAdj();
\r
459 System.out.println(table1[i]);
\r
467 public static class TestRewriter extends PDFTextStripper{
\r
469 final PDPageContentStream content;
\r
471 public TestRewriter(PDPageContentStream content)throws IOException {
\r
473 this.content = content;
\r
476 public void processTextPosition(TextPosition text)
\r
480 while(iii<texte.length()){
\r
485 content.beginText();
\r
486 if(iii==77 || iii==157 || iii==236 || iii==309 || iii==383 || iii==460 || iii==537 || iii==611 || iii==686 || iii==760 || iii==835 || iii==912){
\r
489 if(kkk<signal.length){
\r
491 jjj = 400-table3[kkk+1][0];
\r
492 content.appendRawCommands(""+table3[kkk][0]+" "+jjj+" Td");
\r
493 content.drawString(""+texte.charAt(iii));
\r
494 //content.appendRawCommands("("+texte.charAt(iii)+")"+"Tj\n");
\r
500 }else if(kkk>=signal.length){
\r
502 jjj = 400-table1[kkk+1];
\r
503 content.appendRawCommands(""+table1[kkk]+" "+jjj+" Td");
\r
504 content.drawString(""+texte.charAt(iii));
\r
516 } catch (IOException ex) {
\r
517 ex.printStackTrace();
\r