Here I am creating the pdf with FUTURA font but my BaseFont.createFont method is giving error. Currently, PDF is being created successfully with Halvetica font but I want that to be created with FUTURA font.
Here I am using itextpdf5.5.13.jar for this activity
I have tried importing tiff file using
BaseFont bf = BaseFont.createFont("futura",BaseFont.TIMES_ITALIC, BaseFont.EMBEDDED, true, bytesArray, null);
But it is giving error
com.itextpdf.text.DocumentException: Font 'futura' with 'Identity-H' is not recognized.
at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:716)
at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:625)
at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:568)
This is the code I am using to write in the PDF.
PdfContentByte cb = writer.getDirectContent();
File file = new File(p.getProperty("FONTFILE"));
byte[] bytesArray = new byte[(int) file.length()];
BaseFont bf = BaseFont.createFont("futura",BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, bytesArray, null);
cb.saveState();
cb.beginText();
cb.moveText(x, y);
cb.setFontAndSize(bf, size);
cb.showText(text);
cb.endText();
cb.restoreState();
Expected result should be "PDF should create using Futura font"
But it is throwing exception.
BaseFont.createFont uses the first parameter to determine the type of font to instantiate.
If it is a standard 14 font name or a CJK font name (from the Adobe CJK font pack, I assume), the font is initialized from the known characteristics thereof without embedding.
If it is ending with ".afm" or ".pfm", the font is initialized as type 1 font.
If it is ending with ".ttf" or ".otf", or contains ".ttc,", the font is initialized as a true type font.
Otherwise (depending on the boolean noThrow parameter) null is returned or an Exception is thrown.
In the case at hand a true type font shall be loaded. Thus, in
BaseFont bf = BaseFont.createFont("futura",BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, bytesArray, null);
replace "futura" by "futura.ttf". Or even, as you eventually did, use the complete path and file name of the font file.
Related
In the previous version of itext (5.5.x) I used the BaseFont class as follows:
BaseFont bf = BaseFont.createFont ("Arial.ttf", BaseFont.WINANSI, true);
Then used the method getWidthPoint
bf.getWidthPoint (TEXT_EXAMPLE, fontSize);
But in the version of itext 7 I am not finding the BaseFont class and also some utility that allows me to get the withPoint of a certain text.
Any help is welcome.
To create a similar font in iText7, use:
PdfFont font = PdfFontFactory.createFont("Arial.ttf", PdfEncodings.WINANSI, true);
To get the width of a certain String, use:
float width = font.getWidth(TEXT_EXAMPLE, fontSize);
I use itext library for creating PDF file because has very detailed rendering functions for PDF creation. When user click the button i write a template and fill the blank cells from DB everytime.
Than i use Icepdf library for show to user and taking output of the created pdf file.
But Icepdf has some character encoding problem i think. When PDf created and callled by Icepdf one of Turkish character looks as square. Turkish characters can be seen at this link. All characters rendered succesfully but eighth character at the picture is not.
When i go to filepath of created pdf file (created by itext library) and open it manually with Adobe Acrobat Reader all characters showing correctly. But if programaticly Icepdf open the file and show to user, eighth character at the picture looks as square.
I need change character encoding of Icepdf but i can't yet. Reading many articles about character and Font encoding of Icepdf but i have not yet succeeded. If i solve this character problem my application ready to deploy.
The generated PDF file can be downloaded here.
When I open this file with Adobe Acrobat it looks like this:
When I open the file with IcePDF programaticly it looks like thi:
Also i read some questions and answers about this on Stackoverflow but none of them have an accepted answer/help.
Code used to create the file path:
File fUrl = new File(CreateAndViewPdf
.class
.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath()
);
String path = fUrl
.toString()
.substring(0,fUrl.toString().lastIndexOf(File.separator))
.replace("%20", " ") + "\\hello.pdf";
Code for the createPdf() method:
public void createPdf()throws DocumentException, IOException {
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, "ISO-8859-9", BaseFont.EMBEDDED);
Document document = new Document(PageSize.A5);
PdfWriter.getInstance(document, new FileOutputStream(path));
document.setMargins(10, 10, 10, 10);
document.setMarginMirroring(true);
document.open();
Font font = new Font( bf );
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
font = new Font( bf );
font.setSize(15);
font.setStyle("bold");
cell = new PdfPCell(new Phrase("Sender\n"+"Information", font));
cell.setPaddingBottom(7);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
font = new Font( bf );
font.setSize(12);
font.setStyle("normal");
cell = new PdfPCell(new Phrase("â ç ğ ı İ î ö ş ü û\n\n"+"Â Ç Ğ I İ Î Ö Ş Ü Û",font));
cell.setPaddingBottom(7);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
table.setWidths(new int[]{50,200});
table.setWidthPercentage(100);
table.setSpacingAfter(10);
table.setSpacingBefore(10);
document.add(table);
document.close();
}
Code for the viewPdf() method:
public void viewPdf(String fileP)throws IOException, InterruptedException {
String filePath = fileP;
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewerComponentPanel = factory.buildViewerPanel();
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
CreateAndViewPdf.this.getContentPane().add(viewerComponentPanel);
CreateAndViewPdf.this.setSize(new Dimension(screen.width/2,screen.height));
CreateAndViewPdf.this.setLocationRelativeTo(null);
controller.openDocument(filePath);
}
--- SOLVED ---
Firstly, thanks for the router comment to #Mike 'Pomax' Kamermans
After reading him/her comments begin investigating " how can i embed the spesific font file to PDF with iText " After 1-2 days later, i found solution just like below;
Going to Windows Control Panel\Appearance and Personalization\Fonts and copy the times.ttf (which tested all special characters supported font) file to my Java Application resources container.
Than i add below lines to top of my createPDF method.
Font fontBase = FontFactory.getFont("/resources/times.ttf",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED,
0.8f, Font.NORMAL,
BaseColor.BLACK);
BaseFont bf = fontBase.getBaseFont();
After this, viewPdf() method get the document to screen with all special characters truely rendering.
I have that part of code:
//Staff
// Title font
BaseFont titleBf = null;
try {
titleBf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252, BaseFont.EMBEDDED);
} catch (IOException e) {
System.out.println(e.getMessage());
}
com.itextpdf.text.Font titleFont = new com.itextpdf.text.Font(titleBf,24);
titleFont.setColor(new BaseColor(0, 0, 204));
/*
* Pdf creation
*/
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(fc.getSelectedFile() + ".pdf"));
document.open();
/*
* Title
*/
Paragraph p = new Paragraph("مرحبا بكم",titleFont);
p.setSpacingAfter(20);
p.setAlignment(1); // Center
document.add(p);
//Staff
document.close();
The output was empty.
Even when I tried to add simple pdfpcell, I got same result, here is the way:
com.itextpdf.text.Font fontNormal = FontFactory.getFont(
("arialuni.ttf"), BaseFont.CP1252, BaseFont.EMBEDDED,
8, com.itextpdf.text.Font.NORMAL);
Chunk chunkArabic = new Chunk("مرحبا العالم", fontNormal);
infoTab.addCell(new PdfPCell(new Phrase(chunkArabic)));
document.add(infoTab);
Different problems may be at play.
Encoding: You are putting Arabic text in your source code. If you save your Java file using the wrong encoding, that text will become corrupt. If you compile your Java file using the wrong encoding, that text will become corrupt. And so on. I hope you get the idea. Store literal text like this: "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628" to avoid problems like this.
As documented in Chapter 11 of my book, right-to-left writing and Arabic ligatures are only supported in the context of PdfPCell and ColumnText. I see that you're using PdfPCell, but I don't see you using cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); anywhere. That's wrong.
You are using an object fontNormal, but you're not telling us which font you're actually using. For instance: if you're using the standard Type1 font Helvetica, no text will show up, because Helvetica doesn't know how to render Arabic text.
Amendment: You've now updated your question, showing that you're using "arialuni.ttf". However, you are using the wrong encoding: CP1252 is the encoding for the Latin Alphabet. You should use BaseFont.IDENTITY_H.
Update: looking at the update of your question as well as the extra comment, I see two major errors.
You are assuming that the name of the font is "arialuni.ttf". That's the font file, but not the font name. Also: you are asking the FontFactory for this font, but did you tell the FontFactory where to look for fonts? Are you sure the FontFactory can locate c:/windows/fonts/ or whatever directory the file arialuni.ttf is stored.
You are declaring a font, but you're not using it: Paragraph p = new Paragraph("\u062D\u064A\u0633\u0648"); creates a Paragraph using the default font.
This is code that works:
BaseFont bf = BaseFont.createFont(
"c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 8);
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("\u062D\u064A\u0633\u0648", font));
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(cell);
document.add(table);
The result looks like this (zoomed in):
I am trying to add a Header to an existing PDF file. It works but the table header in the existing PDF are messed up by the change in the font. If I remove setting the font then the header doesn't show up. Here is my code:
// the document
PDDocument doc = null;
try
{
doc = PDDocument.load( file );
List allPages = doc.getDocumentCatalog().getAllPages();
//PDFont font = PDType1Font.HELVETICA_BOLD;
for( int i=0; i<allPages.size(); i++ )
{
PDPage page = (PDPage)allPages.get( i );
PDRectangle pageSize = page.findMediaBox();
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
PDFont font = PDType1Font.TIMES_ROMAN;
float fontSize = 15.0f;
contentStream.beginText();
// set font and font size
contentStream.setFont( font, fontSize);
contentStream.moveTextPositionByAmount(700, 1150);
contentStream.drawString( message);
contentStream.endText();
//contentStream.
contentStream.close();}
doc.save( outfile );
}
finally
{
if( doc != null )
{
doc.close();
}
}
}`
Essentially you are running into a PDFBox bug in the current version 1.8.2.
A workaround:
Add a getFonts call of the page resources after creating the new content stream before using a font:
PDPage page = (PDPage)allPages.get( i );
PDRectangle pageSize = page.findMediaBox();
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
page.getResources().getFonts(); // <<<<<<<<
PDFont font = PDType1Font.TIMES_ROMAN;
float fontSize = 15.0f;
contentStream.beginText();
The bug itself:
The bug is in the method PDResources.addFont which is called from PDPageContentStream.setFont:
public String addFont(PDFont font)
{
return addFont(font, MapUtil.getNextUniqueKey( fonts, "F" ));
}
It uses the current content of the fonts member variable to determine a unique name for the new font resource on the page at hand. Unfortunately this member variable still can be (and in your case is) uninitialized at this time. This results in the MapUtil.getNextUniqueKey( fonts, "F" ) call to always return F0.
The font variable then is initialized implicitly during the addFont(PDFont, String) call later.
Thus, if unfortunately there already existed a font named F0 on that page, it is replaced by the new font.
Having tested with your PDF this is exactly what happens in your case. As the existing font F0 uses some custom encoding while your replacement font uses a standard one, the text originally written using F0 now looks like gibberish.
The work-around mentioned above implicitly initializes that member variable and, thus, prevents the font replacement.
If you plan to use PDFBox in production for this task, you might want to report the bug.
PS: As mentioned in the comments above there is another bug to observe in context with inherited resources. It should be brought to the PDFBox development's attention, too.
PPS: The issue at hand meanwhile has been fixed in PDFBox for versions 1.8.3 and 2.0.0, cf. PDFBOX-1753.
How can I set a font type by giving it's path in iText
I tried this one. But it is not working.
Font ff = new Font(Font.getFamily("C:/Windows/Fonts/Harry Potter Regular.ttf"));
ff.setSize(12);
First create a BaseFont with required font file:
BaseFont bf = BaseFont.createFont(src, myEncoding, BaseFont.EMBEDDED);
Then create the actual font with size and style attributes, for example:
com.itextpdf.text.Font f = new Font(bf, 14.5f, Font.ITALIC | Font.BOLD);
The getFamily method you used, refers to the Acrobat predefined fonts: FontFactory.COURIER, FontFactory.HELVETICA, FontFactory.TIMES_ROMAN, FontFactory.SYMBOL, FontFactory.ZAPFDINGBATS.