Aggiungi testo al PDF tramite PHP

Aggiungi testo al documento PDF con PHP via Java. Usa Aspose.PDF per modificare i documenti PDF a livello di codice

Come lavorare con il testo in un PDF utilizzando la libreria PHP via Java

Per aggiungere testo in un file PDF, utilizzeremo Aspose.PDF per PHP tramite Java API, uno strumento di manipolazione dei documenti ricco di funzionalità, potente e facile da usare in php-java. Installa la versione Tomcat 9.0 in qualsiasi posizione, aggiungi Aspose.pdf.war, per maggiori dettagli consulta la pagina GitHub.

Aggiungi testo al file PDF tramite PHP


È necessario Aspose.PDF for PHP via Java per provare il codice nel proprio ambiente.

  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 - PHP

Questo codice di esempio mostra come aggiungere testo in un documento PDF - PHP


    // Open document
    $document = new Document($inputFile);
    
    // get particular page
    $page = $document->getPages()->add();
    
    // create text fragment
    $textFragment = new TextFragment("Aspose.PDF");
    $textFragment->setPosition(new Position(80, 700));

    // // set text properties
    $fontRepository = new FontRepository();
    
    $colors = new Color();
    $textFragment->getTextState()->setFont($fontRepository->findFont("Verdana"));
    $textFragment->getTextState()->setFontSize(14);
    $textFragment->getTextState()->setForegroundColor($colors->getBlue());
    $textFragment->getTextState()->setBackgroundColor($colors->getLightGray());

    // create TextBuilder object
    $textBuilder = new TextBuilder($page);
    // append the text fragment to the PDF page
    $textBuilder->appendText($textFragment);

    // Save resulting PDF document.    
    $document->save($outputFile);
    $document->close();