PPTX DOCX XLSX PDF ODP
Aspose.PDF  for Java
PDF

Work with Text in PDF via Java

How to Work with Text in PDF Using Java Library

How to Work with Text in PDF Using Java Library

To add Text into PDF File Aspose.PDF for Java API, which is a feature-rich, powerful, and easy-to-use conversion API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
    </repository>
    

 

Dependency

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>version of aspose-pdf API</version>
    <classifier>jdk17</classifier>
    </dependency>
    

 

Add Text to PDF File via Java

You need Aspose.PDF for Java to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create a TextParagraph and define its properties.
  3. Add the TextParagraph to Page using TextBuilder.
  4. Save the file again.

System Requirements

Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with Java Runtime Environment for JSP/JSF Application and Desktop Applications.
  • Development environment like Eclipse or IntelliJ IDEA.
  • Aspose.PDF for Java library referenced in your project.
 

Add Text to PDF - Java.


    public static void AddingText() {
    // Load the PDF file
    Document document = new Document(_dataDir + "sample.pdf");

    // get particular page
    Page pdfPage = document.getPages().get_Item(1);
    // create text fragment
    TextFragment textFragment = new TextFragment("Aspose.PDF");
    textFragment.setPosition(new Position(80, 700));

    // set text properties
    textFragment.getTextState().setFont(FontRepository.findFont("Verdana"));
    textFragment.getTextState().setFontSize(14);
    textFragment.getTextState().setForegroundColor(Color.getBlue());
    textFragment.getTextState().setBackgroundColor(Color.getLightGray());

    // create TextBuilder object
    TextBuilder textBuilder = new TextBuilder(pdfPage);
    // append the text fragment to the PDF page
    textBuilder.appendText(textFragment);

    // Save resulting PDF document.
 

About Aspose.PDF for Java API

Aspose.PDF API can be used for PDF document manipulation and parsing within applications. One can create, modify, compress, secure, print or save PDF to TXT, HTML, PCL, XFA, XML, XPS, EPUB, TEX, Images and more formats. Aspose.PDF is a standalone API and it does not depend on any software including Adobe Acrobat.

Online PDF Parser Live Demos

Extract text and images from PDF documents right now by visiting our Live Demos website. The live demo has the following benefits

  No need to download or setup anything
  No need to write any code
  Just upload your PDF file & edit document properties
  It will be parsed instantly