Lavora con testo in PDF tramite Python

Come lavorare con il testo in un PDF utilizzando la libreria Python for .NET

Come lavorare con il testo in un PDF utilizzando la libreria Python for .NET

Per aggiungere testo al file PDF, useremo l’API Aspose.PDF for Python, un’API di manipolazione di documenti ricca di funzionalità, potente e facile da usare per .NET. Apri il gestore di pacchetti NuGet, cerca Aspose.pdf e installa. È inoltre possibile utilizzare il seguente comando dalla console di Package Manager.

Python Package Manager Console

pip install aspose-pdf

Aggiungi testo al file PDF tramite Python


Per provare il codice nel tuo ambiente, hai bisogno di Aspose.PDF per Python.

  1. Carica il PDF con un’istanza di Document.
  2. Crea un TextParagraph e definisci le sue proprietà.
  3. Aggiungi il TextParagraph alla pagina usando TextBuilder.
  4. Salvate nuovamente il file.

Aggiungi testo al PDF - Python

<% text.code-block.subtitle %>


    // Open document
    Document pdfDocument = new Document(dataDir + "input.pdf");
    // Get particular page
    Page pdfPage = (Page)pdfDocument.Pages[1];
    // Create text fragment
    TextFragment textFragment = new TextFragment("main text");
    textFragment.Position = new Position(100, 600);
    // Set text properties
    textFragment.TextState.FontSize = 12;
    textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
    textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
    textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
    // Create TextBuilder object
    TextBuilder textBuilder = new TextBuilder(pdfPage);
    // Append the text fragment to the PDF page
    textBuilder.AppendText(textFragment);
    dataDir = dataDir + "AddText_out.pdf";
    // Save resulting PDF document.
    pdfDocument.Save(dataDir);