本文整理匯總了Java中com.itextpdf.text.pdf.PdfContentByte.endText方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfContentByte.endText方法的具體用法?Java PdfContentByte.endText怎麽用?Java PdfContentByte.endText使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.itextpdf.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.endText方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: onEndPage
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
@Override
public void onEndPage(final PdfWriter writer,
final Document document) {
final PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// compose the footer
final String text = "Page "
+ writer.getPageNumber() + " of ";
final float textSize = _headerFooterFont.getWidthPoint(text, 12);
final float textBase = document.bottom() - 20;
cb.beginText();
cb.setFontAndSize(_headerFooterFont, 12);
final float adjust = _headerFooterFont.getWidthPoint("0", 12);
cb.setTextMatrix(document.right()
- textSize - adjust, textBase);
cb.showText(text);
cb.endText();
cb.addTemplate(_tpl, document.right()
- adjust, textBase);
cb.restoreState();
}
示例3: stampTextOriginal
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* The OP's original code transformed into Java
*/
void stampTextOriginal(InputStream source, OutputStream target) throws DocumentException, IOException
{
Date today = new Date();
PdfReader reader = new PdfReader(source);
PdfStamper stamper = new PdfStamper(reader, target);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
int tSize = 24;
String mark = "DRAFT " + today;
int angle = 45;
float height = reader.getPageSizeWithRotation(1).getHeight()/2;
float width = reader.getPageSizeWithRotation(1).getWidth()/2;
PdfContentByte cb = stamper.getOverContent(1);
cb.setColorFill(new BaseColor(255,200,200));
cb.setFontAndSize(bf, tSize);
cb.beginText();
cb.showTextAligned(Element.ALIGN_CENTER, mark, width, height, angle);
cb.endText();
stamper.close();
reader.close();
}
示例4: stampTextChanged
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* The OP's code transformed into Java changed with the work-around.
*/
void stampTextChanged(InputStream source, OutputStream target) throws DocumentException, IOException
{
Date today = new Date();
PdfReader reader = new PdfReader(source);
PdfStamper stamper = new PdfStamper(reader, target);
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
int tSize = 24;
String mark = "DRAFT " + today;
int angle = 45;
float height = reader.getPageSizeWithRotation(1).getHeight()/2;
float width = reader.getPageSizeWithRotation(1).getWidth()/2;
PdfContentByte cb = stamper.getOverContent(1);
cb.setFontAndSize(bf, tSize);
cb.beginText();
cb.setColorFill(new BaseColor(255,200,200));
cb.showTextAligned(Element.ALIGN_CENTER, mark, width, height, angle);
cb.endText();
stamper.close();
reader.close();
}
示例5: onStartPage
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onStartPage(PdfWriter writer, Document document) {
if (writer.getPageNumber() < 3) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.saveState();
cb.setColorFill(BaseColor.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();
}
}
示例6: onEndPage
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
@Override
public void onEndPage(final PdfWriter writer,
final Document document) {
final PdfContentByte cb = writer.getDirectContent();
cb.saveState();
// compose the footer
final float textSize = _headerFooterFont.getWidthPoint(_legendText, 12);
final float textBase = document.bottom()
- 20;
cb.beginText();
cb.setFontAndSize(_headerFooterFont, 12);
final float adjust = _headerFooterFont.getWidthPoint("0", 12);
cb.setTextMatrix(document.right()
- textSize
- adjust, textBase);
cb.showText(_legendText);
cb.endText();
cb.addTemplate(_tpl, document.right()
- adjust, textBase);
cb.restoreState();
}
示例7: addImageToPdf
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* The <code>addImageToPdf</code> method is used to add image to pdf and make it searchable by adding image text in invisible mode
* w.r.t parameter 'isPdfSearchable' passed.
*
* @param pdfWriter {@link PdfWriter} writer of pdf in which image has to be added
* @param htmlUrl {@link HocrPage} corresponding html file for fetching text and coordinates
* @param imageUrl {@link String} url of image to be added in pdf
* @param isPdfSearchable true for searchable pdf else otherwise
* @param widthOfLine
*/
private void addImageToPdf(PdfWriter pdfWriter, HocrPage hocrPage, String imageUrl, boolean isPdfSearchable, final int widthOfLine) {
if (null != pdfWriter && null != imageUrl && imageUrl.length() > 0) {
try {
LOGGER.info("Adding image" + imageUrl + " to pdf using iText");
Image pageImage = Image.getInstance(imageUrl);
float dotsPerPointX = pageImage.getDpiX() / PDF_RESOLUTION;
float dotsPerPointY = pageImage.getDpiY() / PDF_RESOLUTION;
PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY);
pageImage.setAbsolutePosition(0, 0);
// Add image to pdf
pdfWriter.getDirectContentUnder().addImage(pageImage);
pdfWriter.getDirectContentUnder().add(pdfContentByte);
// If pdf is to be made searchable
if (isPdfSearchable) {
LOGGER.info("Adding invisible text for image: " + imageUrl);
float pageImagePixelHeight = pageImage.getHeight();
Font defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK);
// Fetch text and coordinates for image to be added
Map<String, int[]> textCoordinatesMap = getTextWithCoordinatesMap(hocrPage, widthOfLine);
Set<String> ketSet = textCoordinatesMap.keySet();
// Add text at specific location
for (String key : ketSet) {
int[] coordinates = textCoordinatesMap.get(key);
float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX;
float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY;
pdfContentByte.beginText();
// To make text added as invisible
pdfContentByte.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
pdfContentByte.setLineWidth(Math.round(bboxWidthPt));
// Ceil is used so that minimum font of any text is 1
// For exception of unbalanced beginText() and endText()
if (bboxHeightPt > 0.0) {
pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), (float) Math.ceil(bboxHeightPt));
} else {
pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), 1);
}
float xCoordinate = (float) (coordinates[0] / dotsPerPointX);
float yCoordinate = (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY);
pdfContentByte.moveText(xCoordinate, yCoordinate);
pdfContentByte.showText(key);
pdfContentByte.endText();
}
}
pdfContentByte.closePath();
} catch (BadElementException badElementException) {
LOGGER
.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
+ badElementException.toString());
} catch (DocumentException documentException) {
LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + documentException.toString());
} catch (MalformedURLException malformedURLException) {
LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
+ malformedURLException.toString());
} catch (IOException ioException) {
LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + ioException.toString());
}
}
}
示例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: createFile
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
private void createFile(String finalPDF, ByteArrayOutputStream outputStream) throws FileNotFoundException, IOException
{
if (outputStream == null || finalPDF == null)
return;
FileOutputStream fos = new FileOutputStream (new File(finalPDF));
Document document = new Document();
try
{
fos.write(outputStream.toByteArray());
fos.close();
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "This is a Draft Document, its contents might still change", 520, 5, 0);
cb.endText();
document.close();
}
catch (Exception e)
{
engine.createSystemLogEntry(SystemLogType.REPORTS, SystemLogLevel.ERROR, "Error Converting DRAFT Document into PDF");
}
finally
{
fos.close();
if (document.isOpen()) {
document.close();
}
}
}
示例10: createFile
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
public static void createFile(String finalPDF, ByteArrayOutputStream outputStream, UIEngine engine) throws FileNotFoundException, IOException
{
if (outputStream == null || finalPDF == null)
return;
FileOutputStream fos = new FileOutputStream (new File(finalPDF));
Document document = new Document();
try
{
fos.write(outputStream.toByteArray());
fos.close();
PdfWriter writer = PdfWriter.getInstance(document, fos);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "This is a Draft Document, its contents might still change", 520, 5, 0);
cb.endText();
document.close();
}
catch (Exception e)
{
engine.createSystemLogEntry(SystemLogType.REPORTS, SystemLogLevel.ERROR, "Error Converting DRAFT Document into PDF");
}
finally
{
fos.close();
if (document.isOpen()) {
document.close();
}
}
}
示例11: addText
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
private void addText(String text, int x, int y, BaseFont font,int fontSize,PdfWriter writer) throws DocumentException, IOException {
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.beginText();
cb.setFontAndSize(font, fontSize);
float n = 0;
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, x, y, n);
cb.endText();
cb.restoreState();
}
示例12: applyWatermarkOnAllPages
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
public static void applyWatermarkOnAllPages(
PdfReader reader, PdfStamper stamper, String text, BaseColor color, float opacity
) throws DocumentException, IOException {
int numberOfPages = reader.getNumberOfPages();
PdfGState gs = new PdfGState();
gs.setFillOpacity(opacity);
for (int i = 1; i <= numberOfPages; i++) {
Rectangle pageSize = reader.getPageSize(i);
int pageRotation = reader.getPageRotation(i);
boolean isLandscape = false;
if(pageRotation > 0){
isLandscape = true;
}
int pageDiagonalSize = getDiagonalSize(pageSize);
int watermarkAngle = getDiagonalAngle(pageSize, pageDiagonalSize, isLandscape);
LinkedHashSet<Integer> fontSizeSet = new LinkedHashSet<Integer>();
float fontSize = determineFontSize(pageDiagonalSize, a4WatermarkFontSize, fontSizeSet);
fontSize = fontSize - 1;
int cornerPlacement = getCornerPlacementDiagonal(fontSize);
PdfContentByte underContent = stamper.getUnderContent(i);
underContent.setGState(gs);
underContent.setFontAndSize(getWaterkmarkFont(), fontSize);
underContent.setColorFill(color);
underContent.beginText();
underContent.showTextAligned(
Element.ALIGN_BASELINE, text,
cornerPlacement/2,
cornerPlacement/2,
watermarkAngle
);
underContent.endText();
}
}
示例13: testCreateLinkWithAppearance
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/34734669/define-background-color-and-transparency-of-link-annotation-in-pdf">
* Define background color and transparency of link annotation in PDF
* </a>
* <p>
* This test creates a link annotation with custom appearance. Adobe Reader chooses
* to ignore it but other viewers use it. Interestingly Adobe Acrobat export-as-image
* does use the custom appearance...
* </p>
*/
@Test
public void testCreateLinkWithAppearance() throws IOException, DocumentException
{
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(RESULT_FOLDER, "custom-link.appearance.pdf")));
writer.setCompressionLevel(0);
doc.open();
BaseFont baseFont = BaseFont.createFont();
int fontSize = 15;
doc.add(new Paragraph("Hello", new Font(baseFont, fontSize)));
PdfContentByte content = writer.getDirectContent();
String text = "Test";
content.setFontAndSize(baseFont, fontSize);
content.beginText();
content.moveText(100, 500);
content.showText(text);
content.endText();
Rectangle linkLocation = new Rectangle(95, 495 + baseFont.getDescentPoint(text, fontSize),
105 + baseFont.getWidthPoint(text, fontSize), 505 + baseFont.getAscentPoint(text, fontSize));
PdfAnnotation linkGreen = PdfAnnotation.createLink(writer, linkLocation, PdfName.HIGHLIGHT, "green" );
PdfTemplate appearance = PdfTemplate.createTemplate(writer, linkLocation.getWidth(), linkLocation.getHeight());
PdfGState state = new PdfGState();
//state.FillOpacity = .3f;
// IMPROVEMENT: Use blend mode Darken instead of transparency; you may also want to try Multiply.
state.setBlendMode(new PdfName("Darken"));
appearance.setGState(state);
appearance.setColorFill(BaseColor.GREEN);
appearance.rectangle(0, 0, linkLocation.getWidth(), linkLocation.getHeight());
appearance.fill();
linkGreen.setAppearance(PdfName.N, appearance);
writer.addAnnotation(linkGreen);
doc.open();
doc.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();
}
示例15: writeAlignedText
import com.itextpdf.text.pdf.PdfContentByte; //導入方法依賴的package包/類
/**
* Writes text watermark to one of the 5 preconfigured locations
*
* @param pcb
* @param r
* @param tokens
* @param size
* @param position
*/
private void writeAlignedText(PdfContentByte pcb, Rectangle r, Vector<String> tokens, float size, String position)
{
// get the dimensions of our 'rectangle' for text
float height = size * tokens.size();
float width = 0;
float centerX = 0, startY = 0;
for (int i = 0; i < tokens.size(); i++)
{
if (pcb.getEffectiveStringWidth(tokens.get(i), false) > width)
{
width = pcb.getEffectiveStringWidth(tokens.get(i), false);
}
}
// now that we have the width and height, we can calculate the center
// position for
// the rectangle that will contain our text.
if (position.equals(POSITION_BOTTOMLEFT))
{
centerX = width / 2 + PAD;
startY = 0 + PAD + height;
}
else if (position.equals(POSITION_BOTTOMRIGHT))
{
centerX = r.getWidth() - (width / 2) - PAD;
startY = 0 + PAD + height;
}
else if (position.equals(POSITION_TOPLEFT))
{
centerX = width / 2 + PAD;
startY = r.getHeight() - (PAD * 2);
}
else if (position.equals(POSITION_TOPRIGHT))
{
centerX = r.getWidth() - (width / 2) - PAD;
startY = r.getHeight() - (PAD * 2);
}
else if (position.equals(POSITION_CENTER))
{
centerX = r.getWidth() / 2;
startY = (r.getHeight() / 2) + (height / 2);
}
// apply text to PDF
pcb.beginText();
for (int t = 0; t < tokens.size(); t++)
{
pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, tokens.get(t), centerX, startY - (size * t), 0);
}
pcb.endText();
}