@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......");
}
}