i am creaing a pdf using with itext libary but i cant able to print rupee symbol in pdf
i have string value to print rupee symbol
<string name="Rs">\u20B9</string>
and my code to add data in to table is below
PdfPTable table1 = new PdfPTable(columnWidths);
table1.setWidthPercentage(100);
table1.getDefaultCell().setUseAscender(true);
PdfPCell cell;
cell = new PdfPCell(new Phrase((R.string.rs)+"2500",StaticValue.FONT_SUBTITLE));
cell.setFixedHeight(28);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table1.addCell(cell);
In your case, maybe the front (StaticValue.FONT_SUBTITLE) does not support the Rupees symbol. So required to use any fonts which support the Rupees Symbol. (For example, fonts such as Lato, Arial, etc.)
Refer the below code snipet,
//Rupees symbol
Font rupeesFont = FontFactory.getFont("assets/font"+FONT_NAME, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, FONT_SIZE);
chunkRupee = new Chunk(getString(R.string.rupee_symbol), rupeesFont);
Where FONT_NAME is the font in asset->font folder which supports Rupees symbol, FONT_SIZE is the required text size and rupee_symbol is "\u20B9"
Paragraph p = new Paragraph();
Chunk chunkContent = new Chunk("Rupees 2500", font);
cell.add(chunkContent); // your content
cell.add(chunkRupee); // rupees symbol
table1.addCell(cell);
Related
I am using itext pdf generater inside my android application. Where I am using both Ordered list and unOrdered List. I am unable to increase size of bullets and Order's text. I have increased List Item size using below code.
List list = new List(List.ORDERED);
list.setPostSymbol(") ");
//Ingredients Entry
for (String ingredient : ingredients.split("#")) {
font = new Font(Font.FontFamily.HELVETICA, txtSize_content, Font.NORMAL, BaseColor.BLACK);
chunk = new Chunk(ingredient, font);
//To set alignment to chunk
phrase = new Phrase();
phrase.add(chunk);
para = new Paragraph();
para.add(phrase);
para.setSpacingBefore(txtSize_content);
para.setAlignment(Element.ALIGN_LEFT);
//end To set alignment to chunk
//Add List Item to List
ListItem item = new ListItem(para);
item.setAlignment(Element.ALIGN_JUSTIFIED);
list.add(item);
}
document.add(list);
You are messing things up with that chunk and phrase combination. Try this instead:
list.add(new ListItem(ingredient, font))
or create your paragraphs like:
new Paragraph(ingredient, font)
On unordered lists the first ListItem with a font seems to set the size of all list symbols.
Ordered lists take the font of each ListItem (or paragraph) to render the numbers.
(v4.1.6)
i am generating a pdf file. Everything works fine. Now I want to increase the paragraph text size in my pdf document but don't know how.
Here is my code
Font f = new Font(Font.FontFamily.TIMES_ROMAN, 25.0f, Font.BOLD, BaseColor.BLACK);
Chunk c = new Chunk("Ergebnisse");
c.setBackground(BaseColor.RED);
Paragraph p1 = new Paragraph(c);
document.add(p1);
p1.setAlignment(Paragraph.ALIGN_CENTER);
I want the text "Ergebnisse" to get bigger and centered in the middle.
Your code was almost correct:
Font f = new Font(Font.FontFamily.TIMES_ROMAN, 25.0f, Font.BOLD, BaseColor.BLACK);
// you created a font, but you never used it:
Chunk c = new Chunk("Ergebnisse", f);
c.setBackground(BaseColor.RED);
Paragraph p1 = new Paragraph(c);
// you changed the alignment AFTER adding p1 to the document
p1.setAlignment(Paragraph.ALIGN_CENTER);
document.add(p1);
So:
use f when creating the Chunk.
switch the last two lines in your code snippet.
I am using Itext to create pdf files in android using below code:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(root.getPath() + "/" + System.currentTimeMillis() + ".pdf"));
document.open();
Font chapterFont = FontFactory.getFont("", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
Font paragraphFont = FontFactory.getFont("", BaseFont.IDENTITY_H, 12, Font.NORMAL);
Chunk chunk = new Chunk(b.getTitle(), chapterFont);
Chapter chapter = new Chapter(new Paragraph(chunk), 1);
chapter.setNumberDepth(0);
chapter.add(new Paragraph(b.getLead(), paragraphFont));
document.add(chapter);
document.close();
when i use english words it works fine, but when i use arabic or persian words it shows empty lines,
what is the problem? and how can i solve it?
thanks in advance,
step 1 - add font:
Font font= FontFactory.getFont("assets/arial.ttf", BaseFont.IDENTITY_H, 16, Font.NORMAL);
step 2 - add table (in my case) :
PdfPTable table = new PdfPTable(4);
table.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
table.addCell(new PdfPCell(new Paragraph("بستانکار",font)));
and for RTL use :
setRunDirection(PdfWriter.RUN_DIRECTION_RTL) .
you can use this method in PdfWriter too;
This problem fixed by changing this line:
Font chapterFont = FontFactory.getFont("", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
to
Font chapterFont = FontFactory.getFont("assets/arial.ttf", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
It's probably a encoding issue, Make sure the default enconding is set to UTF-8.
I am generating PDF file using itext lib.
I want to write Arabic words.
When i run the below code, The words characters are reverse displayed.
The used code :
PdfContentByte cb = docWriter.getDirectContent();
BaseFont bfBold = BaseFont.createFont("assets/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
createHeadings(cb, document.leftMargin(), 70, "السعر الاجمالي: " + tprice + " L.E.");
.
.
.
private void createHeadings(PdfContentByte cb, float x, float y, String text){
cb.beginText();
cb.setFontAndSize(bfBold, 10);
cb.setTextMatrix(x,y);
cb.showText(text.trim());
cb.endText();
}
This image describes the output of the code above:
http://i.stack.imgur.com/OLoLo.jpg
Please take a look at the Ligatures2 example.
Aren't you forgetting this line:
cb.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
The setRunDirection() method is necessary when you want iText to write the text from right to left and create ligatures where necessary. This method also exists in the context of tables in which case you apply it to a PdfPCell object instead of to a ColumnText object.
Also, I don't understand why you use this String: "السعر الاجمالي: ". Please use the Unicode notation instead (e.g. something like "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"), because using a String like yours can create all kinds of confusion regarding encoding and ligatures. Some editors won't use the correct encoding (changing your text into gibberish); some editors will make ligatures (which isn't what iText expects).
For instance, in your case, I don't know Arabic, so I don't know if it's "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a" or "\u064a\u0644\u0627\u0645\u062c\u0627\u0644\u0627 \u0631\u0639\u0633\u0644\u0627" because I don't know if I have to start to read at the glyph with value \u0627 or at the glyph with value \u064a. In any case: iText expects the first "character" in the String to be the first thing that is read by humans.
Please take a look at the ArabicExample example:
The first line is incorrect, because RTL nor Arabic ligatures are supported when using document.add(). The second line is correct (as far as I know: I can't read Arabic) because I used ColumnText.
This is the code I used:
public static final String FONT = "resources/fonts/NotoNaskhArabic-Regular.ttf";
public static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Phrase p = new Phrase("This is incorrect: ");
p.add(new Chunk(ARABIC, f));
p.add(new Chunk(": 50.00 USD"));
document.add(p);
p = new Phrase("This is correct: ");
p.add(new Chunk(ARABIC, f));
p.add(new Phrase(": 50.00"));
ColumnText canvas = new ColumnText(writer.getDirectContent());
canvas.setSimpleColumn(36, 750, 559, 780);
canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
canvas.addElement(p);
canvas.go();
document.close();
}
I used a Phrase, but you can expect the same result when using a Paragraph (Paragraph extends Phrase). Please clarify if this doesn't answer your question. Take into account that most people on StackOverflow don't understand Arabic, so you have to be very explicit when you ask a question and when you say "it doesn't work". As we don't know Arabic, we don't know how it is supposed to work.
Friends am using itextpdf-5.3.4.jar for creating pdf. For showing rupee symbol am using custom font. I tried arial.ttf,arialbd.ttf both this font but no luck rupee symbol is not showing. For showing the rupee symbol i have followed these links but it's not working for me.
How to display indian rupee symbol in iText PDF in MVC3. This is the code I have used.
BaseFont rupee =BaseFont.createFont( "assets/arial .ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
createHeadings(cb,495,60,": " +edt_total.getText().toString(),12,rupee);
private void createHeadings(PdfContentByte cb, float x, float y, String text, int size,BaseFont fb){
cb.beginText();
cb.setFontAndSize(fb, size);
cb.setTextMatrix(x,y);
cb.showText(text.trim());
cb.endText();
}
Please help me guys.
In the comment section, Funkystein wrote that the problem you describe is typical when
you are using a font which doesn't have that glyph. or
you aren't using the right encoding.
I have written an example that illustrates this: RupeeSymbol
public static final String DEST = "results/fonts/rupee.pdf";
public static final String FONT1 = "resources/fonts/PlayfairDisplay-Regular.ttf";
public static final String FONT2 = "resources/fonts/PT_Sans-Web-Regular.ttf";
public static final String FONT3 = "resources/fonts/FreeSans.ttf";
public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font f1 = FontFactory.getFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f2 = FontFactory.getFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f3 = FontFactory.getFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f4 = FontFactory.getFont(FONT3, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
document.add(new Paragraph(RUPEE, f1));
document.add(new Paragraph(RUPEE, f2));
document.add(new Paragraph(RUPEE, f3));
document.add(new Paragraph(RUPEE, f4));
document.close();
}
The RUPEE constant is a String that contains the Rupee character as well as the Rupee symbol: "The Rupee character ₹ and the Rupee symbol ₨".
The characters are stored as Unicode values, because if we store the characters otherwise, they may not be rendered correctly. For instance: if you retrieve the values from a database as Winansi, you will end up with incorrect characters.
I test three different fonts (PlayfairDisplay-Regular.ttf, PT_Sans-Web-Regular.ttf and FreeSans.ttf)
and I use IDENTITY_H as encoding three times. I also use WINANSI a fourth time to show that it goes wrong if you do.
The result is a file named rupee.pdf:
As you can see, the first two fonts know how to draw the Rupee character. The third one doesn't. The first two fonts don't know how to draw the Rupee symbol. The third one does. However, if you use the wrong encoding, none of the fonts draw the correct character or symbol.
In short: you need to find a font that knows how to draw the characters or symbols you need, then you have to make sure that you are using the correct encoding (for the String as well as the Font).
You can download the full sample code here.