Trabaja con texto en PDF a través de Python

Cómo trabajar con texto en PDF mediante la biblioteca Python for .NET

Cómo trabajar con texto en PDF mediante la biblioteca Python for .NET

Para añadir texto a un archivo PDF, utilizaremos la API Aspose.PDF para Python, que es una API de manipulación de documentos para .NET rica en funciones, potente y fácil de usar. Abra el administrador de paquetes NuGet, busque Aspose.pdf e instálelo. También puede usar el siguiente comando desde la consola del administrador de paquetes.

Python Package Manager Console

pip install aspose-pdf

Agregue texto a un archivo PDF a través de Python


Para probar el código de su entorno, necesita Aspose.PDF para Python.

  1. Cargue el PDF con una instancia de Document.
  2. Cree un TextParagraph y defina sus propiedades.
  3. Agregue el párrafo de texto a la página con TextBuilder.
  4. Vuelva a guardar el archivo.

Añadir texto 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);