0

I have to replace string between << and >>. However I'm unable to do so.

public void doIt( String inputFile, String outputFile) throws IOException, COSVisitorException
{

    PDDocument doc = null;
    try
    {
        doc = PDDocument.load( inputFile );
        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;
                    if( op.getOperation().equals( "Tj" ))

                    {
                        Scanner in = new Scanner(System.in);
                        COSString previous = (COSString)tokens.get( j-1 );
                        String string = previous.getString();
                        if(string.startsWith("<<") && string.endsWith(">>"))
                        {
                        System.out.println(string);
                        System.out.println("enter the word to be replaced");
                        String string2=in.nextLine();
                        string = string.replaceAll( string, string2 );
                        previous.reset();
                        previous.append( string.getBytes() );
                        }
                    }     
                    else if( op.getOperation().equals( "TJ" ))
                    {
                        COSArray previous = (COSArray)tokens.get( j-1 );
                        for( int k=0; k<previous.size(); k++ )
                        {
                            Scanner in = new Scanner(System.in);
                            Object arrElement = previous.getObject( k );
                            if(arrElement instanceof COSString)
                            {
                                COSString cosString = (COSString)arrElement;
                                String string = cosString.getString();
                                if(string.startsWith("<<") && string.endsWith(">>"))
                                {
                                    System.out.println(string);
                                    System.out.println("enter the word to be replaced");
                                    String string2=in.nextLine();
                                    string = string.replaceAll( string, string2 );
                                    cosString.reset();
                                    cosString.append( string.getBytes());
                                }
                            }
                        }
                    }
                }
            }
            PDStream updatedStream = new PDStream(doc);
            OutputStream out = updatedStream.createOutputStream();
            ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
            tokenWriter.writeTokens(tokens);
            page.setContents(updatedStream);
        }
        doc.save( outputFile );
        System.out.println("Done!! Now You can Open.");
    }
    finally
    {
        if( doc != null )
        {
            doc.close();
        }
    }
}
4

1 回答 1

3

请阅读我的书第 6 章的介绍。您假设 PDF 是一种用于编辑文本的格式。PDF 不是为文字处理而设计的。

当然:也许您在问如何创建静态表单,如我的书第 6.3.5 节中所述,但我怀疑 AcroForm 技术的静态特性能否满足您的需求。纯 XFA 表单(动态 PDF)可能会解决您的问题,但解释 XFA 并不是在 SO 的答案范围内可以完成的事情。XFA 规范长达数百页。正如 Duncan Jones 的评论中所指出的,您应该首先做一些初步工作。

于 2013-07-03T13:09:24.787 回答