Substituir texto no PDF com Java

O Aspose.PDF for Java fornece recursos de substituição simples e substituição sensível ao layout através TextFragmentAbsorber e substituir opções.

Substituir texto em todas as páginas

Use este exemplo quando a mesma frase deve ser substituída em todo o documento.

  1. Abra o documento PDF de origem.
  2. Pesquise todas as páginas pela frase alvo com TextFragmentAbsorber.
  3. Substitua o texto correspondente e salve o PDF atualizado.
public static void replaceTextOnAllPages(Path inputFile, Path outputFile) {
        String searchPhrase = "PDF";
        String replacePhrase = "pdf";

        try (Document document = new Document(inputFile.toString())) {
            TextFragmentAbsorber absorber = new TextFragmentAbsorber(searchPhrase);
            document.getPages().accept(absorber);

            for (TextFragment fragment : absorber.getTextFragments()) {
                fragment.setText(replacePhrase);
            }

            document.save(outputFile.toString());
        }
    }

Substituir texto em uma região específica da página

Use este exemplo quando a substituição deve ser limitada a um retângulo selecionado em uma página.

  1. Abra o documento PDF de origem.
  2. Configure TextSearchOptions com limites de página e um retângulo alvo.
  3. Substitua o texto correspondente dentro dessa região e salve o documento.
public static void replaceTextInParticularPageRegion(Path inputFile, Path outputFile) {
    String searchPhrase = "doc";
    String replacePhrase = "DOC";

    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber(searchPhrase);
        absorber.getTextSearchOptions().setLimitToPageBounds(true);
        absorber.getTextSearchOptions().setRectangle(new Rectangle(300, 442, 500, 742, true));
        document.getPages().get_Item(1).accept(absorber);

        for (TextFragment fragment : absorber.getTextFragments()) {
            fragment.setText(replacePhrase);
        }

        document.save(outputFile.toString());
    }
}

Substituir texto e ajustar o espaçamento dentro de um retângulo deslocado

Use este exemplo quando o texto de substituição deve permanecer na página com espaçamento ajustado, mas o tamanho da fonte deve permanecer inalterado.

  1. Abra o PDF de origem e colete fragmentos de texto da página de destino.
  2. Modifique o retângulo de substituição e escolha AdjustSpaceWidth comportamento.
  3. Defina o novo texto e salve o documento.
public static void replaceTextAndResizeAndShiftWithoutChangingFontSize(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.visit(document.getPages().get_Item(1));

        TextFragment fragment = absorber.getTextFragments().get_Item(1);
        String text = fragment.getText();
        Rectangle rectangle = fragment.getRectangle();
        rectangle.setLLX(rectangle.getLLX() + 50);
        rectangle.setURX(rectangle.getURX() - 50);
        fragment.getReplaceOptions().setRectangle(rectangle);
        fragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth);
        fragment.setText(text + " " + text);

        document.save(outputFile.toString());
    }
}

Substituir o texto dentro de um retângulo de parágrafo maior

Use este exemplo quando o texto de substituição deve expandir para uma área maior da página.

  1. Abra o PDF de origem e obtenha o primeiro fragmento de texto da página de destino.
  2. Crie um retângulo de substituição maior a partir da caixa de mídia da página.
  3. Aplique as opções de substituição e salve o PDF.
public static void replaceTextAndResizeAndShiftParagraph(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.visit(document.getPages().get_Item(1));

        TextFragment fragment = absorber.getTextFragments().get_Item(1);
        String text = fragment.getText();
        Rectangle rectangle = document.getPages().get_Item(1).getMediaBox();
        rectangle.setLLX(rectangle.getLLX() + 20);
        rectangle.setURX(rectangle.getURX() - 20);
        rectangle.setURY(rectangle.getURY() - 20);
        fragment.getReplaceOptions().setRectangle(rectangle);
        fragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth);
        fragment.setText(text + " " + text);

        document.save(outputFile.toString());
    }
}

Substituir texto e escalar a fonte para preencher o retângulo

Use este exemplo quando o texto de substituição deve ser ampliado para preencher uma área-alvo.

  1. Abra o PDF de origem e acesse o fragmento de texto de destino.
  2. Defina um retângulo de substituição e habilite ScaleToFill ajuste de fonte.
  3. Defina o novo texto e salve o documento atualizado.
public static void replaceTextAndResizeAndExpandFont(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.visit(document.getPages().get_Item(1));

        TextFragment fragment = absorber.getTextFragments().get_Item(1);
        String text = fragment.getText();
        fragment.getReplaceOptions().setRectangle(new Rectangle(100, 300, 512, 692, true));
        fragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth);
        fragment.getReplaceOptions().setFontSizeAdjustmentAction(TextReplaceOptions.FontSizeAdjustment.ScaleToFill);
        fragment.setText(text + " " + text);

        document.save(outputFile.toString());
    }
}

Substituir texto e reduzir para ajustar

Use este exemplo quando o texto de substituição precisar permanecer dentro do retângulo de texto original.

  1. Abra o PDF de origem e selecione o fragmento de destino.
  2. Reutilizar o retângulo de fragmento atual e ativar ShrinkToFit.
  3. Substitua o texto e salve o documento.
public static void replaceTextAndFitTextIntoRectangle(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.visit(document.getPages().get_Item(1));

        TextFragment fragment = absorber.getTextFragments().get_Item(1);
        String text = fragment.getText();
        fragment.getReplaceOptions().setRectangle(fragment.getRectangle());
        fragment.getReplaceOptions().setFontSizeAdjustmentAction(TextReplaceOptions.FontSizeAdjustment.ShrinkToFit);
        fragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth);
        fragment.setText(text + " " + text);

        document.save(outputFile.toString());
    }
}

Substituir texto por expressão regular

Use este exemplo quando o texto correspondido deve ser encontrado por um padrão regex e reestilizado durante a substituição.

  1. Abra o documento PDF de origem.
  2. Pesquise a página com suporte a regex TextFragmentAbsorber.
  3. Substitua cada correspondência, atualize seu estilo de texto e salve o resultado.
public static void replaceTextBasedOnRegex(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber(Pattern.compile("\\d{4}-\\d{4}"));
        absorber.setTextSearchOptions(new TextSearchOptions(true));
        document.getPages().get_Item(1).accept(absorber);

        for (TextFragment fragment : absorber.getTextFragments()) {
            fragment.setText("ABC1-2XZY");
            fragment.getTextState().setFont(FontRepository.findFont("Verdana"));
            fragment.getTextState().setFontSize(12);
            fragment.getTextState().setForegroundColor(Color.getBlue());
            fragment.getTextState().setBackgroundColor(Color.getLightGreen());
        }

        document.save(outputFile.toString());
    }
}

Substituir o texto de espaço reservado e deixe a página reorganizar

Use este exemplo quando um placeholder precisar ser substituído por um valor real mais longo, preservando o layout da página.

  1. Abra o PDF de origem e procure o texto de espaço reservado.
  2. Atribua o texto de substituição e atualize suas configurações de fonte.
  3. Salve o documento para que o layout seja recalculado.
public static void automaticallyRearrangePageContents(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber("[Long_placeholder_Long_placeholder]");
        document.getPages().accept(absorber);

        for (TextFragment textFragment : absorber.getTextFragments()) {
            textFragment.setText("John Smith, South Development Studio");
            textFragment.getTextState().setFont(FontRepository.findFont("Calibri"));
            textFragment.getTextState().setFontSize(12);
            textFragment.getTextState().setForegroundColor(Color.getNavy());
        }

        document.save(outputFile.toString());
    }
}

Substituir uma fonte por outra

Use este exemplo quando o texto que utiliza uma fonte incorporada específica deve ser trocado por outra fonte.

  1. Abra o PDF de origem e colete todos os fragmentos de texto.
  2. Verifique o nome da fonte de cada fragmento e substitua a fonte de destino.
  3. Salve o PDF atualizado.
public static void replaceFonts(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        document.getPages().accept(absorber);

        for (TextFragment fragment : absorber.getTextFragments()) {
            if ("Arial-BoldMT".equals(fragment.getTextState().getFont().getFontName())) {
                fragment.getTextState().setFont(FontRepository.findFont("Verdana"));
            }
        }

        document.save(outputFile.toString());
    }
}

Substituir fontes e remover recursos de fontes não utilizados

Use este exemplo quando o documento precisar ser limpo após a substituição da fonte.

  1. Abra o PDF de origem e configure TextEditOptions para remover fontes não utilizadas.
  2. Absorva os fragmentos de texto e atribua a fonte de substituição.
  3. Salve o documento otimizado.
public static void removeUnusedFonts(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextEditOptions options = new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts);
        TextFragmentAbsorber absorber = new TextFragmentAbsorber(options);
        document.getPages().accept(absorber);

        for (TextFragment textFragment : absorber.getTextFragments()) {
            textFragment.getTextState().setFont(FontRepository.findFont("TimesNewRoman"));
        }

        document.save(outputFile.toString());
    }
}

Remover todo o texto do documento

Use este exemplo quando todo o conteúdo de texto deve ser excluído de cada página.

  1. Abra o documento PDF de origem.
  2. Crie um TextFragmentAbsorber e chame removeAllText(document).
  3. Salve o PDF limpo.
public static void removeAllTextUsingAbsorber1(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.removeAllText(document);
        document.save(outputFile.toString());
    }
}

Remover todo o texto de uma página

Use este exemplo quando todo o texto deve ser removido apenas de uma página específica.

  1. Abra o documento PDF de origem.
  2. Crie um TextFragmentAbsorber e remova texto da página de destino.
  3. Salve o documento atualizado.
public static void removeAllTextUsingAbsorber2(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.removeAllText(document.getPages().get_Item(1));
        document.save(outputFile.toString());
    }
}

Remover texto de um retângulo selecionado

Use este exemplo quando o texto deve ser excluído somente dentro de uma área de página escolhida.

  1. Abra o documento PDF de origem.
  2. Crie um TextFragmentAbsorber e defina o retângulo a ser limpo.
  3. Remova o texto daquela região e salve o documento.
public static void removeAllTextUsingAbsorber3(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        absorber.removeAllText(document.getPages().get_Item(1), new Rectangle(10, 200, 120, 600, true));
        document.save(outputFile.toString());
    }
}

Remover texto oculto

Use este exemplo quando fragmentos de texto invisíveis devem ser removidos do PDF.

  1. Abra o PDF de origem e absorva todos os fragmentos de texto.
  2. Verifique cada fragmento quanto ao estado de texto invisível.
  3. Limpe o texto oculto e salve o documento.
public static void removeHiddenText(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber textAbsorber = new TextFragmentAbsorber();
        textAbsorber.setTextReplaceOptions(new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.None));
        document.getPages().accept(textAbsorber);

        for (TextFragment fragment : textAbsorber.getTextFragments()) {
            if (fragment.getTextState().isInvisible()) {
                fragment.setText("");
            }
        }

        document.save(outputFile.toString());
    }
}