本文整理汇总了Java中com.lowagie.text.pdf.PdfContentByte.showText方法的典型用法代码示例。如果您正苦于以下问题:Java PdfContentByte.showText方法的具体用法?Java PdfContentByte.showText怎么用?Java PdfContentByte.showText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.lowagie.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.showText方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setPageNumbers
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
private void setPageNumbers() throws DocumentException, IOException {
int pages = getReader().getNumberOfPages();
int i = 0;
PdfContentByte overContent;
Rectangle pageSize = null;
BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
while (i < pages) {
i++;
overContent = getStamper().getOverContent(i);
pageSize = overContent.getPdfDocument().getPageSize();
overContent.beginText();
overContent.setFontAndSize(font, 9);
overContent.setTextMatrix(pageSize.getWidth() - 50, pageSize.getHeight() - 70);
overContent.showText("Page " + i + " of " + pages);
overContent.endText();
}
}
示例2: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Shading example.
*/
@Test
public void main() throws Exception {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
PdfTestBase.getOutputStream("shading_pattern.pdf"));
document.open();
PdfShading shading = PdfShading.simpleAxial(writer, 100, 100, 400, 100,
Color.red, Color.cyan);
PdfShadingPattern shadingPattern = new PdfShadingPattern(shading);
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD,
BaseFont.WINANSI, false);
cb.setShadingFill(shadingPattern);
cb.beginText();
cb.setTextMatrix(100, 100);
cb.setFontAndSize(bf, 40);
cb.showText("Look at this text!");
cb.endText();
PdfShading shadingR = PdfShading.simpleRadial(writer, 200, 500, 50,
300, 500, 100, new Color(255, 247, 148), new Color(247, 138,
107), false, false);
cb.paintShading(shadingR);
cb.sanityCheck();
document.close();
}
示例3: main
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
@Test
public void main() throws Exception {
// we create a reader for a certain document
PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR +"ChapterSection.pdf");
int n = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader,PdfTestBase.getOutputStream("watermark_pagenumbers.pdf"));
// adding some metadata
HashMap<String, String> moreInfo = new HashMap<String, String>();
moreInfo.put("Author", "Bruno Lowagie");
stamp.setMoreInfo(moreInfo);
// adding content to each page
int i = 0;
PdfContentByte under;
PdfContentByte over;
Image img = Image.getInstance(PdfTestBase.RESOURCES_DIR +"watermark.jpg");
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
img.setAbsolutePosition(200, 400);
while (i < n) {
i++;
// watermark under the existing page
under = stamp.getUnderContent(i);
under.addImage(img);
// text over the existing page
over = stamp.getOverContent(i);
over.beginText();
over.setFontAndSize(bf, 18);
over.setTextMatrix(30, 30);
over.showText("page " + i);
over.setFontAndSize(bf, 32);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
over.endText();
}
// adding an extra page
stamp.insertPage(1, PageSize.A4);
over = stamp.getOverContent(1);
over.beginText();
over.setFontAndSize(bf, 18);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE OF AN EXISTING PDF DOCUMENT", 30, 600, 0);
over.endText();
// adding a page from another document
PdfReader reader2 = new PdfReader(PdfTestBase.RESOURCES_DIR +"SimpleAnnotations1.pdf");
under = stamp.getUnderContent(1);
under.addTemplate(stamp.getImportedPage(reader2, 3), 1, 0, 0, 1, 0, 0);
// closing PdfStamper will generate the new PDF file
stamp.close();
}
示例4: 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();
}
示例5: onEndPage
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// write the headertable
table.setTotalWidth(document.right() - document.left());
table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb);
// compose the footer
String text = "Page " + writer.getPageNumber() + " of ";
float textSize = helv.getWidthPoint(text, 12);
float textBase = document.bottom() - 20;
cb.beginText();
cb.setFontAndSize(helv, 12);
// for odd pagenumbers, show the footer at the left
if ((writer.getPageNumber() & 1) == 1) {
cb.setTextMatrix(document.left(), textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.left() + textSize, textBase);
}
// for even numbers, show the footer at the right
else {
float adjust = helv.getWidthPoint("0", 12);
cb.setTextMatrix(document.right() - textSize - adjust, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, document.right() - adjust, textBase);
}
// draw a Rectangle around the page
cb.setColorStroke(Color.orange);
cb.setLineWidth(2);
cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
cb.stroke();
// starting on page 3, a watermark with an Image that is made transparent
if (writer.getPageNumber() >= 3) {
cb.setGState(gstate);
cb.setColorFill(Color.red);
cb.beginText();
cb.setFontAndSize(helv, 48);
cb.showTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
cb.endText();
try {
cb.addImage(headerImage, headerImage.getWidth(), 0, 0, headerImage.getHeight(), 440, 80);
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
cb.restoreState();
cb.sanityCheck();
}
示例6: onFinPagina2
import com.lowagie.text.pdf.PdfContentByte; //导入方法依赖的package包/类
private void onFinPagina2(PdfWriter writer, Document document) {
Rectangle page = document.getPageSize();
PdfContentByte cb = writer.getDirectContent();
Font font = new Font(Font.HELVETICA, 7, Font.ITALIC, Color.GRAY);
if (cabecera != null){
PdfPTable head = new PdfPTable(1);
head.getDefaultCell().setBorder(Rectangle.NO_BORDER);
head.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
Paragraph bloque = new Paragraph(new Chunk(cabecera,font));
head.addCell(bloque);
head.addCell(new Phrase(new Chunk("",font)));
head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(),writer.getDirectContent());
}
// Texto de pie y c�digo de barras
if (pie == null) pie = "";
/** VRS: cambio para a�adir paginacion */
cb.saveState();
String text = pie + " - P�g. " + writer.getPageNumber() + " de ";
float textBase = document.bottom() - 20;
float textSize = bFont.getWidthPoint(text, 7);
cb.saveState();
cb.beginText();
cb.setFontAndSize(bFont, 7);
float adjust = bFont.getWidthPoint("0", 7);
cb.setTextMatrix(document.right() - textSize - adjust, textBase);
cb.setColorFill(Color.GRAY);
cb.showText(text);
cb.endText();
cb.addTemplate(tplTotal, document.right() - adjust, textBase);
cb.restoreState();
}