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

avec Java Discussion :

Rendre quelques caractères comme "invisible" dans un document PDF déjà existant en utilisant PDFBox


Sujet :

avec Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Liban

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 16
    Points : 13
    Points
    13
    Par défaut Rendre quelques caractères comme "invisible" dans un document PDF déjà existant en utilisant PDFBox
    Bonjour àtous,


    j'ai déjà lu le "Tj" operator d'un document PDF existant en utilisant la librairie Apache PDFBox.
    Quand j'ai éxecuté complètement cette tâche, j'ai pu faire aussi le remplacement de quelques caractères.
    Si on considère par exemple un document PDF qui contient (hello world)Tj, donc le code ci-dessous permet de remplacer "hello" par "hi123". Enfin, on aura un document PDF modifié contenant (hi123 world)Tj à la place de (hello world)Tj.

    Mon grand problème maintenant est comment éditer le code ci-dessous afin de rendre "hello" comme un mot invisible (text rendering mode "invisible"). Donc je ne veux jamais remplacer "hello" par "hi123" mais de rendre "hello" invisible (disappear) et à la fin, le document modifié contiendra alors seulement le mot "world" où ""hello" reste existant dans le document mais qui sera invisible (mode invisible).

    J'apprécie beaucoup toutes vos réponses.


    mon code jusqu'à maintenant:

    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
    public class Test1 {
        private static Test1 tes; 
        private static final String src="...";
        private static PDPageContentStream content;
        private static PDType1Font font; 
        public static void CreatePdf(String src) throws IOException, COSVisitorException{
        PDRectangle rec= new PDRectangle(400,400);
        PDDocument document= null;
        document = new PDDocument();
        PDPage page = new PDPage(rec);
        document.addPage(page);
        PDDocumentInformation info=document.getDocumentInformation();
        info.setAuthor("PdfBox");
        info.setCreator("Pdf");
        info.setSubject("Stéganographie");
        info.setTitle("Stéganographie dans les documents PDF");
        info.setKeywords("Stéganographie, pdf");
        content= new PDPageContentStream(document, page);
        font= PDType1Font.HELVETICA;
        String texte="hello world";
        content.beginText();
        content.setFont(font, 12);
        content.moveTextPositionByAmount(15, 385);   
        // content.appendRawCommands("3 Tr");
        content.drawString(texte);
        content.endText();
        content.close();
        document.save("doc.pdf");
        document.close();       
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException, COSVisitorException {
            // TODO code application logic here   
               tes= new Test1();
               tes.CreatePdf(src);
               PDDocument doc ;
               doc = PDDocument.load("doc.pdf");
               List pages = doc.getDocumentCatalog().getAllPages();  
               for (int i = 0; i < pages.size(); i++)  {
                  PDPage page = (PDPage) pages.get(i);  
                  PDStream contents = page.getContents();  
                  PDFStreamParser parser = new PDFStreamParser(contents.getStream()); 
                  parser.parse();  
                  List tokens = parser.getTokens();  
                    for (int j = 0; j < tokens.size(); j++)  
                {  
                      Object next = tokens.get(j); 
                         if (next instanceof PDFOperator)  {
                           PDFOperator op = (PDFOperator) next;  
                        // Tj and TJ are the two operators that display strings in a PDF  
                                 if (op.getOperation().equals("Tj"))  
                        { 
                            // Tj takes one operator and that is the string  
                            // to display so lets update that operator 
                                   COSString previous = (COSString) tokens.get(j - 1);  
                                   String string = previous.getString();  
                                   System.out.println(string);
                                   //Word you want to change. Currently this code changes word "hello" to "hi123"
                                   string = string.replaceFirst("hello", "hi123"); 
                                   previous.reset();  
                                   previous.append(string.getBytes("ISO-8859-1"));                           
                        }
                     }      
                }
                    // now that the tokens are updated we will replace the page content stream.
                PDStream updatedStream = new PDStream(doc);  
                OutputStream out = updatedStream.createOutputStream();  
                ContentStreamWriter tokenWriter = new ContentStreamWriter(out);  
                tokenWriter.writeTokens(tokens);  
                page.setContents(updatedStream);
        }
          doc.save("a.pdf"); 
          doc.close();  
        }
    }
    Mille merci d'avance.

  2. #2
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Liban

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 16
    Points : 13
    Points
    13
    Par défaut
    J'ai résolu le problème

    Voici le code final:

    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
        public class Test1 {
        private static Test1 tes; 
        private static final String src="...";
        private static PDPageContentStream content;
        private static PDType1Font font; 
        public static void CreatePdf(String src) throws IOException, COSVisitorException{
        PDRectangle rec= new PDRectangle(400,400);
        PDDocument document= null;
        document = new PDDocument();
        PDPage page = new PDPage(rec);
        document.addPage(page);
        PDDocumentInformation info=document.getDocumentInformation();
        info.setAuthor("PdfBox");
        info.setCreator("Pdf");
        info.setSubject("Stéganographie");
        info.setTitle("Stéganographie dans les documents PDF");
        info.setKeywords("Stéganographie, pdf");
        content= new PDPageContentStream(document, page);
        font= PDType1Font.HELVETICA;
        String texte="hello world";
        content.beginText();
        content.setFont(font, 12);
        content.moveTextPositionByAmount(15, 385);   
        // content.appendRawCommands("3 Tr");
        content.drawString(texte);
        content.endText();
        content.close();
        document.save("doc.pdf");
        document.close();       
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException, COSVisitorException {
            // TODO code application logic here   
               tes= new Test1();
               tes.CreatePdf(src);
               PDDocument doc ;
               doc = PDDocument.load("doc.pdf");
               List pages = doc.getDocumentCatalog().getAllPages();  
               for (int i = 0; i < pages.size(); i++)  {
                  PDPage page = (PDPage) pages.get(i);  
                  PDStream contents = page.getContents();  
                  PDFStreamParser parser = new PDFStreamParser(contents.getStream()); 
                  parser.parse();  
                  List tokens = parser.getTokens();  
                    for (int j = 0; j < tokens.size(); j++)  
                {  
                      Object next = tokens.get(j); 
                         if (next instanceof PDFOperator)  {
                           PDFOperator op = (PDFOperator) next;  
                        // Tj and TJ are the two operators that display strings in a PDF  
                                 if (op.getOperation().equals("Tj"))  
                        { 
                            // Tj takes one operator and that is the string  
                            // to display so lets update that operator 
                                   //COSString previous = (COSString) tokens.get(j - 1);  
                                   //String string = previous.getString();  
                                   //System.out.println(string);
                                   //Word you want to change. Currently this code changes word "hello" to "hi123"
                                  // string = string.replaceFirst("hello", "hi123"); 
                                  // previous.reset();  
                                  // previous.append(string.getBytes("ISO-8859-1"));                    
                tokens.set(j-1, COSInteger.get(3));
                tokens.set(j, PDFOperator.getOperator("Tr"));
                tokens.add(++j, new COSString("hello"));
                tokens.add(++j, PDFOperator.getOperator("Tj"));
                tokens.add(++j, COSInteger.get(0));
                tokens.add(++j, PDFOperator.getOperator("Tr"));
                tokens.add(++j, new COSString(" world"));
                tokens.add(++j, PDFOperator.getOperator("Tj"));                
                        }
                     }      
                }
                    // now that the tokens are updated we will replace the page content stream.
                PDStream updatedStream = new PDStream(doc);  
                OutputStream out = updatedStream.createOutputStream();  
                ContentStreamWriter tokenWriter = new ContentStreamWriter(out);  
                tokenWriter.writeTokens(tokens);  
                page.setContents(updatedStream);
        }
          doc.save("a.pdf"); 
          doc.close();  
        }
    }
    Bien Cordialement,

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 10/08/2013, 13h47
  2. Recherche dans un document PDF à partir d'Access
    Par Bes74 dans le forum VBA Access
    Réponses: 1
    Dernier message: 21/01/2008, 17h37
  3. Fonction dans un document PDF
    Par acheo dans le forum VBA Access
    Réponses: 1
    Dernier message: 30/09/2007, 04h31

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