Come trovare e sostituire il testo in PDF usando C#

Questa guida rapida descrive come trovare e sostituire il testo in PDF utilizzando C# con l’aiuto di passaggi dettagliati e un codice eseguibile. Aiuta a configurare l’ambiente e quindi fornisce un processo passo-passo per sostituire il testo in un PDF utilizzando C#. Una volta aggiornato il file, puoi salvarlo nuovamente sul disco nel formato originale, ad esempio PDF o come DOCX, Excel, HTML, ecc. solo per citarne alcuni.

Passaggi per trovare e sostituire il testo in PDF utilizzando C#

  1. Configura il progetto per l’utilizzo di Aspose.PDF for .NET utilizzando il gestore di pacchetti NuGet
  2. Crea o carica un file PDF contenente un testo di esempio utilizzando l’oggetto classe Document
  3. Utilizzando l’oggetto classe TextFragmentAbsorber, imposta il testo da cercare
  4. Per tutte le pagine nel file PDF di input, accetta l’assorbitore di testo
  5. Ottieni la raccolta di frammenti in cui il testo viene estratto dal file PDF caricato
  6. Analizza tutti i frammenti e imposta un nuovo testo
  7. Salva il file PDF aggiornato

Questi passaggi descrivono come in un PDF cercare e sostituire il testo utilizzando C#. Viene creato un nuovo file con del testo di esempio, tuttavia è possibile caricare un file PDF esistente il cui testo deve essere sostituito. C’è una varietà di opzioni disponibili per cercare il testo nel PDF come ignorare il testo ombra, limitare la ricerca alla pagina rilegata, ecc.

Codice per sostituire il testo in un PDF utilizzando C#

using Aspose.Pdf;
using Aspose.Pdf.Text;
namespace FindAndReplaceTextInPdfUsingCSharp
{
class Program
{
static void Main(string[] args) // Main function to create 7z archive in CSharp
{
// Instantiate a license to avoid watermark in output PDF
Aspose.Pdf.License licForPdf= new Aspose.Pdf.License();
licForPdf.SetLicense("Aspose.Pdf.lic");
// Create an empty PDF document
Document newPDFFile = new Document();
// Add an empty page in the newly created PDF
Page page = newPDFFile.Pages.Add();
// Add sample text in the PDF file
for(int iTxtCounter = 0 ; iTxtCounter < 15; iTxtCounter++)
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment($"my_data\nanother data"));
// Save the newly created PDF file containing the test data in it
newPDFFile.Save("InputPDFToReplaceText.pdf");
// Open PDF document to replace text in it
Document inputPDFFile = new Document("InputPDFToReplaceText.pdf");
// Set the text that is to be searched in the TextAbsorber object
TextFragmentAbsorber txtAbsorber = new TextFragmentAbsorber("my_data");
// Apply the text absorber for all the pages in the input PDF file
inputPDFFile.Pages.Accept(txtAbsorber);
// Get the collection of fragments containing extracted text from the PDF
TextFragmentCollection textFragmentCollection = txtAbsorber.TextFragments;
// Parse all the fragments and replace text using particular font, size and foreground/background color
foreach (TextFragment txtFragment in textFragmentCollection)
txtFragment.Text = "MY_DATA";
// Save resulting PDF document.
inputPDFFile.Save("OutputPDFAfterReplacingText.pdf");
System.Console.WriteLine("Done");
}
}
}

Questo codice usa TextFragmentAbsorber e TextFragment per trovare e sostituire il testo in PDF usando C#. Non solo puoi sostituire il testo, ma anche cambiarne la famiglia di caratteri, le dimensioni, il colore di primo piano e il colore di sfondo nel file PDF risultante. Sono inoltre disponibili opzioni per sostituire il testo nell’intero PDF in una volta o per sostituire il testo in base all’espressione regolare.

In questo argomento abbiamo imparato a trovare e sostituire il testo in PDF, tuttavia, se vuoi imparare a dividere i file PDF per pagine, fai riferimento all’articolo su come dividere il file PDF per pagine in C#.

 Italiano