pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jarrod <xho...@yahoo.com.INVALID>
Subject Font AutoSize Issue
Date Thu, 09 Jul 2015 22:32:18 GMT
I'm evaluating pdfbox for filling in form fields from a template pdf file.

>From what I can tell, pdfbox does not support autosize font. While it will retain the
font size 0 setting, the text that is placed into the fields is not autosized (until you alter
the value manually).

Option A:
Is there some kind of command to execute before saving the pdf to force it to autosize?

Option B:
Do some math and calculate a new font size. I've done this (see code below) but you obviously
lose the ability to autosize if the user ends up modifying a value.
Is there a way to do this math and set the initial display font size but leave the DA alone
with autosize set?
I'm guessing this would be done by setting some other COSName String similar to setting the
COSName.DA. However, I can't find much information on these.

Any help is appreciated. Code is below;

//item.field_value is the value I'm sticking into the form field

if (field instanceof PDTextbox)
	int len = item.field_value.length();
	if (len>2)
		COSDictionary dict = field.getDictionary();
		COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
		if (defaultAppearance != null)
			//split the DA to grab the font size
			String[] da = defaultAppearance.getString().split(" ");
			List<String> da2 = new ArrayList<String>();

			//loop through and remove any empty strings
			// because for some reason when the font size is 0 there is an empty extra string in the
			for(int x=0; x<da.length; x++)
				if (da[x].length()!=0)
			//Move back to a String[] (I'm new to java)
			da = da2.toArray(da);

			if (da.length >= 2) //only process if there are at least two characters
				if (da[1].equals("0")) //If font size is actually autosized
					COSArray fieldAreaArray = (COSArray) dict.getDictionaryObject(COSName.RECT);
					PDRectangle rect = new PDRectangle(fieldAreaArray);
					float width = rect.getWidth();
					int size = (int)(width / len * 1.92); //doing stupid math to estimate new font size
					if (size > 12)
						size = 12;
					else if (size < 8)
						size = 8;
					String customSize = "/Helv " + size + " Tf 0 g";
					dict.setString(COSName.DA, customSize); //Set the new font size here
					field = new PDTextbox(acroForm, dict); //Create as a new field (as seen in other threads)
field.setValue(item.field_value); //Set the field value


To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org

View raw message