本文整理汇总了Java中com.itextpdf.text.pdf.PdfContentByte.addImage方法的典型用法代码示例。如果您正苦于以下问题:Java PdfContentByte.addImage方法的具体用法?Java PdfContentByte.addImage怎么用?Java PdfContentByte.addImage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.addImage方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: build
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* Update NDA file with visitor name and visitor signature.
*
* @param destFile
* @param signatureImage signature file
* @param visitorName
* @return File
*/
public static File build(Path destFile, File signatureImage, String visitorName) {
try {
PdfReader pdfReader = new PdfReader(ndaUrl);
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream(destFile.toString()));
Image image = createNDAImage(signatureImage, 0, 0);
PdfContentByte over = pdfStamper.getOverContent(5);
over.addImage(image);
PdfContentByte pdfContentByte = pdfStamper.getOverContent(5);
pdfContentByte.beginText();
pdfContentByte.setFontAndSize(BaseFont.createFont
(BaseFont.HELVETICA,
BaseFont.CP1257,
BaseFont.EMBEDDED
)
, 10);
pdfContentByte.setTextMatrix(112, 428);
pdfContentByte.showText(visitorName);
pdfContentByte.setTextMatrix(89, 406);
pdfContentByte.showText(new SimpleDateFormat("E, dd MMM yyyy").format(new Date()));
pdfContentByte.endText();
pdfStamper.close();
return destFile.toFile();
} catch (IOException | DocumentException | NumberFormatException e) {
logger.error("Exception while generating NDA file. ",e);
return null;
}
}
示例2: addRotatedImage
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
void addRotatedImage(PdfContentByte contentByte, Image image, float x, float y, float width, float height, float rotation) throws DocumentException
{
// Draw image at x,y without rotation
contentByte.addImage(image, width, 0, 0, height, x, y);
// Draw image as if the previous image was rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Stretch it to its dimensions
AffineTransform B = AffineTransform.getScaleInstance(width, height);
// Rotate it
AffineTransform C = AffineTransform.getRotateInstance(rotation);
// Move it to have the same center as above
AffineTransform D = AffineTransform.getTranslateInstance(x + width/2, y + height/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
//Draw
contentByte.addImage(image, M);
}
示例3: createRotatedImagePdf
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
static byte[] createRotatedImagePdf() throws DocumentException, IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
PdfContentByte directContent = writer.getDirectContent();
BufferedImage bim = new BufferedImage(1000, 250, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bim.createGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0, 0, 500, 500);
g2d.dispose();
Image image = Image.getInstance(bim, null);
directContent.addImage(image, 0, 500, -500, 0, 550, 50);
document.close();
return baos.toByteArray();
}
示例4: stampLayer
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public static byte[] stampLayer(InputStream _pdfFile, Image iImage, int x, int y, String layername, boolean readLayers) throws IOException, DocumentException
{
PdfReader reader = new PdfReader(_pdfFile);
try ( ByteArrayOutputStream ms = new ByteArrayOutputStream() )
{
PdfStamper stamper = new PdfStamper(reader, ms);
//Don't delete otherwise the stamper flattens the layers
if (readLayers)
stamper.getPdfLayers();
PdfLayer logoLayer = new PdfLayer(layername, stamper.getWriter());
PdfContentByte cb = stamper.getUnderContent(1);
cb.beginLayer(logoLayer);
//300dpi
iImage.scalePercent(24f);
iImage.setAbsolutePosition(x, y);
cb.addImage(iImage);
cb.endLayer();
stamper.close();
return (ms.toByteArray());
}
}
示例5: onRender
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public void onRender( PdfContentByte cb ) throws PdfRenderException{
float[] positions = new BlockFactory().getBoundsInPs(this);
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType( BarCodeType.getBarCodeType(barCodeType).codeType );
barcode.setCode( getCode() );
barcode.setSize( getFontSize() );
barcode.setGuardBars(isShowGuardBars());
barcode.setBarHeight( positions[3] );
barcode.setTextAlignment(Element.ALIGN_RIGHT);
if( getBaseFont() != null ){
barcode.setFont(getBaseFont());
}
if( align.equals( "top" )){
barcode.setBaseline( -1f );
}
PdfDocument request = getPage().getPdfDocument();
float pageHeight = request.getSize()[1];
float pageHeightInPs = SizeFactory.millimetersToPostscriptPoints(pageHeight);
com.itextpdf.text.Image image = barcode.createImageWithBarcode(cb, getBarBaseColor(), getCodeBaseColor());
image.setRotationDegrees( rotation );
image.setAbsolutePosition( positions[0] , pageHeightInPs - ( positions[1] + positions[3] + fontSize ) );
try {
cb.addImage( image );
} catch (DocumentException e) {
throw new PdfRenderException(e);
}
}
示例6: tiff2Pdf
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public void tiff2Pdf(File singleTifFile, File pdfFile) {
// based on tiff2pdf from itext toolbox 0.0.2
// (cfr.http://itexttoolbox.sourceforge.net/doku.php?id=download&DokuWiki=
// ecde1bfec0b8cca87dd8c6c042183992)
try {
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(
singleTifFile.getAbsolutePath());
// RandomAccessFileOrArray ra = new
// RandomAccessFileOrArray(tempByteArray);
int comps = TiffImage.getNumberOfPages(ra);
Document document = new Document(PageSize.A4);
float width = PageSize.A4.getWidth();
float height = PageSize.A4.getHeight();
Image img = TiffImage.getTiffImage(ra, 1);
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
// pdf/a
// from
// http://www.opensubscriber.com/message/[email protected]ceforge
// .net/7593470.html
// check that it is really pdf/a:
// http://www.intarsys.de/produkte/pdf-a-live/pdf-a-check-1
// => 2 warnings
// Keine eindeutige ID gefunden
// Kein History-Eintrag vorhanden
writer.setPDFXConformance(PdfWriter.PDFA1B);
document.open();
PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
outi.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString(
"sRGB IEC61966-2.1"));
outi.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
outi.put(PdfName.S, PdfName.GTS_PDFA1);
ICC_Profile icc = ICC_Profile.getInstance(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(
"/srgb.profile"));
PdfICCBased ib = new PdfICCBased(icc);
ib.remove(PdfName.ALTERNATE);
outi.put(PdfName.DESTOUTPUTPROFILE, writer.addToBody(ib)
.getIndirectReference());
writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS,
new PdfArray(outi));
PdfContentByte cb = writer.getDirectContent();
for (int c = 0; c < comps; ++c) {
img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
document.setPageSize(PageSize.A4);
document.newPage();
img.setAbsolutePosition(0, 0);
img.scaleToFit(width, height);
cb.addImage(img);
logger.debug("Finished page " + (c + 1));
}
}
ra.close();
writer.createXmpMetadata();// pdfa
document.close();
} catch (Throwable e) {
// catch Throwable because we encountere a java.lang.InternalError
// cfr. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6503430
// probably better to move to later java version for poller
logger.error("Pdf not created", e);
}
}
示例7: addBackground
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
private static void addBackground(PdfWriter writer)
throws BadElementException, MalformedURLException, IOException, DocumentException {
PdfContentByte canvas = writer.getDirectContentUnder();
canvas.saveState();
canvas.addImage(bkgnd);
canvas.restoreState();
}
示例8: createClippingTextPdf
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
static byte[] createClippingTextPdf() throws DocumentException, IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
PdfContentByte directContent = writer.getDirectContent();
directContent.beginText();
directContent.setTextRenderingMode(PdfPatternPainter.TEXT_RENDER_MODE_CLIP);
directContent.setTextMatrix(AffineTransform.getTranslateInstance(100, 400));
directContent.setFontAndSize(BaseFont.createFont(), 100);
directContent.showText("Test");
directContent.endText();
BufferedImage bim = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bim.createGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0, 0, 500, 500);
g2d.dispose();
Image image = Image.getInstance(bim, null);
directContent.addImage(image, 500, 0, 0, 599, 50, 50);
document.close();
return baos.toByteArray();
}
示例9: addLogo
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public void addLogo(PdfContentByte cb, String imagePath, float xPos, float yPos) throws MalformedURLException, IOException, DocumentException {
URL imageUrl = getClass().getResource(imagePath + "/IPAT_logo_Relaunch2016_RZ_RGB.jpg");
final Image logoImage = Image.getInstance(imageUrl);
logoImage.setAbsolutePosition(xPos, yPos);
logoImage.scalePercent(3f, 3f);
cb.addImage(logoImage);
}
示例10: generatePDF
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
public void generatePDF(File file) throws IOException, DocumentException {
Image image = Image.getInstance(getClass().getClassLoader().getResource("blank.png"));
image.scaleAbsolute(PageSize.A4.getWidth(), PageSize.A4.getHeight());
image.setAbsolutePosition(0, 0);
if (logoFile != null) {
logoImage = Image.getInstance(logoFile.toURI().toURL());
logoImage.scaleAbsolute((float) (logoImage.getWidth() * 0.35), (float) (logoImage.getHeight() * 0.35));
float logoWidth = logoImage.getWidth();
float logoHeight = logoImage.getHeight();
float ratioH = logoHeight / logoWidth;
float ratioW = logoWidth / logoHeight;
if (logoHeight >= logoWidth) {
logoHeight = 150f;
logoWidth = 150.0f * ratioW;
logoImage.scaleAbsolute(logoWidth, logoHeight);
} else {
logoHeight = 150f * ratioH;
logoWidth = 150.0f;
logoImage.scaleAbsolute(logoWidth, logoHeight);
}
logoImage.setAbsolutePosition(130f - (0.5f * logoWidth), 725f - (0.5f * logoHeight));
}
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
OutputStream outputStream = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
BaseFont baseBold = BaseFont.createFont(getClass().getClassLoader().getResource("font/AbhayaLibre-Bold.ttf").toString(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseFont baseRegular = BaseFont.createFont(getClass().getClassLoader().getResource("font/AbhayaLibre-Regular.ttf").toString(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
BaseFont baseExtraBold = BaseFont.createFont(getClass().getClassLoader().getResource("font/AbhayaLibre-ExtraBold.ttf").toString(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font fontBold = new Font(baseBold, 22);
Font fontExtraBold = new Font(baseExtraBold, 40);
Font fontRegular = new Font(baseRegular, 12);
document.open();
PdfContentByte pdfContentByte = writer.getDirectContent();
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("PODPIS WYSTAWCY", fontRegular), 100, 100, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("PODPIS ODBIORCY ", fontRegular), 390, 100, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("Termin płatności: " + invoiceData.getString("paymentDate"), fontRegular), 35, 170, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("Słownie: " + invoiceData.getString("inWordsValue"), fontRegular), 35, 195, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("RAZEM", fontBold), 35, 245, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase(invoiceData.getString("value") + " PLN", fontBold), 565, 245, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(invoiceData.getString("service"), fontBold), 35, 380, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase(invoiceData.getString("value") + " PLN", fontBold), 565, 380, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("Nazwa usługi", fontBold), 35, 400, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase("Wartość", fontBold), 565, 400, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(senderData.getString("accountNumber"), fontRegular), 35, 435, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("Numer konta bankowego:", fontRegular), 35, 450, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("NIP: " + senderData.getString("NIP"), fontRegular), 35, 485, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase("NIP: " + receiverData.getString("NIP"), fontRegular), 565, 485, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(senderData.getString("postalCode") + " " + senderData.getString("street"), fontRegular), 35, 510, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase(receiverData.getString("postalCode") + " " + receiverData.getString("street"), fontRegular), 565, 510, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("ul. " + senderData.getString("street") + " " + senderData.getString("building") + (senderData.getString("apartment").isEmpty() ? "" : "/") + senderData.getString("apartment"), fontRegular), 35, 535, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase("ul. " + receiverData.getString("street") + " " + receiverData.getString("building") + (receiverData.getString("apartment").isEmpty() ? "" : "/") + receiverData.getString("apartment"), fontRegular), 565, 535, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(senderData.getString("nameMore"), fontRegular), 35, 560, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase(receiverData.getString("nameMore"), fontRegular), 565, 560, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(senderData.getString("name"), fontRegular), 35, 585, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase(receiverData.getString("name"), fontRegular), 565, 585, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("WYSTAWCA", fontRegular), 35, 605, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase("ODBIORCA", fontRegular), 565, 605, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("NR: " + invoiceData.getString("ID"), new Font(baseBold, 36)), 265, 690, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase("RACHUNEK", fontExtraBold), 265, 733, 0);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_RIGHT, new Phrase(invoiceData.getString("city") + ", " + invoiceData.getString("date"), fontRegular), 565, 795, 0);
//pdfContentByte = writer.getDirectContent();
if (logoImage != null) {
pdfContentByte.addImage(logoImage);
}
pdfContentByte = writer.getDirectContentUnder();
pdfContentByte.addImage(image);
document.close();
outputStream.close();
}
示例11: addTextAndImage
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
private void addTextAndImage(PcGtsType pc, int cutoffLeft, int cutoffTop, Image img, boolean imageOnly) throws DocumentException, IOException {
lineAndColorList.clear();
PdfContentByte cb = writer.getDirectContentUnder();
cb.setColorFill(BaseColor.BLACK);
cb.setColorStroke(BaseColor.BLACK);
//BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", BaseFont.NOT_EMBEDDED);
if (!imageOnly){
cb.beginLayer(ocrLayer);
cb.setFontAndSize(bfArial, 32);
List<TrpRegionType> regions = pc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
/*
* use reading order comparator for sorting since at this time reading order is more trustable
* other sorting is not transitive and seldomly produces "Comparison violates its general contract" exception
*/
Collections.sort(regions, new TrpElementReadingOrderComparator<RegionType>(true));
for(RegionType r : regions){
//TODO add paths for tables etc.
if(r instanceof TextRegionType){
TextRegionType tr = (TextRegionType)r;
//PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX();
addTextFromTextRegion(tr, cb, cutoffLeft, cutoffTop, bfArial);
}
}
//scale after calculating lineMeanHeightForAllRegions
//lineMeanHeight = lineMeanHeight/scaleFactorX;
cb.endLayer();
}
cb.beginLayer(imgLayer);
cb.addImage(img);
cb.endLayer();
if (highlightTags){
highlightAllTagsOnImg(lineAndColorList, cb, cutoffLeft, cutoffTop);
}
/*
* draw tag lines
*/
// addTocLinks(doc, page,cutoffTop);
}
示例12: testAddRotatedImage
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/39364197/how-to-rotate-around-the-image-center-by-itext">
* How to rotate around the image center by itext?
* </a>
* <p>
* This test draws an image at given coordinates without rotation and then again
* as if that image was rotated around its center by some angle.
* </p>
*/
@Test
public void testAddRotatedImage() throws IOException, DocumentException
{
try ( FileOutputStream stream = new FileOutputStream(new File(RESULT_FOLDER, "rotatedImage.pdf")) )
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, stream);
document.open();
PdfContentByte contentByte = writer.getDirectContent();
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;
try (InputStream imageStream = getClass().getResourceAsStream("/mkl/testarea/itext5/layer/Willi-1.jpg"))
{
Image image = Image.getInstance(IOUtils.toByteArray(imageStream));
// Draw image at x,y without rotation
contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);
// Draw image as if the previous image was rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Stretch it to its dimensions
AffineTransform B = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform C = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform D = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
//Draw
contentByte.addImage(image, M);
}
document.close();
}
}
示例13: testAddRotatedFlippedImage
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/39364197/how-to-rotate-around-the-image-center-by-itext">
* How to rotate around the image center by itext?
* </a>
* <p>
* This test draws an image at given coordinates without rotation and then again
* as if that image was flipped and rotated around its center by some angle.
* </p>
*/
@Test
public void testAddRotatedFlippedImage() throws IOException, DocumentException
{
try ( FileOutputStream stream = new FileOutputStream(new File(RESULT_FOLDER, "rotatedFlippedImage.pdf")) )
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, stream);
document.open();
PdfContentByte contentByte = writer.getDirectContent();
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;
try (InputStream imageStream = getClass().getResourceAsStream("/mkl/testarea/itext5/layer/Willi-1.jpg"))
{
Image image = Image.getInstance(IOUtils.toByteArray(imageStream));
// Draw image at x,y without rotation
contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);
// Draw image as if the previous image was flipped and rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Flip it horizontally
AffineTransform B = new AffineTransform(-1, 0, 0, 1, 0, 0);
// Stretch it to its dimensions
AffineTransform C = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform D = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform E = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
M.preConcatenate(E);
//Draw
contentByte.addImage(image, M);
}
document.close();
}
}
示例14: onEndPage
import com.itextpdf.text.pdf.PdfContentByte; //导入方法依赖的package包/类
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.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);
}
cb.saveState();
// draw a Rectangle around the page
cb.setColorStroke(BaseColor.ORANGE);
cb.setLineWidth(2);
cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
cb.stroke();
cb.restoreState();
// starting on page 3, a watermark with an Image that is made transparent
if (writer.getPageNumber() >= 3) {
cb.setGState(gstate);
cb.setColorFill(BaseColor.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();
}