本文整理匯總了Java中com.lowagie.text.pdf.PdfContentByte.setColorFill方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfContentByte.setColorFill方法的具體用法?Java PdfContentByte.setColorFill怎麽用?Java PdfContentByte.setColorFill使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.lowagie.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.setColorFill方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onStartPage
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
*/
public void onStartPage(PdfWriter writer, Document document) {
if (writer.getPageNumber() < 3) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.saveState();
cb.setColorFill(Color.pink);
cb.beginText();
cb.setFontAndSize(helv, 48);
cb.showTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
cb.endText();
cb.restoreState();
}
}
示例2: placeBarcode
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
*
* @param cb
* @param foreground
* @param moduleSide
*/
public void placeBarcode(PdfContentByte cb, Color foreground, float moduleSide) {
int width = bm.getWidth();
int height = bm.getHeight();
byte[][] mt = bm.getArray();
cb.setColorFill(foreground);
for (int y = 0; y < height; ++y) {
byte[] line = mt[y];
for (int x = 0; x < width; ++x) {
if (line[x] == 0) {
cb.rectangle(x * moduleSide, (height - y - 1) * moduleSide, moduleSide, moduleSide);
}
}
}
cb.fill();
}
示例3: pictureBackdrop
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Prints a square and fills half of it with a gray rectangle.
* @param x
* @param y
* @param cb
* @throws Exception
*/
public static void pictureBackdrop(float x, float y, PdfContentByte cb) throws Exception {
cb.setColorStroke(Color.black);
cb.setColorFill(Color.red);
cb.rectangle(x, y, 100, 200);
cb.fill();
cb.setLineWidth(2);
cb.rectangle(x, y, 200, 200);
cb.stroke();
}
示例4: pictureCircles
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Prints 3 circles in different colors that intersect with eachother.
* @param x
* @param y
* @param cb
* @throws Exception
*/
public static void pictureCircles(float x, float y, PdfContentByte cb) throws Exception {
PdfGState gs = new PdfGState();
gs.setBlendMode(PdfGState.BM_SOFTLIGHT);
gs.setFillOpacity(0.7f);
cb.setGState(gs);
cb.setColorFill(Color.gray);
cb.circle(x + 70, y + 70, 50);
cb.fill();
cb.circle(x + 100, y + 130, 50);
cb.fill();
cb.circle(x + 130, y + 70, 50);
cb.fill();
}
示例5: pictureBackdrop
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Prints a square and fills half of it with a gray rectangle.
*
* @param x
* @param y
* @param cb
* @throws Exception
*/
private static void pictureBackdrop(float x, float y, PdfContentByte cb)
throws Exception {
cb.setColorStroke(Color.black);
cb.setColorFill(Color.gray);
cb.rectangle(x, y, 100, 200);
cb.fill();
cb.setLineWidth(2);
cb.rectangle(x, y, 200, 200);
cb.stroke();
}
示例6: pictureCircles
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Prints 3 circles in different colors that intersect with eachother.
*
* @param x
* @param y
* @param cb
* @throws Exception
*/
private static void pictureCircles(float x, float y, PdfContentByte cb)
throws Exception {
cb.setColorFill(Color.red);
cb.circle(x + 70, y + 70, 50);
cb.fill();
cb.setColorFill(Color.yellow);
cb.circle(x + 100, y + 130, 50);
cb.fill();
cb.setColorFill(Color.blue);
cb.circle(x + 130, y + 70, 50);
cb.fill();
}
示例7: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Changing the Graphics State with saveState() and restoreState().
*
*/
@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( "state.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();
cb.circle(260.0f, 500.0f, 250.0f);
cb.fill();
cb.saveState();
cb.setColorFill(Color.red);
cb.circle(260.0f, 500.0f, 200.0f);
cb.fill();
cb.saveState();
cb.setColorFill(Color.blue);
cb.circle(260.0f, 500.0f, 150.0f);
cb.fill();
cb.restoreState();
cb.circle(260.0f, 500.0f, 100.0f);
cb.fill();
cb.restoreState();
cb.circle(260.0f, 500.0f, 50.0f);
cb.fill();
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();
}
示例8: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Changing the Graphics State with PdfGState.
*
*/
@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( "gstate.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();
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.5f);
cb.setGState(gs);
cb.setColorFill(Color.red);
cb.circle(260.0f, 500.0f, 250.0f);
cb.fill();
cb.circle(260.0f, 500.0f, 200.0f);
cb.fill();
cb.circle(260.0f, 500.0f, 150.0f);
cb.fill();
gs.setFillOpacity(0.2f);
cb.setGState(gs);
cb.setColorFill(Color.blue);
cb.circle(260.0f, 500.0f, 100.0f);
cb.fill();
cb.circle(260.0f, 500.0f, 50.0f);
cb.fill();
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();
}
示例9: 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();
}
示例10: main
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Creates a document with some PdfAnnotations.
*
*/
@Test
public void main() throws Exception {
// step 1: creation of a document-object
Document document = new Document();
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Annotations.pdf"));
// step 3:
writer.setPdfVersion(PdfWriter.VERSION_1_5);
document.open();
// step 4:
PdfContentByte cb = writer.getDirectContent();
// page 1
PdfFileSpecification fs = PdfFileSpecification.fileExtern(writer, PdfTestBase.RESOURCES_DIR + "cards.mpg");
writer.addAnnotation(PdfAnnotation.createScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "cards.mpg", fs,
"video/mpeg", true));
PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.javaScript(
"app.alert('Hello');\r", writer));
document.add(new Chunk("click to trigger javascript").setAnnotation(a).setLocalDestination("top"));
writer.addAnnotation(a);
writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
"This is some text", "some text".getBytes(), null, "some.txt"));
writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help",
"This Help annotation was made with 'createText'", false, "Help"));
writer.addAnnotation(PdfAnnotation.createText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help",
"This Comment annotation was made with 'createText'", true, "Comment"));
cb.rectangle(200, 700, 100, 100);
cb.rectangle(200, 550, 100, 100);
cb.rectangle(200, 400, 100, 100);
cb.rectangle(200, 250, 100, 100);
cb.stroke();
document.newPage();
// page 2
writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 700f, 300f, 800f),
PdfAnnotation.HIGHLIGHT_TOGGLE, PdfAction.javaScript("app.alert('Hello');\r", writer)));
writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 550f, 300f, 650f),
PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
writer.addAnnotation(PdfAnnotation.createLink(writer, new Rectangle(200f, 400f, 300f, 500f),
PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
writer.addAnnotation(PdfAnnotation.createSquareCircle(writer, new Rectangle(200f, 250f, 300f, 350f),
"This Comment annotation was made with 'createSquareCircle'", false));
document.newPage();
// page 3
PdfContentByte pcb = new PdfContentByte(writer);
pcb.setColorFill(new Color(0xFF, 0x00, 0x00));
writer.addAnnotation(PdfAnnotation.createFreeText(writer, new Rectangle(200f, 700f, 300f, 800f),
"This is some free text, blah blah blah", pcb));
writer.addAnnotation(PdfAnnotation.createLine(writer, new Rectangle(200f, 550f, 300f, 650f), "this is a line",
200, 550, 300, 650));
writer.addAnnotation(PdfAnnotation.createStamp(writer, new Rectangle(200f, 400f, 300f, 500f),
"This is a stamp", "Stamp"));
writer.addAnnotation(PdfAnnotation.createPopup(writer, new Rectangle(200f, 250f, 300f, 350f),
"Hello, I'm a popup!", true));
cb.rectangle(200, 700, 100, 100);
cb.rectangle(200, 550, 100, 100);
cb.rectangle(200, 250, 100, 100);
cb.stroke();
// step 5: we close the document
document.close();
}
示例11: 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();
}
示例12: addFooterAndWater
import com.lowagie.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* 添加水印、頁眉、頁腳
* @param fileName 源文件路徑
* @param savepath 目標文件路徑
* @param waterMarkName 文字水印
* @param pageHeade 頁眉
* @param foot 頁腳
* @return
*/
public static int addFooterAndWater(String fileName, String savepath, String waterMarkName, String pageHeade, String foot) {
// 文檔總頁數
int num = 0;
Document document = new Document();
try {
PdfReader reader = new PdfReader(fileName);
//BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
BaseFont base = BaseFont.createFont(BaseFont.COURIER, "utf-8", BaseFont.EMBEDDED);
num = reader.getNumberOfPages();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
document.open();
for (int i = 0; i < num;) {
PdfImportedPage page = copy.getImportedPage(reader, ++i);
PageStamp stamp = copy.createPageStamp(page);
Font f = new Font(base);
// 添加頁腳,左側文字,右側頁碼
ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_RIGHT, new Phrase(String.format("第 %d 頁/共 %d 頁", i, num),
f), 550f, 28, 0);
ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_LEFT, new Phrase(foot, f), 50f, 28, 0);
// 添加頁眉 (文字頁眉,居中)
ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_CENTER, new Phrase(pageHeade, f), 150f, 800, 0);
// 頁眉添加logo (圖片頁眉,居右)
Image img = Image.getInstance("F:\\Tools\\pdf2swf工具\\resource/watermark.png");//"template/logo.png");// 選擇圖片
img.setAlignment(1);
img.scaleAbsolute(436 / 5, 96 / 5);// 控製圖片大小
img.setAbsolutePosition(450f, 800);// 控製圖片位置
stamp.getUnderContent().addImage(img);
// 添加水印
PdfContentByte under = stamp.getUnderContent();
under.beginText();
under.setColorFill(Color.LIGHT_GRAY);
// 字符越長,字體越小,設置字體
int fontSize = getFontSize(waterMarkName);
under.setFontAndSize(base, fontSize);
// 設置水印文字字體傾斜 開始
float pageWidth = reader.getPageSize(i).getWidth();
float pageHeight = reader.getPageSize(i).getHeight();
// 水印文字成60度角傾斜,且頁麵居中展示
//under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, pageWidth / 2, pageHeight / 2, 60);
img.setAlignment(1);
img.scaleAbsolute(636 / 5, 126 / 5);// 控製圖片大小
img.setAbsolutePosition(pageWidth / 2, pageHeight / 2);// 控製圖片位置
img.setRotation(60);
stamp.getUnderContent().addImage(img);
// 字體設置結束
under.endText();
stamp.alterContents();
copy.addPage(page);
}
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
if (null != document) {
document.close();
}
}
System.out.println("pdf totalpages:" + num);
return num;
}