本文整理汇总了Java中com.itextpdf.text.pdf.PdfContentByte.showText方法的典型用法代码示例。如果您正苦于以下问题:Java PdfContentByte.showText方法的具体用法?Java PdfContentByte.showText怎么用?Java PdfContentByte.showText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.showText方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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();
}
示例4: 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());
}
}
}
示例5: 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();
}
示例6: 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();
}
示例7: 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();
}