本文整理匯總了Java中com.itextpdf.text.pdf.PdfContentByte.moveText方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfContentByte.moveText方法的具體用法?Java PdfContentByte.moveText怎麽用?Java PdfContentByte.moveText使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.itextpdf.text.pdf.PdfContentByte
的用法示例。
在下文中一共展示了PdfContentByte.moveText方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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());
}
}
}
示例2: 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();
}