本文整理匯總了Java中com.itextpdf.text.pdf.BaseFont.createFont方法的典型用法代碼示例。如果您正苦於以下問題:Java BaseFont.createFont方法的具體用法?Java BaseFont.createFont怎麽用?Java BaseFont.createFont使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.itextpdf.text.pdf.BaseFont
的用法示例。
在下文中一共展示了BaseFont.createFont方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testCreateUnicodePdf
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/35082653/adobe-reader-cant-display-unicode-font-of-pdf-added-with-itext">
* Adobe Reader can't display unicode font of pdf added with iText
* </a>
* <p>
* Indeed, just like in the iTextSharp version of the code, the resulting file has
* issues in Adobe Reader, cf. {@link #testAddUnicodeStampSampleOriginal()}. With
* a different starting file, though, it doesn't, cf.
* {@link #testAddUnicodeStampEg_01()}. This test creates a new PDF with the same
* font and chunk as stamped by the OP. Adobe Reader has no problem with it either.
* </p>
* <p>
* As it eventually turns out, Adobe Reader treats PDF files with composite fonts
* differently if they claim to be PDF-1.2 like the OP's sample file.
* </p>
*/
@Test
public void testCreateUnicodePdf() throws DocumentException, IOException
{
Document document = new Document();
try ( OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "unicodePdf.pdf")) )
{
PdfWriter.getInstance(document, result);
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
document.open();
Phrase p = new Phrase();
p.setFont(new Font(bf, 25, Font.NORMAL, BaseColor.BLUE));
p.add("Sample Text");
document.add(p);
document.close();
}
}
示例2: getIdentityFont
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
private BaseFont getIdentityFont(String fontPath,ApplicationContext applicationContext) throws DocumentException,IOException {
if(!fontPath.startsWith(ApplicationContext.CLASSPATH_URL_PREFIX)){
fontPath=ApplicationContext.CLASSPATH_URL_PREFIX+fontPath;
}
String fontName = fontPath;
int lastSlashPos=fontPath.lastIndexOf("/");
if(lastSlashPos!=-1){
fontName = fontPath.substring(lastSlashPos+1,fontPath.length());
}
if (fontName.toLowerCase().endsWith(".ttc")) {
fontName = fontName + ",0";
}
InputStream inputStream=null;
try{
inputStream=applicationContext.getResource(fontPath).getInputStream();
byte[] bytes = IOUtils.toByteArray(inputStream);
BaseFont baseFont = BaseFont.createFont(fontName, BaseFont.IDENTITY_H,BaseFont.EMBEDDED,true,bytes,null);
baseFont.setSubset(true);
return baseFont;
}finally{
if(inputStream!=null)inputStream.close();
}
}
示例3: writeSimplePdf
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
public static void writeSimplePdf() throws Exception{
//1.新建document對象
//第一個參數是頁麵大小。接下來的參數分別是左、右、上和下頁邊距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//2.建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。
//創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\Documents\\ITextTest.pdf"));
//3.打開文檔
document.open();
//4.向文檔中添加內容
//通過 com.lowagie.text.Paragraph 來添加文本。可以用文本及其默認的字體、顏色、大小等等設置來創建一個默認段落
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 22, Font.BOLD, BaseColor.BLACK);
document.add(new Paragraph("sdfsdfsd全是中文顯示了沒.fsdfsfs",fontChinese));
document.add(new Paragraph("Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new BaseColor(255, 150, 200))));
Paragraph pragraph=new Paragraph("你這裏有中亠好", fontChinese);
document.add(pragraph);
//圖像支持格式 GIF, Jpeg, PNG, wmf
Image gif = Image.getInstance("F:/keyworkspace/survey/WebRoot/images/logo/snlogo.png");
gif.setBorder(5);
gif.scaleAbsolute(30,30);
gif.setAlignment(Image.RIGHT|Image.TEXTWRAP);
document.add(gif);
Paragraph pragraph11=new Paragraph("你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好", fontChinese);
document.add(pragraph11);
Image gif15 = Image.getInstance("F:/keyworkspace/survey/WebRoot/images/logo/snlogo.png");
// gif15.setBorder(50);
gif15.setBorder(Image.BOX);
gif15.setBorderColor(BaseColor.RED);
// gif15.setBorderColorBottom(borderColorBottom)
gif15.setBorderWidth(1);
gif15.scalePercent(50);
document.add(gif15);
//5.關閉文檔
document.close();
}
示例4: testShowTextAlignedVsSimpleColumnTopAlignment
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/32162759/columntext-showtextaligned-vs-columntext-setsimplecolumn-top-alignment">
* ColumnText.ShowTextAligned vs ColumnText.SetSimpleColumn Top Alignment
* </a>
* <p>
* Indeed, the coordinates do not line up. The y coordinate of
* {@link ColumnText#showTextAligned(PdfContentByte, int, Phrase, float, float, float)}
* denotes the baseline while {@link ColumnText#setSimpleColumn(Rectangle)} surrounds
* the text to come.
* </p>
*/
@Test
public void testShowTextAlignedVsSimpleColumnTopAlignment() throws DocumentException, IOException
{
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "ColumnTextTopAligned.pdf")));
document.open();
Font fontQouteItems = new Font(BaseFont.createFont(), 12);
PdfContentByte canvas = writer.getDirectContent();
// Item Number
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("36222-0", fontQouteItems), 60, 450, 0);
// Estimated Qty
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("47", fontQouteItems), 143, 450, 0);
// Item Description
ColumnText ct = new ColumnText(canvas); // Uses a simple column box to provide proper text wrapping
ct.setSimpleColumn(new Rectangle(193, 070, 390, 450));
ct.setText(new Phrase("In-Situ : Poly Cable - 100'\nPoly vented rugged black gable 100ft\nThis is an additional description. It can wrap an extra line if it needs to so this text is long.", fontQouteItems));
ct.go();
document.close();
}
示例5: makePDF
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
private static void makePDF(Bitmap bmp, File file) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
document.addAuthor(FullscreenActivity.mAuthor.toString());
document.addTitle(FullscreenActivity.mTitle.toString());
document.addCreator("OpenSongApp");
if (bmp!=null && bmp.getWidth()>bmp.getHeight()) {
document.setPageSize(PageSize.A4.rotate());
} else {
document.setPageSize(PageSize.A4);
}
document.addTitle(FullscreenActivity.mTitle.toString());
document.open();//document.add(new Header("Song title",FullscreenActivity.mTitle.toString()));
BaseFont urName = BaseFont.createFont("assets/fonts/Lato-Reg.ttf", "UTF-8",BaseFont.EMBEDDED);
Font TitleFontName = new Font(urName, 14);
Font AuthorFontName = new Font(urName, 10);
document.add(new Paragraph(FullscreenActivity.mTitle.toString(),TitleFontName));
document.add(new Paragraph(FullscreenActivity.mAuthor.toString(),AuthorFontName));
addImage(document,bmp);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例6: stampTextOriginal
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的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();
}
示例7: stampTextChanged
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的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();
}
示例8: testAddUnicodeStampSampleOriginal
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/35082653/adobe-reader-cant-display-unicode-font-of-pdf-added-with-itext">
* Adobe Reader can't display unicode font of pdf added with iText
* </a>
* <br/>
* <a href="https://www.dropbox.com/s/erkv9wot9d460dg/sampleOriginal.pdf?dl=0">
* sampleOriginal.pdf
* </a>
* <p>
* Indeed, just like in the iTextSharp version of the code, the resulting file has
* issues in Adobe Reader. With a different starting file, though, it doesn't, cf.
* {@link #testAddUnicodeStampEg_01()}.
* </p>
* <p>
* As it eventually turns out, Adobe Reader treats PDF files with composite fonts
* differently if they claim to be PDF-1.2 like the OP's sample file.
* </p>
*/
@Test
public void testAddUnicodeStampSampleOriginal() throws DocumentException, IOException
{
try ( InputStream resource = getClass().getResourceAsStream("sampleOriginal.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "sampleOriginal-unicodeStamp.pdf")) )
{
PdfReader reader = new PdfReader(resource);
PdfStamper stamper = new PdfStamper(reader, result);
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfContentByte cb = stamper.getOverContent(1);
Phrase p = new Phrase();
p.setFont(new Font(bf, 25, Font.NORMAL, BaseColor.BLUE));
p.add("Sample Text");
ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_LEFT, p, 200, 200, 0);
stamper.close();
}
}
示例9: testAddUnicodeStampEg_01
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/35082653/adobe-reader-cant-display-unicode-font-of-pdf-added-with-itext">
* Adobe Reader can't display unicode font of pdf added with iText
* </a>
* <br/>
* <a href="https://www.dropbox.com/s/erkv9wot9d460dg/sampleOriginal.pdf?dl=0">
* sampleOriginal.pdf
* </a>
* <p>
* Indeed, just like in the iTextSharp version of the code, the resulting file has
* issues in Adobe Reader, cf. {@link #testAddUnicodeStampSampleOriginal()}. With
* a different starting file, though, it doesn't as this test shows.
* </p>
* <p>
* As it eventually turns out, Adobe Reader treats PDF files with composite fonts
* differently if they claim to be PDF-1.2 like the OP's sample file.
* </p>
*/
@Test
public void testAddUnicodeStampEg_01() throws DocumentException, IOException
{
try ( InputStream resource = getClass().getResourceAsStream("eg_01.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "eg_01-unicodeStamp.pdf")) )
{
PdfReader reader = new PdfReader(resource);
PdfStamper stamper = new PdfStamper(reader, result);
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfContentByte cb = stamper.getOverContent(1);
Phrase p = new Phrase();
p.setFont(new Font(bf, 25, Font.NORMAL, BaseColor.BLUE));
p.add("Sample Text");
ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_LEFT, p, 200, 200, 0);
stamper.close();
}
}
示例10: addCellToTableCzech
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/44005834/changing-rowspans">
* Changing rowspans
* </a>
* <p>
* Helper method of the OP.
* </p>
* @see #testUseRowspanLikeUser7968180()
* @see #testUseRowspanLikeUser7968180Fixed()
* @see #createPdf(String)
* @see #createPdfFixed(String)
*/
private static void addCellToTableCzech(PdfPTable table, int horizontalAlignment,
int verticalAlignment, String value, int colspan, int rowspan,
String fontType, float fontSize) {
BaseFont base = null;
try {
base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(base, fontSize);
PdfPCell cell = new PdfPCell(new Phrase(value, font));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(verticalAlignment);
cell.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell);
}
示例11: generateFooter
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
public PdfPTable generateFooter() {
try {
BaseFont baseFont = BaseFont.createFont(/*"resources/ARIAL.TTF"*/ "c:/Windows/Fonts/arial.ttf", BaseFont.IDENTITY_H, true);
footerTable = new PdfPTable(1);
footerTable.setTotalWidth(440);
footerTable.setLockedWidth(true);
Font pageNumberFont = new Font(baseFont, 9, Font.BOLD);
Paragraph pageNumberP = new Paragraph(titleIndex+"-"+ pageNumber, pageNumberFont);
PdfPCell pageNumberCell = new PdfPCell(pageNumberP);
pageNumberCell.setBorder(0);
pageNumberCell.setPaddingTop(20);
footerTable.addCell(pageNumberCell);
} catch (Exception e) {
e.printStackTrace();
}
return footerTable;
}
示例12: PDFFont
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
public PDFFont(float size, Style style, RGBColor color) throws HTDocumentException {
try {
BaseFont baseFont = (
BaseFont.createFont("fonts/ttf/dejavu/DejaVuSans.ttf", "cp1251", BaseFont.EMBEDDED)
);
int iStyle;
switch (style) {
case BOLD:
iStyle = Font.BOLD;
break;
case ITALIC:
iStyle = Font.ITALIC;
break;
case UNDERLINE:
iStyle = Font.UNDERLINE;
break;
default:
iStyle = Font.NORMAL;
break;
}
m_font = new Font(
baseFont,
size,
iStyle,
new BaseColor(color.getRed(), color.getGreen(), color.getBlue())
);
} catch (DocumentException | IOException e) {
throw new HTDocumentException("Error while creating PDF font", e);
}
}
示例13: generateAwesomeFontPdf
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
private File generateAwesomeFontPdf() throws DocumentException, IOException {
File fileOut = openDocument("awesomeFont");
BaseFont bf = BaseFont.createFont("font/FontAwesome.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
FontAwesome awesome = FontAwesome.getInstance();
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(288 / 5.23f);
table.setWidths(new int[]{2, 1});
for (String key : awesome.keys().toSortedList(stringComparator())) {
table.addCell(new PdfPCell(new Phrase(key)));
table.addCell(new PdfPCell(new Phrase(awesome.get(key), font)));
}
document.add(table);
closeDocument();
return fileOut;
}
示例14: emit
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
private void emit(String fontName, String encoding) throws DocumentException, IOException {
String text = "" +
"(defn year-end-evaluation\n" +
" []\n" +
" (if (> (rand) 0.5)\n" +
" \"You get a raise!\"\n" +
" \"Better luck next year!\"))";
BaseFont bf = BaseFont.createFont(fontName, encoding, BaseFont.EMBEDDED);
document.add(new Paragraph(String.format("Font file: %s with encoding %s", fontName, encoding)));
document.add(new Paragraph(String.format("iText class: %s", bf.getClass().getName())));
Font font = new Font(bf, 12);
document.add(new Paragraph(text, font));
document.add(new LineSeparator(0.5f, 100, null, 0, -5));
}
示例15: createPdf
import com.itextpdf.text.pdf.BaseFont; //導入方法依賴的package包/類
public static void createPdf(String filename, String dbTable) throws SQLException, DocumentException, IOException {
// step 1
Document document = new Document(PageSize.A4);
// System.out.println(Tax.class.getResource("fonts/arial.ttf").getPath());
BaseFont bf = BaseFont.createFont("etc/Arial.ttf", "Cp1253", BaseFont.EMBEDDED);
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfPTableEvent event = new Printer();
PdfPTable table = getTable(dbTable, bf);
table.setTableEvent(event);
document.add(table);
document.newPage();
// step 5
document.close();
}