How to Add a Text Paragraph in PDF with JAVA REST API

Hi,
I need guidance with regards to adding a paragraph in my PDF. So far i am doing something like this:

    Page page = doc.getPages().getList().get(1);
    Paragraph paragraph = new Paragraph();
    
    Rectangle rect = new Rectangle();
   
    rect.setLLX(250.0);
    rect.setLLY(718.0);
    rect.setURX(560.0);
    rect.setURY(770.0);

    paragraph.setSubsequentLinesIndent(0.0);
    paragraph.setRectangle(rect);
    paragraph.setHorizontalAlignment(TextHorizontalAlignment.LEFT);
    paragraph.setVerticalAlignment(VerticalAlignment.BOTTOM);
    paragraph.setWrapMode(WrapMode.BYWORDS);

   
    TextState textState = new TextState();
    textState.setFontSize(16.0);
   
    Segment segment = new Segment();
    segment.setValue(desc);
    segment.setTextState(textState);

    List<TextLine> lines = new ArrayList<>();
    TextLine line = new TextLine();
    line.addSegmentsItem(segment);
    lines.add(line);
    paragraph.setLines(lines);

For Aspose.pdf (not cloud based API) we used to add a textbuilder and then append the paragraph to the textbuilder. However, i can’t find the same functionality on pdf-cloud. Kindly help how i can achieve to make a paragraph

@ahmedtariq

Please use PutAddText API method to add a text paragraph in PDF with Java using Aspose.PDF Cloud. Kindly check the following sample code for reference.

PUT ​/pdf​/{name}​/pages​/{pageNumber}​/text Add text to PDF document page.

Add a Text Paragraph in PDF with Java

import com.aspose.asposecloudpdf.ApiClient;
import com.aspose.asposecloudpdf.ApiException;
import com.aspose.asposecloudpdf.api.PdfApi;
import com.aspose.asposecloudpdf.model.*;

import java.io.IOException;
import java.util.ArrayList;

public class AddTextParagraphPDFJavaExample {
    public static void main(String[] args) throws ApiException, IOException {
        //TODO: Get your ClientId and ClientSecret at https://dashboard.aspose.cloud (free registration is required)
        ApiClient apiClient = new ApiClient();
        apiClient.setAppKey("xxxxxxxxxxxxxxxxxxxxxxxxx");
        apiClient.setAppSid("xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx");

        // Initialize API
        PdfApi pdfApi = new PdfApi(apiClient);
        String name = "4pages.pdf";
        String fontFile = "NotoSans-Bold.ttf";
        String fontFolder="Fonts";
        int pageNumber = 1;

        Rectangle rectangle = new Rectangle();
        rectangle.setLLX(100.);
        rectangle.setLLY(100.);
        rectangle.setURX(200.);
        rectangle.setURY(200.);

        Color foregroundColor = new Color();
        foregroundColor.setA(0x00);
        foregroundColor.setR(0x00);
        foregroundColor.setG(0xFF);
        foregroundColor.setB(0x00);

        Color backgroundColor = new Color();
        backgroundColor.setA(0x00);
        backgroundColor.setR(0xFF);
        backgroundColor.setG(0x00);
        backgroundColor.setB(0x00);

        TextState textState = new TextState();
        textState.setFont("NotoSans");
        textState.setFontSize(10.);
        textState.setForegroundColor(foregroundColor);
        textState.setBackgroundColor(backgroundColor);
        textState.setFontStyle(FontStyles.REGULAR);
        textState.setFontFile(fontFolder + '/' + fontFile);

        final Segment segment = new Segment();
        segment.setValue("segment 1");
        segment.setTextState(textState);

        final TextLine textLine = new TextLine();
        textLine.setHorizontalAlignment(TextHorizontalAlignment.RIGHT);
        textLine.setSegments(new ArrayList<Segment>(){{ add(segment);}});

        Paragraph paragraph = new Paragraph();
        paragraph.setRectangle(rectangle);
        paragraph.setLeftMargin(10.);
        paragraph.setRightMargin(10.);
        paragraph.setTopMargin(20.);
        paragraph.setBottomMargin(20.);
        paragraph.setHorizontalAlignment(TextHorizontalAlignment.FULLJUSTIFY);
        paragraph.setLineSpacing(LineSpacing.FONTSIZE);
        paragraph.setRotation(10.);
        paragraph.setSubsequentLinesIndent(20.);
        paragraph.setVerticalAlignment(VerticalAlignment.CENTER);
        paragraph.setWrapMode(WrapMode.BYWORDS);
        paragraph.setLines(new ArrayList<TextLine>(){{ add(textLine);}});


        AsposeResponse response = pdfApi.putAddText(name, pageNumber, paragraph, null, null);
        System.out.println("completed......");
    }
}

A post was split to a new topic: How to underline text in PDF Stamp using Java