Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:18:02+00:00 2026-06-16T15:18:02+00:00

I can insert simple text like this: document = new PDDocument(); page = new

  • 0

I can insert simple text like this:

document = new PDDocument();
page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page);
content.beginText();
content.moveTextPositionByAmount (10 , 10);
content.drawString ("test text");
content.endText();
content.close();

but how can I create a paragraph similar to HTML using the width attribute?

<p style="width:200px;">test text</p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:18:04+00:00Added an answer on June 16, 2026 at 3:18 pm

    Warning: this answer applies to and old version of PDFBox and relies on features that has since been deprecated. See the comments below for more details.

    According to this answer it’s not possible to insert line breaks into some text and have PDF display it correctly (whether using PDFBox or something else), so I believe auto-wrapping some text to fit in some width may also be something it can’t do automatically. (besides, there are many ways to wrap a text – whole words only, break them in smaller parts, etc)

    This answer to another question (about centering a string) gives some pointers on how to do this yourself. Assuming you wrote a function possibleWrapPoints(String):int[] to list all points in the text a word wrap can happen (excluding “zero”, including “text length”), one possible solution could be:

    PDFont font = PDType1Font.HELVETICA_BOLD; // Or whatever font you want.
    int fontSize = 16; // Or whatever font size you want.
    int paragraphWidth = 200;
    String text = "test text";
    
    int start = 0;
    int end = 0;
    int height = 10;
    for ( int i : possibleWrapPoints(text) ) {
        float width = font.getStringWidth(text.substring(start,i)) / 1000 * fontSize;
        if ( start < end && width > paragraphWidth ) {
            // Draw partial text and increase height
            content.moveTextPositionByAmount(10 , height);
            content.drawString(text.substring(start,end));
            height += font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
            start = end;
        }
        end = i;
    }
    // Last piece of text
    content.moveTextPositionByAmount(10 , height);
    content.drawString(text.substring(start));
    

    One example of possibleWrapPoints, that allow wrapping at any point that’s not part of a word (reference), could be:

    int[] possibleWrapPoints(String text) {
        String[] split = text.split("(?<=\\W)");
        int[] ret = new int[split.length];
        ret[0] = split[0].length();
        for ( int i = 1 ; i < split.length ; i++ )
            ret[i] = ret[i-1] + split[i].length();
        return ret;
    }
    

    Update: some additional info:

    • The PDF file format was designed to look the same in different situations, functionality like the one you requested makes sense in a PDF editor/creator, but not in the PDF file per se. For this reason, most “low level” tools tend to concentrate on dealing with the file format itself and leave away stuff like that.

    • Higher level tools OTOH usually have means to make this conversion. An example is Platypus (for Python, though), that do have easy ways of creating paragraphs, and relies on the lower level ReportLab functions to do the actual PDF rendering. I’m unaware of similar tools for PDFBox, but this post gives some hints on how to convert HTML content to PDF in a Java environment, using freely available tools. Haven’t tried them myself, but I’m posting here since it might be useful (in case my hand-made attempt above is not enough).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this is a rather simple question, but I can't seem to
I have a page full of text like this ( NOTE: the c. is
I store user editable articles in a database. Users can insert some simple widgets
EntityManager.merge() can insert new objects and update existing ones. Why would one want to
Using the code below I can insert a new row to my table (
I have text files with tables like this: Investment advisory and related fees receivable
I'm trying to make a simple application form were user can input data like
I want to insert TAB characters inside a TEXTAREA, like this: <textarea>{KEYPRESS-INSERTS-TAB-HERE}Hello World</textarea> I
I need to simple insert an equal symbol (=) onto every line of text
I would like to write a simple plain text editor including simple markdown instructions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.