Add Text to PDF in PHP SDK
Build your own PHP apps to add Text into PDF document files using server-side APIs.
Get StartedHow to add Text to PDF via Cloud PHP SDK
The following code snippet, by the Aspose.PDF, loads a PDF and custom font file, defines the item with the custom style (text color, background color, font, rotation, alignment, and spacing), and specifies the rectangle in which the paragraph should be placed on the page. This is useful for adding stylized text to a certain area of the PDF page. To add text into PDF, we’ll use Aspose.PDF Cloud PHP SDK This Cloud SDK assists PHP programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using PHP programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud PHP SDK.
Package Manager Console Command
composer install
Steps to add Text into PDF via Cloud PHP SDK
Aspose.PDF Cloud developers can easily load & add Text to PDF in just a few lines of code.
- Upload Necessary PDF Files.
- Define the Text Rectangle Area.
- Set Text Colors and Configure Text Properties.
- Create the Text Segment.
- Assemble the Text Line and define the Paragraph Properties.
- Add the Paragraph to the PDF.
- Save the Updated PDF.
Add Text to PDF using PHP
$name = 'sample.pdf';
$this->uploadFile($name);
$fontName = 'Righteous-Regular.ttf';
$this->uploadFile($fontName);
$pageNumber = 1;
$folder = $this->tempFolder;
$rectangle = new Aspose\PDF\Model\Rectangle();
$rectangle->setLlx(100);
$rectangle->setLly(100);
$rectangle->setUrx(200);
$rectangle->setUry(200);
$foregroundColor = new Aspose\PDF\Model\Color();
$foregroundColor->setA(0x00);
$foregroundColor->setR(0x00);
$foregroundColor->setG(0xFF);
$foregroundColor->setB(0x00);
$backgroundColor = new Aspose\PDF\Model\Color();
$backgroundColor->setA(0x00);
$backgroundColor->setR(0xFF);
$backgroundColor->setG(0x00);
$backgroundColor->setB(0x00);
$textState = new Aspose\PDF\Model\TextState();
$textState->setFont('Righteous');
$textState->setFontSize(10);
$textState->setForegroundColor($foregroundColor);
$textState->setBackgroundColor($backgroundColor);
$textState->setFontStyle(Aspose\PDF\Model\FontStyles::REGULAR);
$textState->setFontFile($this->tempFolder . '/' . $fontName);
$segment = new Aspose\PDF\Model\Segment();
$segment->setValue('segment 1');
$segment->setTextState($textState);
$textLine = new Aspose\PDF\Model\TextLine();
$textLine->setHorizontalAlignment(Aspose\PDF\Model\TextHorizontalAlignment::RIGHT);
$textLine->setSegments([$segment]);
$paragraph = new Aspose\PDF\Model\Paragraph();
$paragraph->setRectangle($rectangle);
$paragraph->setLeftMargin(10);
$paragraph->setRightMargin(10);
$paragraph->setTopMargin(20);
$paragraph->setBottomMargin(20);
$paragraph->setHorizontalAlignment(Aspose\PDF\Model\TextHorizontalAlignment::FULL_JUSTIFY);
$paragraph->setLineSpacing(Aspose\PDF\Model\LineSpacing::FONT_SIZE);
$paragraph->setRotation(10);
$paragraph->setSubsequentLinesIndent(20);
$paragraph->setVerticalAlignment(Aspose\PDF\Model\VerticalAlignment::CENTER);
$paragraph->setWrapMode(Aspose\PDF\Model\WrapMode::BY_WORDS);
$paragraph->setLines([$textLine]);
Work with Text in PDF
Text can be dynamically added to customize a PDF for a specific user, such as adding their name or personalized message. This is often used in marketing materials or for personalized reports. Adding text in specific fonts, colors, and alignments can make certain sections more readable and visually appealing, creating a better user experience. Text additions can help maintain consistent information across different PDF versions, avoiding manual edits and ensuring that key information remains in place. Add the Text into PDF documents with Aspose.PDF Cloud PHP SDK.
With our PHP library you can:
- Add PDF document’s header & footer in text or image format.
- Add tables & stamps (text or image) to PDF documents.
- Append multiple PDF documents to an existing file.
- Work with PDF attachments, annotations, & form fields.
- Apply encryption or decryption to PDF documents & set a password.
- Delete all stamps & tables from a page or entire PDF document.
- Delete a specific stamp or table from the PDF document by its ID.
- Replace single or multiple instances of text on a PDF page or from the entire document.
- Extensive support for converting PDF documents to various other file formats.
- Extract various elements of PDF files & make PDF documents optimized.
- You can try out our free App to add the text into PDF files online and test the functionality.
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.PDF Cloud for PHP?
- Customers List
- Security