【问题标题】:Make some characters as invisible from an existing PDF document by using PDFBox使用 PDFBox 使某些字符在现有 PDF 文档中不可见
【发布时间】:2018-02-08 01:41:11
【问题描述】:

我尝试使用 Apache PDFBox. 从现有 PDF 中读取“Tj”运算符 当我完全完成这项任务时,我尝试替换一些字符。例如,让我们考虑一个包含 (hello world)Tj 的 pdf 文档,因此这段代码将“hello”替换为“hi123”。 因此修改后的文档变为包含 (hi123 world)Tj 而不是 (hello world)Tj。

我现在最大的问题是如何编辑此代码以使“hello”成为文本呈现模式 3(换句话说:文本呈现模式“不可见”)。所以我不想用“hi123”替换“hello”,而是让“hello”消失(模式不可见)。因此,修改后的文档只包含“世界”,而“hello”变得不可见。

到目前为止我的代码:

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();       
    }

    public static void main(String[] args) throws IOException, COSVisitorException {
        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();  
    }
}

【问题讨论】:

    标签: java pdf pdfbox


    【解决方案1】:

    本质上,您必须通过将两个标记 (hello world) Tj 替换为八个标记 3 Tr (hello) Tj 0 Tr ( world) Tj 来更改 tokens 列表

    因此,替换你的循环

    List tokens = parser.getTokens();  
    for (int j = 0; j < tokens.size(); j++)  
    {
        [...]
    }
    

    类似

    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"))  
            { 
                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"));
            }
        }      
    }
    

    【讨论】:

    • 我不知道该怎么感谢你!!!! :) 我测试了你的代码,我的问题完全解决了。再次感谢你!!!李斯特。
    【解决方案2】:

    最终代码:

        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();  
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多