Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用PDFBox替换PDF中居中的文本_Java_Pdf_Pdfbox - Fatal编程技术网

Java 如何用PDFBox替换PDF中居中的文本

Java 如何用PDFBox替换PDF中居中的文本,java,pdf,pdfbox,Java,Pdf,Pdfbox,我使用了PDFTextReplacement示例。 如果我的文本左对齐,它会按预期进行替换。 但是,如果我的输入pdf以文本为中心,它会将文本替换为左对齐。 好的,我必须重新计算正确的起点 因此,我有两个目标或问题: 如何确定路线 如何计算正确的起点 这是我的密码: public PDDocument doIt(String inputFile, Map<String, String> text) throws IOException, COSVisitorExc

我使用了
PDFTextReplacement
示例。 如果我的文本左对齐,它会按预期进行替换。 但是,如果我的输入pdf以文本为中心,它会将文本替换为左对齐。 好的,我必须重新计算正确的起点

因此,我有两个目标或问题:

  • 如何确定路线
  • 如何计算正确的起点
这是我的密码:

public PDDocument doIt(String inputFile, Map<String, String> text)
        throws IOException, COSVisitorException {
    // the document
    PDDocument doc = null;

    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;

                // Tj and TJ are the two operators that display
                // strings in a PDF

                String pstring = "";
                int prej = 0;
                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(j + " " + string);
                    if (j == prej) {
                        pstring += string;
                    } else {
                        prej = j;
                        pstring = string;
                    }

                    previous.reset();
                    previous.append(string.getBytes("ISO-8859-1"));
                } else if (op.getOperation().equals("TJ")) {
                    COSArray previous = (COSArray) tokens.get(j - 1);
                    for (int k = 0; k < previous.size(); k++) {
                        Object arrElement = previous.getObject(k);
                        if (arrElement instanceof COSString) {
                            COSString cosString = (COSString) arrElement;
                            String string = cosString.getString();

                            if (j == prej) {
                                pstring += string;
                            } else {
                                prej = j;
                                pstring = string;
                            }

                            cosString.reset();
                            // cosString.append(string
                            // .getBytes("ISO-8859-1"));
                        }

                    }

                    COSString cosString2 = (COSString) previous
                            .getObject(0);

                    for (int t = 1; t < previous.size(); t++)
                        previous.remove(t);

                    // cosString2.setNeedToBeUpdate(true);

                    if (text.containsKey(pstring.trim())) {

                        String textValue = text.get(pstring.trim());
                        cosString2.append(textValue.getBytes("ISO-8859-1"));

                        for (int k = 1; k < previous.size(); k++) {
                            previous.remove(k);

                        }
                    }

                }
            }
        }
        // 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);
    }
    return doc;
}
公共PDDocument doIt(字符串输入文件,映射文本) 抛出IOException,COSVisitorException{ //文件 PDDocument=null; doc=PDDocument.load(输入文件); 列表页面=doc.getDocumentCatalog().getAllPages(); 对于(int i=0;i您可以使用此功能:

 public void doIt( String inputFile, String outputFile, String strToFind, String message)
            throws IOException, COSVisitorException
        {
            // the document
            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;
                            //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();
                                string = string.replaceFirst( strToFind, message );
                                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++ )
                                {
                                    Object arrElement = previous.getObject( k );
                                    if( arrElement instanceof COSString )
                                    {
                                        COSString cosString = (COSString)arrElement;
                                        String string = cosString.getString();
                                        string = string.replaceFirst( strToFind, message );
                                        cosString.reset();
                                        cosString.append( string.getBytes() );
                                    }
                                }
                            }
                        }
                    }
                    //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( outputFile );
            }
            finally
            {
                if( doc != null )
                {
                    doc.close();
                }
            }
        }
public void doIt(字符串输入文件、字符串输出文件、字符串strofind、字符串消息)
抛出IOException,COSVisitorException
{
//文件
PDDocument=null;
尝试
{
doc=PDDocument.load(输入文件);
列表页面=doc.getDocumentCatalog().getAllPages();

对于(int i=0;i如何确定对齐方式-PDF不知道对齐方式。它从当前原点开始绘制文本,仅此而已。您可以尝试通过比较当前“行”的文本位置与页面尺寸以及(“行”)前后“行”上的文本位置来确定对齐方式因为PDF不一定遵循文本行概念)。但是,如果某些文本看起来居中,您确定它是要居中吗?它也可能只是缩进了一段距离,碰巧现在看起来居中了。@mkl是的,这正是我在PDDocument中看到的。因此我必须完善我的问题。1.如何获得内容使用的确切空间(icepdf使用lineText.getBounds())?2.如何计算新字符串的已用空间(基于BASE14字体))您的代码在非常低的级别上工作,它检查页面内容流中的单个指令。因此,它不能从更高级别的功能中受益。这尤其意味着在该级别上,您必须自己跟踪当前图形状态的更改。要做到这一点,您应该学习第一部分,特别是章节8(了解图形状态如何变化)和9(了解文本如何绘制)。不过,大部分编码已经在PDFBox中完成。
PDFStreamEngine
浏览页面内容流中的说明,并使用文本块及其位置和维度调用
processTextPosition