本文整理匯總了Java中org.apache.pdfbox.pdmodel.font.PDFont類的典型用法代碼示例。如果您正苦於以下問題:Java PDFont類的具體用法?Java PDFont怎麽用?Java PDFont使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PDFont類屬於org.apache.pdfbox.pdmodel.font包,在下文中一共展示了PDFont類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addHeaderPJ
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
/**
* @param textHeader
* @param font
* @param PAGE_SIZE_A4
* @param contentStream
* @return ajoute un header a la piece
* @throws IOException
*/
private Float addHeaderPJ(final String textHeader, final PDFont font, final PDRectangle PAGE_SIZE_A4,
final PDPageContentStream contentStream) throws IOException {
Float marginTop = 0f;
// si font Ok, on ajoute le text
if (font != null && ConstanteUtils.DOSSIER_ADD_HEADER_IMG) {
// calcul de la largeur et hauteur du txt
Float titleWidth = font.getStringWidth(textHeader) / 1000 * ConstanteUtils.DOSSIER_FONT_SIZE;
Float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000
* ConstanteUtils.DOSSIER_FONT_SIZE;
// calcul de la marge du haut : hauteur du text + marge
marginTop = titleHeight + ConstanteUtils.DOSSIER_MARGIN;
// calcul de la position du text
Float xText = (PAGE_SIZE_A4.getWidth() - 2 * ConstanteUtils.DOSSIER_MARGIN - titleWidth) / 2;
Float yText = PAGE_SIZE_A4.getHeight() - marginTop;
// ecriture du text
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, ConstanteUtils.DOSSIER_FONT_SIZE);
contentStream.newLineAtOffset(xText, yText);
contentStream.showText(textHeader);
contentStream.endText();
}
return marginTop;
}
示例2: showTextOnStream
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
private void showTextOnStream(IFontTextDrawerEnv env, PDPageContentStream contentStream, Font attributeFont,
PDFont font, boolean isStrikeThrough, boolean isUnderline, boolean isLigatures, String text)
throws IOException {
if (isStrikeThrough || isUnderline) {
// noinspection unused
float stringWidth = font.getStringWidth(text);
// noinspection unused
LineMetrics lineMetrics = attributeFont.getLineMetrics(text, env.getFontRenderContext());
/*
* TODO: We can not draw that yet, we must do that later. While in textmode its
* not possible to draw lines...
*/
}
// noinspection StatementWithEmptyBody
if (isLigatures) {
/*
* No idea how to map this ...
*/
}
contentStream.showText(text);
}
示例3: generateBillHeader
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
private void generateBillHeader(PDPage firstPage, PDPageContentStream contentStream)
throws IOException {
// Add header text
PDFont currentFont;
int currentFontSize;
String headerLine1 = "Git Rekt Resort";
String headerLine2 = "Customer Bill";
contentStream.setLeading(10);
currentFont = BOLD;
currentFontSize = 14;
contentStream.setFont(currentFont, currentFontSize);
contentStream.beginText();
float offsetX = getCenteredTextXPos(firstPage, headerLine1, currentFont, currentFontSize);
contentStream.newLineAtOffset(offsetX, 750f);
contentStream.showText(headerLine1);
currentFont = PDType1Font.COURIER_BOLD;
currentFontSize = 12;
contentStream.setFont(currentFont, currentFontSize);
float offsetX2 = getCenteredTextXPos(firstPage, headerLine2, currentFont, currentFontSize);
contentStream.newLineAtOffset(-offsetX + offsetX2, -5f);
contentStream.newLine();
contentStream.showText(headerLine2);
contentStream.endText();
}
示例4: getCenteredTextXPos
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
private float getCenteredTextXPos(PDPage page, String text, PDFont font, int fontSize)
throws IOException {
float textWidth = getStringWidth(text, font, fontSize);
PDRectangle pageSize = page.getMediaBox();
float pageCenterX = pageSize.getWidth() / 2F;
float textX = pageCenterX - textWidth/2F;
return textX;
}
示例5: recognizeWithFont
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
private static boolean recognizeWithFont(
@NotNull String[] keywords,
@NotNull byte[] inputText,
@NotNull Set<PDFont> pdFonts,
@NotNull boolean useStrict) {
String encodedInput = generateByteString(inputText);
for (PDFont f : pdFonts) {
if (f == null) continue;
for (String k : keywords) {
try {
byte[] encodedKeywordBytes = f.encode(k);
final String encodedKeyword = generateByteString(encodedKeywordBytes);
if (checkDuplicate(encodedInput, encodedKeyword, useStrict)) return true;
} catch (IOException | IllegalArgumentException ignored) {
}
}
}
return false;
}
示例6: chooseMatchingTimes
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
/**
* Get a PDType1Font.TIMES-variant, which matches the given font
*
* @param font
* Font to get the styles from
* @return a PDFont Times variant which matches the style in the given Font
* object.
*/
public static PDFont chooseMatchingTimes(Font font) {
if ((font.getStyle() & (Font.ITALIC | Font.BOLD)) == (Font.ITALIC | Font.BOLD))
return PDType1Font.TIMES_BOLD_ITALIC;
if ((font.getStyle() & Font.ITALIC) == Font.ITALIC)
return PDType1Font.TIMES_ITALIC;
if ((font.getStyle() & Font.BOLD) == Font.BOLD)
return PDType1Font.TIMES_BOLD;
return PDType1Font.TIMES_ROMAN;
}
示例7: chooseMatchingCourier
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
/**
* Get a PDType1Font.COURIER-variant, which matches the given font
*
* @param font
* Font to get the styles from
* @return a PDFont Courier variant which matches the style in the given Font
* object.
*/
public static PDFont chooseMatchingCourier(Font font) {
if ((font.getStyle() & (Font.ITALIC | Font.BOLD)) == (Font.ITALIC | Font.BOLD))
return PDType1Font.COURIER_BOLD_OBLIQUE;
if ((font.getStyle() & Font.ITALIC) == Font.ITALIC)
return PDType1Font.COURIER_OBLIQUE;
if ((font.getStyle() & Font.BOLD) == Font.BOLD)
return PDType1Font.COURIER_BOLD;
return PDType1Font.COURIER;
}
示例8: chooseMatchingHelvetica
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
/**
* Get a PDType1Font.HELVETICA-variant, which matches the given font
*
* @param font
* Font to get the styles from
* @return a PDFont Helvetica variant which matches the style in the given Font
* object.
*/
public static PDFont chooseMatchingHelvetica(Font font) {
if ((font.getStyle() & (Font.ITALIC | Font.BOLD)) == (Font.ITALIC | Font.BOLD))
return PDType1Font.HELVETICA_BOLD_OBLIQUE;
if ((font.getStyle() & Font.ITALIC) == Font.ITALIC)
return PDType1Font.HELVETICA_OBLIQUE;
if ((font.getStyle() & Font.BOLD) == Font.BOLD)
return PDType1Font.HELVETICA_BOLD;
return PDType1Font.HELVETICA;
}
示例9: exportGraphic
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
try {
PDDocument document = new PDDocument();
PDFont pdArial = PDFontFactory.createDefaultFont();
File parentDir = new File("target/test/" + dir);
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D imageGraphics = image.createGraphics();
exporter.draw(imageGraphics);
imageGraphics.dispose();
ImageIO.write(image, "PNG", new File(parentDir, name + ".png"));
for (Mode m : Mode.values()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 400, 400);
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
contentStream.beginText();
contentStream.setStrokingColor(0, 0, 0);
contentStream.setNonStrokingColor(0, 0, 0);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
contentStream.showText("Mode " + m);
contentStream.endText();
switch (m) {
case FontTextIfPossible:
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
case DefaultFontText: {
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
}
case ForceFontText:
fontTextDrawer = new PdfBoxGraphics2DFontTextForcedDrawer();
fontTextDrawer.registerFont(
PdfBoxGraphics2DTestBase.class.getResourceAsStream("DejaVuSerifCondensed.ttf"));
fontTextDrawer.registerFont("Arial", pdArial);
break;
case DefaultVectorized:
default:
break;
}
if (fontTextDrawer != null) {
pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
}
exporter.draw(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 20);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
matrix.scale(1.5f, 1.5f);
matrix.translate(0, 100);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
}
document.save(new File(parentDir, name + ".pdf"));
document.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例10: getCharacterCount
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
/**
* This return the character count of a given text.
* @param requiredSize
* @param subString
* @param pdfFont
* @return
* @throws IOException
*/
private static int getCharacterCount(float requiredSize, String subString, PDFont pdfFont) throws IOException {
double factor = 0.95;
String string = subString;
while (pdfFont.getStringWidth(string) > requiredSize) {
string = string.substring(0, (int) Math.round(string.length()*factor));
}
return string.length();
}
示例11: getLines
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
/**
* Method to separate lines of the PDF.
* @param text
* @param fontSize
* @param pdfFont
* @param width
* @return
* @throws IOException
*/
private static List<String> getLines(String text, float fontSize, PDFont pdfFont, float width)
throws IOException {
width = width - 150 ;
java.util.List<String> lines = new ArrayList<String>();
int lastSpace = -1;
while (text.length() > 0) {
int spaceIndex = text.indexOf(' ', lastSpace + 1);
if (spaceIndex < 0)
spaceIndex = text.length();
String subString = text.substring(0, spaceIndex);
float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
if (size > width) {
float requiredSize = (width * 1000)/fontSize;
int characterSize = getCharacterCount(requiredSize, subString, pdfFont);
//if (lastSpace < 0)
lastSpace = characterSize;
subString = text.substring(0, lastSpace);
lines.add(subString);
text = text.substring(lastSpace).trim();
lastSpace = -1;
} else if (spaceIndex == text.length()) {
lines.add(text);
text = "";
} else {
lastSpace = spaceIndex;
}
}
return lines;
}
示例12: loadFont
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
public static PDFont loadFont(String fontFileName, PDDocument doc, PDFont defaultBaseFont) throws IOException {
// public static PDFont loadFont(String fontFileName, PDDocument doc, PDFont defaultBaseFont, PDStream pdStream) throws IOException {
PDFont font = null;
if (fontFileName != null && fontFileName.length() > 0) {
font = PDType1Font.getStandardFont(fontFileName);
if (font == null) {
font = PDTrueTypeFont.loadTTF(doc, fontFileName);
// http://stackoverflow.com/questions/5570225/workaround-for-pdfbox-pdtruetypefont-bad-widths-bug
}
} else {
font = defaultBaseFont;
}
// if (font != null && pdStream != null) {
// if (font instanceof PDType1Font) {
// ((PDType1Font) font).setToUnicode(pdStream.getCOSObject());
// } else if (font instanceof PDTrueTypeFont) {
// ((PDTrueTypeFont) font).setToUnicode(pdStream.getCOSObject());
// }
// }
return font;
}
示例13: recognize
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
static boolean recognize(@NotNull String[] keywords,
@NotNull byte[] inputText,
@NotNull Set<PDFont> pdFonts,
@NotNull boolean useStrict) {
return recognizePlain(keywords, inputText, useStrict) ||
recognizeWithFont(keywords, inputText, pdFonts, useStrict);
}
示例14: mapFont
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
@Override
protected PDFont mapFont(Font font, IFontTextDrawerEnv env) throws IOException, FontFormatException {
PDFont pdFont = mapDefaultFonts(font);
if (pdFont != null)
return pdFont;
/*
* Do we have a manual registered mapping with a font file?
*/
pdFont = super.mapFont(font, env);
if (pdFont != null)
return pdFont;
return chooseMatchingHelvetica(font);
}
示例15: findFallbackFont
import org.apache.pdfbox.pdmodel.font.PDFont; //導入依賴的package包/類
private PDFont findFallbackFont(IFontTextDrawerEnv env) throws IOException {
/*
* We search for the right font in the system folders... We try to use
* LucidaSansRegular and if not found Arial, because this fonts often exists. We
* use the Java default font as fallback.
*
* Normally this method is only used and called if a default font misses some
* special characters, e.g. Hebrew or Arabic characters.
*/
String javaHome = System.getProperty("java.home", ".");
String javaFontDir = javaHome + "/lib/fonts";
String windir = System.getenv("WINDIR");
if (windir == null)
windir = javaFontDir;
File[] paths = new File[] { new File(new File(windir), "fonts"), new File(System.getProperty("user.dir", ".")),
new File("/Library/Fonts"), new File("/usr/share/fonts/truetype"), new File(javaFontDir) };
File foundFontFile = null;
for (String fontFileName : new String[] { "LucidaSansRegular.ttf", "arial.ttf", "Arial.ttf" }) {
for (File path : paths) {
File arialFile = new File(path, fontFileName);
if (arialFile.exists()) {
foundFontFile = arialFile;
break;
}
}
if (foundFontFile != null)
break;
}
return PDType0Font.load(env.getDocument(), foundFontFile);
}