I am trying to add some special characters in my Android App Studio generated PDF, but other than °, none of the characters are visible in iText generated pdf. Please help!
Document doc = new Document(PageSize.A4, 15f, 15f, 35f, 25f);
String outPath = Environment.getExternalStorageDirectory()+"/Bar_Fixed_Compressive.pdf";
try {
PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(outPath));
doc.open();
doc.add(new Paragraph("On increase in the temperature of the bar by " +mEditText4b1113.getText().toString() +"\u00B0 C, the uniform thermal " +
"strain is given by, \n " +"\u03B5 =" +"\u03B1" +"x T " ));
doc.close();
}
Related
I am using Jsoup to get the text from an html doc and display it in my android app.
The text cotains a list (<ul><li>).
If I do it like this I get only the text:
val doc = Jsoup.parse(someHtml)
return doc.text()
I tried using wholeText:
val doc = Jsoup.parse(removeImages)
return doc.wholeText()
In this way it keeps some formatting, but still it ignores the bullet points. Is there any way to get the bullet points in the text?
The bullets are rendered by the browser, so they are not a part of the text.
You'll have to add it by yourself, like in this example:
String html = "<html>" +
"<head>" +
"<title>List</title>" +
"</head>" +
"<body>" +
"<ul>" +
"<li>Item 1</li>" +
"<li>Item 2</li>" +
"<li>Item 3</li>" +
"</ul> " +
"</body>" +
"</html>";
Document doc = Jsoup.parse(html);
Element list = doc.select("ul").first();
Elements item = list.children();
for (Element e : item) {
System.out.println("\u2022" + e.text());
}
The output is:
•Item 1
•Item 2
•Item 3
You can replace the bullet with any other character that you like, by replacing the \u2022 code with any other valid code/character.
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 have an application that allows the user to sign the tablet and an image will be produced. That part I have working with confirmation as I am able to view the images after they are created and saved.
I am writing them to a .csv file with the following code.
//region - Create CSV file
writer = new CSVWriter(new FileWriter(sdCardPath + "Orders_Summary_Report.csv"), ',', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, "\r\n");
//writer.writeNext(CSVcursor.getColumnNames()); //Write the column headings first before the looping starts
String [] headings = ("Order Id,Item ID,Item Name,Item Price,Item Count,Item Total,Paid Amount,VOID Order,Payment Method,Order Signature,Member/Authorization,Tab Number,Order Time").split(",");
writer.writeNext(headings);
for(CSVcursor.moveToFirst(); !CSVcursor.isAfterLast(); CSVcursor.moveToNext()) //Loop through all results (use row count of table)
{
String[] entries = (CSVcursor.getInt(CSVcursor.getColumnIndex("orderId"))+ ","
+CSVcursor.getInt(CSVcursor.getColumnIndex("itemId"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("itemName"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("itemPrice")) + ","
+CSVcursor.getInt(CSVcursor.getColumnIndex("itemCount"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("itemTotal"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("orderPaid"))+ ","
+CSVcursor.getInt(CSVcursor.getColumnIndex("orderVoid"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("orderType"))+ ","
+CSVcursor.getBlob(CSVcursor.getColumnIndex("orderSignature"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("referenceId"))+ ","
+CSVcursor.getInt(CSVcursor.getColumnIndex("orderTab"))+ ","
+CSVcursor.getString(CSVcursor.getColumnIndex("orderTime"))).split(","); //Split the string by the delimiter
writer.writeNext(entries); //Write current row of DB to file
writer.flush(); //Flush the stream
}
writer.close();
The file is getting written to successfully, but when I view the .csv file, the column that is saving the blob data looks like this.
B#2342342
B#2123982
B#3952929
It looks like I'm just saving the signature or some other attribute of the file rather than the file itself.
Do I need to stream the blob by bit/buffer into the csv file?
Just echoing #CommonsWare. The right thing to do is save the blob to the disk as am image. That way it can be easily retrieved for embedding into a file if needed. I still save my images to the db as blobs and just display them in a view. If you want to put these files into a csv, I would consider another type like pdf.
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.
I am using Android PDF Write(APW) to create a PDF, but it doesn't work with some special characters(portuguese).
mypdf.addText(170, 50, 40,"Coração");
The standard enconding is:
mypdf.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
outputToFile("helloworld.pdf",pdfcontent,"ISO-8859-1");
I'v tried
outputToFile("helloworld.pdf",pdfcontent,"UTF-8");
outputToFile("helloworld.pdf",pdfcontent,"UTF-16");
outputToFile("helloworld.pdf",pdfcontent,"Cp1252");
and didn't succeed.
Any ideas what should I do?
EDIT
The method outputToFile is defined as:
private void outputToFile(String fileName, String pdfContent, String encoding) {
File newFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
try {
newFile.createNewFile();
try {
FileOutputStream pdfFile = new FileOutputStream(newFile);
pdfFile.write(pdfContent.getBytes(encoding));
pdfFile.close();
} catch(FileNotFoundException e) {
//
}
} catch(IOException e) {
//
}
}
The method addText is defined as:
public void addText(int leftPosition, int topPositionFromBottom, int fontSize, String text, String transformation) {
addContent(
"BT\n" +
transformation + " " + Integer.toString(leftPosition) + " " + Integer.toString(topPositionFromBottom) + " Tm\n" +
"/F" + Integer.toString(mPageFonts.size()) + " " + Integer.toString(fontSize) + " Tf\n" +
"(" + text + ") Tj\n" +
"ET\n"
);
}
Besides, I change the font color to white adding the following rawcontent:
mypdf.addRawContent("1 1 1 rg\n");
Then I come back to the black font color:
mypdf.addRawContent("0 0 0 rg\n");
I took all the information provided, wrote the following simple unit test method and ran it.
public void test19192108()
{
PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);
mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
mPDFWriter.addText(170, 50, 40,"Coração");
String pdfcontent = mPDFWriter.asString();
outputToFile("helloworld19192108.pdf",pdfcontent,"ISO-8859-1");
}
(outputToFilebeing the helper method from the APW PDFWriterDemo class)
The result looks like this:
This seems pretty much to fulfill the expectations.
Thus, in whichever way it doesn't work with some special characters(portuguese) for the OP, some vital information is missing for reproducing the issue.
PS: Depending on the setup of the development environment, there might be an issue with non-ASCII characters in the source code. Thus, it might be a good idea to replace
mPDFWriter.addText(170, 50, 40,"Coração");
with
mPDFWriter.addText(170, 50, 40,"Cora\u00e7\u00e3o");
PPS: Adobe Reader after viewing a file generated like this wants to repair it. The reason is that the cross reference table is broken. The code generating entries for it is this:
public void addObjectXRefInfo(int ByteOffset, int Generation, boolean InUse) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%010d", ByteOffset));
sb.append(" ");
sb.append(String.format("%05d", Generation));
if (InUse) {
sb.append(" n ");
} else {
sb.append(" f ");
}
sb.append("\r\n");
mList.add(sb.toString());
}
(from CrossReferenceTable.java)
Counting the characters in this entry we get 10 + 1 + 5 + 3 + 2 = 21.
According to the specification, though:
Each entry shall be exactly 20 bytes long, including the end-of-line marker
(from section 7.5.4 Cross-Reference Table of ISO 32000-1)
When using (the current version of) the Android PDF Writer, you should fix this code, too.