How can I limit the number of characters per line?

Asked 1 years ago, Updated 1 years ago, 95 views

I am using iTunes to create PDFs.In those PDFs, you must add two String to a line.
The first String (string1) is between 1 and 10.The length of the second String (string2) is unknown, but I would like to make sure that the length of string1 and string2 do not exceed 10 characters.
How do I add such a string to an underlined line?

java pdf

2022-09-30 21:42

1 Answers

I would like to list the UnderlineParagramWithTwoParts sample code and explain what kind of problem this sample code solved.

First problem: I want to match one line to 100 characters.First, use 101 characters as the basis, assuming that there is a space between string1 and string2(Otherwise, the sample code should be easily modified)

I chose a font with the same width for all glyphs because I did not know what was between string1 and string2.Use Courier (fixed width or equal width font).If you want to use a proposal font (e.g., Arial), you have to calculate the font size separately for each combination of string1 and string2, which makes it very difficult to calculate the font size and makes each column look different.

This code calculates the font size based on the single character width of the COURIER font.Try adding 101 characters to a line that is the same width as the available space between the left and right margins of the page.

PdfFontfont=PdfFontFactory.createFont(FontConstants.COURIER, PdfEncodings.WINANSI, false);
float charWidth = font.getWidth("";
int charactersPerLine=101;
floatpageWidth=pdfDoc.getPage(1) .getPageSize().getWidth() -doc.getLeftMargin() -doc.getRightMargin();
float fontSize=(1000*(pageWidth/(charWidth*charactersPerLine)));
fontSize=(int)(fontSize*100))/100f;

For your information, we will round off the float value.Otherwise, the rounding error inherent in using the float value may cause problems.

Second problem: Now I want to add two strings to one line and then underline them.I don't know if you want to align the strings like this from your question.

A simple task is to separate string1 and string2 in one blank space.

public void addParographWithTwoParts2 (Document doc, PdfFont font, String string1, String string2, float fontSize) {
 if(string1==null)string1=";
 if (string1.length()>10)
 string1 = string1.substring(0,10);
 if(string2==null)string2=";
 if(string1.length()+string2.length()>100)
 string2 = string2.substring(0,100-string1.length());
 Paragrap = new Paragrap(string1+"+string2).setFont(font).setFontSize(fontSize);
 doc.add(p);
 doc.add(new LineSparator(new SolidLine(1)).setMarginTop(-6));
}

A more complex task is to align string2 to the right.

ublic void addParographWithTwoParts1 (Document doc, PdfFont font, String string1, String string2, float fontSize) {
 if(string1==null)string1=";
 if (string1.length()>10)
 string1 = string1.substring(0,10);
 Text chunk1 = new Text(string1).setFont(font).setFontSize(fontSize);
 if(string2==null)string2=";
 if(string1.length()+string2.length()>100)
 string2 = string2.substring(0,100-string1.length());
 Text chunk2 = new Text(string2).setFont(font).setFontSize(fontSize);
 Paragraph = new Paragraph();
 p.add(chunk1);
 p.addTabStop (new TabStop (1000, TabAlignment.RIGHT));
 p.add(new Tab());
 p.add(chunk2);
 doc.add(p);
 doc.add(new LineSparator(new SolidLine(1)).setMarginTop(-6));
}

As you can see, use only TabStop without adding spaces.

p.addTabStop(newTabStop(1000,TabAlignment.RIGHT));
p.add(new Tab());

Attempted to create a sample code with string1 and string2 numbers.The sample code is as follows:

Enter a description of the image here

In the screenshot, string2 is aligned to the right, and string2 is immediately followed by string1 (but separated by one blank).


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.