我希望PDTextbox有红色文字。我能写出红色文字,我可以设置文本框的值,但我不知道如何将文本框内容设置为红色。
即
if (field instanceof PDTextbox) {
field.setValue(field.getPartialName());
//SOME WAY TO SET COLOR HERE?
以下是我正在使用的测试代码:
package com.circumail;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextbox;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("c://temp//Work_Comp_App-Acord_130 fillv2.pdf");
System.out.println("exists= " + file.exists());
// Load the pdfTemplate
PDDocument pdfDoc = PDDocument.load(file);
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
// Get field names
List<PDField> fieldList = acroForm.getFields();
List<PDPage> pages = pdfDoc.getDocumentCatalog().getAllPages();
for (PDPage page : pages) {
// PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, firstPage, true, false);
PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page, true, true, true);
processFields(acroForm, fieldList, contentStream);
contentStream.close();
}
// Save edited file
pdfDoc.save("c://temp//Work_Comp_App-Acord_130 fillv2 out.pdf");
pdfDoc.close();
}
private static void processFields(PDAcroForm acroForm, List<PDField> fieldList, PDPageContentStream contentStream) throws IOException {
for (PDField field : fieldList) {
if (field instanceof PDTextbox) {
field.setValue(field.getPartialName());
}else{
PDRectangle rect = getOffsetRectangle(field);
//set text color to RED - not sure if I neet to set this back, can't get original color by calling contentStream.getNonStrokingColor()
contentStream.setNonStrokingColor(Color.RED);
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 8);
contentStream.moveTextPositionByAmount(rect.getLowerLeftX(),rect.getLowerLeftY());
contentStream.drawString( field.getPartialName());
contentStream.endText();
}
}
}
private static PDRectangle getOffsetRectangle(PDField field) {
COSDictionary fieldDict = field.getDictionary();
COSArray fieldAreaArray = (COSArray) fieldDict.getDictionaryObject(COSName.RECT);
PDRectangle rect = new PDRectangle(fieldAreaArray);
//move the text up and to the right a bit
int extra = 10;
float x = rect.getLowerLeftX()+ extra;
float y = rect.getLowerLeftY() + extra;
float width = rect.getUpperRightX() + extra;
float height = rect.getUpperRightY()+ extra;
rect = new PDRectangle(new BoundingBox(x, y, width, height));
return rect;
}
private static void printRect(final PDPageContentStream contentStream, final PDRectangle rect) throws IOException {
contentStream.setStrokingColor(Color.YELLOW);
contentStream.drawLine(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getLowerLeftX(), rect.getUpperRightY()); // left
contentStream.drawLine(rect.getLowerLeftX(), rect.getUpperRightY(), rect.getUpperRightX(), rect.getUpperRightY()); // top
contentStream.drawLine(rect.getUpperRightX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()); // right
contentStream.drawLine(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getLowerLeftY()); // bottom
contentStream.setStrokingColor(Color.BLACK);
}
}
答案 0 :(得分:3)
通常,文本字段具有默认外观条目,PDFBox构造外观。因此,您只需要更改此默认外观以包含选择红色的语句。
E.g。
PDDocument pdfDoc = PDDocument.load(SOURCE);
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
for (Object field : acroForm.getFields())
{
if (field instanceof PDVariableText)
{
COSDictionary dict = ((PDField)field).getDictionary();
COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
if (defaultAppearance != null)
dict.setString(COSName.DA, defaultAppearance.getString() + " 1 0 0 rg ");
field = field instanceof PDTextbox ? new PDTextbox(acroForm, dict) : new PDChoiceField(acroForm, dict);
((PDField)field).setValue(VALUE);
}
}
pdfDoc.save(TARGET);
pdfDoc.close();
此代码首先增强默认外观,然后设置字段值。字段变量必须在两者之间更新,因为PDVariableText
在初始化期间将默认外观存储在隐藏成员中。