本文整理汇总了Java中com.lowagie.text.pdf.PdfContentByte.moveTo方法的典型用法代码示例。如果您正苦于以下问题:Java PdfContentByte.moveTo方法的具体用法?Java PdfContentByte.moveTo怎么用?Java PdfContentByte.moveTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.lowagie.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.moveTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: drawLine
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Draws a horizontal line.
* @param canvas the canvas to draw on
* @param leftX the left x coordinate
* @param rightX the right x coordindate
* @param y the y coordinate
*/
public void drawLine(PdfContentByte canvas, float leftX, float rightX, float y) {
float w;
if (getPercentage() < 0)
w = -getPercentage();
else
w = (rightX - leftX) * getPercentage() / 100.0f;
float s;
switch (getAlignment()) {
case Element.ALIGN_LEFT:
s = 0;
break;
case Element.ALIGN_RIGHT:
s = rightX - leftX - w;
break;
default:
s = (rightX - leftX - w) / 2;
break;
}
canvas.setLineWidth(getLineWidth());
if (getLineColor() != null)
canvas.setColorStroke(getLineColor());
canvas.moveTo(s + leftX, y + offset);
canvas.lineTo(s + w + leftX, y + offset);
canvas.stroke();
}
示例2: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
@Test
public void main() throws Exception {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "shading.pdf"));
document.open();
PdfFunction function1 = PdfFunction.type2(writer, new float[] { 0, 1 },
null, new float[] { .929f, .357f, 1, .298f }, new float[] {
.631f, .278f, 1, .027f }, 1.048f);
PdfFunction function2 = PdfFunction.type2(writer, new float[] { 0, 1 },
null, new float[] { .929f, .357f, 1, .298f }, new float[] {
.941f, .4f, 1, .102f }, 1.374f);
PdfFunction function3 = PdfFunction.type3(writer, new float[] { 0, 1 },
null, new PdfFunction[] { function1, function2 },
new float[] { .708f }, new float[] { 1, 0, 0, 1 });
PdfShading shading = PdfShading.type3(writer,
new CMYKColor(0, 0, 0, 0),
new float[] { 0, 0, .096f, 0, 0, 1 }, null, function3,
new boolean[] { true, true });
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(316.789f, 140.311f);
cb.curveTo(303.222f, 146.388f, 282.966f, 136.518f, 279.122f, 121.983f);
cb.lineTo(277.322f, 120.182f);
cb.curveTo(285.125f, 122.688f, 291.441f, 121.716f, 298.156f, 119.386f);
cb.lineTo(336.448f, 119.386f);
cb.curveTo(331.072f, 128.643f, 323.346f, 137.376f, 316.789f, 140.311f);
cb.clip();
cb.newPath();
cb.saveState();
cb.concatCTM(27.7843f, 0, 0, -27.7843f, 310.2461f, 121.1521f);
cb.paintShading(shading);
cb.restoreState();
cb.sanityCheck();
document.close();
}
示例3: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Generates an Acroform with a Combobox
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("combo.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(0, 0);
String options[] = { "Red", "Green", "Blue" };
PdfFormField field = PdfFormField.createCombo(writer, true, options, 0);
field.setWidget(new Rectangle(100, 700, 180, 720), PdfAnnotation.HIGHLIGHT_INVERT);
field.setFieldName("ACombo");
field.setValueAsString("Red");
writer.addAnnotation(field);
// step 5: we close the document
document.close();
}
示例4: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Draws some shapes.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "shapes.pdf"));
// step 3: we open the document
document.open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.getDirectContent();
// an example of a rectangle with a diagonal in very thick lines
cb.setLineWidth(10f);
// draw a rectangle
cb.rectangle(100, 700, 100, 100);
// add the diagonal
cb.moveTo(100, 700);
cb.lineTo(200, 800);
// stroke the lines
cb.stroke();
// an example of some circles
cb.setLineDash(3, 3, 0);
cb.setRGBColorStrokeF(0f, 255f, 0f);
cb.circle(150f, 500f, 100f);
cb.stroke();
cb.setLineWidth(5f);
cb.resetRGBColorStroke();
cb.circle(150f, 500f, 50f);
cb.stroke();
// example with colorfill
cb.setRGBColorFillF(0f, 255f, 0f);
cb.moveTo(100f, 200f);
cb.lineTo(200f, 250f);
cb.lineTo(400f, 150f);
// because we change the fill color BEFORE we stroke the triangle
// the color of the triangle will be red instead of green
cb.setRGBColorFillF(255f, 0f, 0f);
cb.closePathFillStroke();
cb.sanityCheck();
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
示例5: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Writing vertical text.
*/
@Test
public void main() throws Exception {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
texts[3] = convertCid(texts[0]);
texts[4] = convertCid(texts[1]);
texts[5] = convertCid(texts[2]);
PdfWriter writer = PdfWriter.getInstance(document,PdfTestBase.getOutputStream("vertical.pdf"));
int idx = 0;
document.open();
PdfContentByte cb = writer.getDirectContent();
for (int j = 0; j < 2; ++j) {
BaseFont bf = BaseFont.createFont("KozMinPro-Regular", encs[j], false);
cb.setRGBColorStroke(255, 0, 0);
cb.setLineWidth(0);
float x = 400;
float y = 700;
float height = 400;
float leading = 30;
int maxLines = 6;
for (int k = 0; k < maxLines; ++k) {
cb.moveTo(x - k * leading, y);
cb.lineTo(x - k * leading, y - height);
}
cb.rectangle(x, y, -leading * (maxLines - 1), -height);
cb.stroke();
VerticalText vt = new VerticalText(cb);
vt.setVerticalLayout(x, y, height, maxLines, leading);
vt.addText(new Chunk(texts[idx++], new Font(bf, 20)));
vt.addText(new Chunk(texts[idx++], new Font(bf, 20, 0, Color.blue)));
vt.go();
vt.setAlignment(Element.ALIGN_RIGHT);
vt.addText(new Chunk(texts[idx++], new Font(bf, 20, 0, Color.orange)));
vt.go();
document.newPage();
}
document.close();
}
示例6: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Generates an Acroform with a PushButton
*/
@Test
public void main() throws Exception {
Document.compress = false;
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("pushbutton.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfFormField pushbutton = PdfFormField.createPushButton(writer);
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(0, 0);
PdfAppearance normal = cb.createAppearance(100, 50);
normal.setColorFill(Color.GRAY);
normal.rectangle(5, 5, 90, 40);
normal.fill();
PdfAppearance rollover = cb.createAppearance(100, 50);
rollover.setColorFill(Color.RED);
rollover.rectangle(5, 5, 90, 40);
rollover.fill();
PdfAppearance down = cb.createAppearance(100, 50);
down.setColorFill(Color.BLUE);
down.rectangle(5, 5, 90, 40);
down.fill();
pushbutton.setFieldName("PushMe");
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, normal);
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, rollover);
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, down);
pushbutton.setWidget(new Rectangle(100, 700, 200, 750), PdfAnnotation.HIGHLIGHT_PUSH);
writer.addAnnotation(pushbutton);
// step 5: we close the document
document.close();
}
示例7: cellLayout
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
* com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
*/
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
cb.moveTo(position.getLeft(), position.getBottom());
cb.lineTo(position.getRight(), position.getTop());
cb.stroke();
}
示例8: addBarcode
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public void addBarcode(String barcode, int x, int y) throws ParserException {
try {
Barcode128 code128 = new Barcode128();
code128.setCode(barcode);
PdfContentByte cb = stamp.getOverContent(1);
Image image128 = Image.getInstance(code128.createImageWithBarcode(cb, null, null));
cb.addImage(image128, image128.width(), 0, 0, image128.height(), x, y);
cb.moveTo(0, 0);
} catch (DocumentException e) {
throw new ParserException("Could not add barcode", e);
}
}
示例9: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Creates a PDF document with shapes, lines and text at specific X and Y coordinates.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
try {
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "XandY.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(25, 25);
// we add some crosses to visualize the coordinates
template.moveTo(13, 0);
template.lineTo(13, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 216 - 13, 720 - 13);
cb.addTemplate(template, 360 - 13, 360 - 13);
cb.addTemplate(template, 360 - 13, 504 - 13);
cb.addTemplate(template, 72 - 13, 144 - 13);
cb.addTemplate(template, 144 - 13, 288 - 13);
cb.moveTo(216, 720);
cb.lineTo(360, 360);
cb.lineTo(360, 504);
cb.lineTo(72, 144);
cb.lineTo(144, 288);
cb.stroke();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
cb.endText();
cb.sanityCheck();
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
示例10: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Changes the default coordinate system so that the origin is in the upper left corner
* instead of the lower left corner.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
try {
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "upsidedown.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
cb.concatCTM(1f, 0f, 0f, -1f, 0f, PageSize.A4.getHeight());
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(25, 25);
// we add some crosses to visualize the coordinates
template.moveTo(13, 0);
template.lineTo(13, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 216 - 13, 720 - 13);
cb.addTemplate(template, 360 - 13, 360 - 13);
cb.addTemplate(template, 360 - 13, 504 - 13);
cb.addTemplate(template, 72 - 13, 144 - 13);
cb.addTemplate(template, 144 - 13, 288 - 13);
cb.moveTo(216, 720);
cb.lineTo(360, 360);
cb.lineTo(360, 504);
cb.lineTo(72, 144);
cb.lineTo(144, 288);
cb.stroke();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(3\", 10\")", 216 + 25, 720 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 5\")", 360 + 25, 360 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(5\", 7\")", 360 + 25, 504 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(1\", 2\")", 72 + 25, 144 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "(2\", 4\")", 144 + 25, 288 + 5, 0);
cb.endText();
cb.sanityCheck();
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
示例11: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Changes the transformation matrix with AffineTransform.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document,
PdfTestBase.getOutputStream("affinetransformation.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
cb.transform(AffineTransform.getScaleInstance(1.2, 0.75));
// we create a PdfTemplate
PdfTemplate template = cb.createTemplate(25, 25);
// we add some crosses to visualize the coordinates
template.moveTo(13, 0);
template.lineTo(13, 25);
template.moveTo(0, 13);
template.lineTo(50, 13);
template.stroke();
template.sanityCheck();
// we add the template on different positions
cb.addTemplate(template, 216 - 13, 720 - 13);
cb.addTemplate(template, 360 - 13, 360 - 13);
cb.addTemplate(template, 360 - 13, 504 - 13);
cb.addTemplate(template, 72 - 13, 144 - 13);
cb.addTemplate(template, 144 - 13, 288 - 13);
cb.moveTo(216, 720);
cb.lineTo(360, 360);
cb.lineTo(360, 504);
cb.lineTo(72, 144);
cb.lineTo(144, 288);
cb.stroke();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(3\" * 1.2, 10\" * .75)", 216 + 25, 720 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(5\" * 1.2, 5\" * .75)", 360 + 25, 360 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(5\" * 1.2, 7\" * .75)", 360 + 25, 504 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(1\" * 1.2, 2\" * .75)", 72 + 25, 144 + 5, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"(2\" * 1.2, 4\" * .75)", 144 + 25, 288 + 5, 0);
cb.endText();
cb.sanityCheck();
// step 5: we close the document
document.close();
}
示例12: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Adding text at absolute positions.
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document,
PdfTestBase.getOutputStream("text.pdf"));
// step 3: we open the document
document.open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.getDirectContent();
// first we draw some lines to be able to visualize the text alignment
// functions
cb.setLineWidth(0f);
cb.moveTo(250, 500);
cb.lineTo(250, 800);
cb.moveTo(50, 700);
cb.lineTo(400, 700);
cb.moveTo(50, 650);
cb.lineTo(400, 650);
cb.moveTo(50, 600);
cb.lineTo(400, 600);
cb.stroke();
// we tell the ContentByte we're ready to draw text
cb.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
cb.setFontAndSize(bf, 12);
String text = "Sample text for alignment";
// we show some text starting on some absolute position with a given
// alignment
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250,
700, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250,
650, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600,
0);
// we draw some text on a certain position
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");
// we draw some rotated text on a certain position
cb.setTextMatrix(0, 1, -1, 0, 100, 300);
cb.showText("Text at position 100,300, rotated 90 degrees.");
// we draw some mirrored, rotated text on a certain position
cb.setTextMatrix(0, 1, 1, 0, 200, 200);
cb.showText("Text at position 200,200, mirrored and rotated 90 degrees.");
// we tell the contentByte, we've finished drawing text
cb.endText();
cb.sanityCheck();
// step 5: we close the document
document.close();
}
示例13: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Adding text at absolute positions.
*
* @param args
* no arguments needed
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
// step 2: creation of the writer
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("complextext.pdf"));
// step 3: we open the document
document.open();
// step 4: we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.getDirectContent();
// first we draw some lines to be able to visualize the text alignment
// functions
cb.setLineWidth(0f);
cb.moveTo(250, 500);
cb.lineTo(250, 800);
cb.moveTo(50, 700);
cb.lineTo(400, 700);
cb.moveTo(50, 650);
cb.lineTo(400, 650);
cb.moveTo(50, 600);
cb.lineTo(400, 600);
cb.stroke();
File font = new File (PdfTestBase.RESOURCES_DIR + "/liberation-fonts-ttf/LiberationSans-Regular.ttf");
// we construct a font
BaseFont bf = BaseFont.createFont(font.getAbsolutePath(), BaseFont.IDENTITY_H, true);
Font ft = new Font(bf, 12);
// This is the text:
String text = "\u0623\u0648\u0631\u0648\u0628\u0627, \u0628\u0631\u0645\u062c\u064a\u0627\u062a \u0627\u0644\u062d\u0627\u0633\u0648\u0628 + \u0627\u0646\u062a\u0631\u0646\u064a\u062a :";
Phrase center = new Phrase(text + " Center", ft);
ColumnText
.showTextAligned(cb, PdfContentByte.ALIGN_CENTER, center, 250, 700, 0, PdfWriter.RUN_DIRECTION_RTL, 0);
ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_RIGHT, new Phrase(text + " Right", ft), 250, 650, 20,
PdfWriter.RUN_DIRECTION_RTL, 0);
ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_LEFT, new Phrase("Some text Left aligned", ft), 250, 600,
20);
float size = ColumnText.getWidth(center, PdfWriter.RUN_DIRECTION_RTL, 0);
cb.setRGBColorStroke(255, 0, 0);
cb.rectangle(250 - size / 2, 690, size, 30);
cb.stroke();
// step 5: we close the document
document.close();
}
示例14: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Generates an Acroform with a Checkbox
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("checkbox.pdf"));
// step 3: we open the document
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(0, 0);
PdfFormField field = PdfFormField.createCheckBox(writer);
PdfAppearance tpOff = cb.createAppearance(20, 20);
PdfAppearance tpOn = cb.createAppearance(20, 20);
tpOff.rectangle(1, 1, 18, 18);
tpOff.stroke();
tpOn.setRGBColorFill(255, 128, 128);
tpOn.rectangle(1, 1, 18, 18);
tpOn.fillStroke();
tpOn.moveTo(1, 1);
tpOn.lineTo(19, 19);
tpOn.moveTo(1, 19);
tpOn.lineTo(19, 1);
tpOn.stroke();
field.setWidget(new Rectangle(100, 700, 120, 720), PdfAnnotation.HIGHLIGHT_INVERT);
field.setFieldName("Urgent");
field.setValueAsName("Off");
field.setAppearanceState("Off");
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff);
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", tpOn);
writer.addAnnotation(field);
// step 5: we close the document
document.close();
}
示例15: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Generates an Acroform with a TextField
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document(PageSize.A4);
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("textfield.pdf"));
// step 3: we open the document
document.open();
// step 4:
BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false);
PdfContentByte cb = writer.getDirectContent();
cb.moveTo(0, 0);
String text = "Some start text";
float fontSize = 12;
Color textColor = new GrayColor(0f);
PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT);
field.setFlags(PdfAnnotation.FLAGS_PRINT);
field.setFieldName("ATextField");
field.setValueAsString(text);
field.setDefaultValueAsString(text);
field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID));
field.setPage();
PdfAppearance tp = cb.createAppearance(171, 19);
PdfAppearance da = (PdfAppearance) tp.getDuplicate();
da.setFontAndSize(helv, fontSize);
da.setColorFill(textColor);
field.setDefaultAppearanceString(da);
tp.beginVariableText();
tp.saveState();
tp.rectangle(2, 2, 167, 15);
tp.clip();
tp.newPath();
tp.beginText();
tp.setFontAndSize(helv, fontSize);
tp.setColorFill(textColor);
tp.setTextMatrix(4, 5);
tp.showText(text);
tp.endText();
tp.restoreState();
tp.endVariableText();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
writer.addAnnotation(field);
// step 5: we close the document
document.close();
}