I want to Extract Shruti Text from pdf file and write new pdf. I am using
iText but it can't Extract Proper text,so what is the solution for it???
i am using iText 5.4 lib for this
iText is show in new pdf ',' '-','_' and blanks instaed of Shruti font text
Code That I am Using is,
//for extract text From pdf
try {
PdfReader pdfreader = new PdfReader(file path,password);
String iTextContent = PdfTextExtractor.getTextFromPage(pdfreader,1);
} catch (IOException ex) {
Logger.getLogger(JFileChooserDemo.class.getName()).log(Level.SEVERE, null, ex);
}
//write new pdf file
try{
Document docNew = new Document();
PdfWriter writer = PdfWriter.getInstance(docNew,new FileOutputStream("D:\\demo.pdf"));
docNew.open();
BaseFont bf = BaseFont.createFont("D:\\DeskTop\\Pdf Box jar\\shruti.ttf", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font f = new Font(bf,5);
docNew.add(new Paragraph(newStr,f));
docNew.close();
writer.close();
}catch(Exception e){
e.printStackTrace();
}
Related
Hi I'm creating a pdf report using Itext 5, the report contains non-Latin characters, some characters are showing, and some are not.
I'm adding the minimum code example to replicate the issue. I think it may be font, but same font is working with Apache POI to generate doc file.
File file = new File(Util.FONTS, "calibri.ttf");
FontFactory.register(file.getAbsolutePath(), "MY_FONT");
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("Hello.pdf"));
document.open();
document.add(new Paragraph("ครูเวียดนามคนนี้เขาเย็ดเด็กนักเรียนทั้งห้อง สุดยอดจริงๆ ม้าขาว.mp4",
FontFactory.getFont("Tahoma", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)));
document.add(new Paragraph("Hello, Καλημέρα, Grüßgott, Привіт",
FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)));
document.close();
} catch (DocumentException | IOException e1) {
e1.printStackTrace();
}
[Font File use][1]
[1]: https://www.dropbox.com/sh/2ca0cc8nvrviido/AAAAcrHj4DMehZm8no0ImR89a?dl=1
Thank you.
I am trying to create PDF file with java and I need to use Lithuanian letters within file. I tried to use html code and use htmlWorker to parse it, however it does not work on most letters(it works on some). If anyone could help me with this I would gladly appreciate it.
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(pathy));
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
String s = ("<html>ĄąĘęŪūČŠ"
+ "ŽčšžĖėĮįŲų</html>");
htmlWorker.parse(new StringReader(s));
document.close();
}
catch(Exception e2){
}
I solved my issue by using unicode, not sure why it doesn't work with html code still...
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(pathy));
document.open();
BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
document.add(new Paragraph("ąĄčČęĘėĖįĮšŠųŲūŪžŽ", new Font(bfComic, 12)));
} catch (Exception e2) {
System.err.println(e2.getMessage());
}
document.close();
So I was trying to use this font I installed called "Tengwar Quenya-1 Regular" and it didn't work, it keep writing de PDF document with the default font. So I tried to use the downloaded file, by using EMBED method, and it is still printing the default font, I wondering if anyone had tried this before, and could tell me what I am doing wrong. Check the code:
public void testePdf(){
Document document = new Document();
String filename = "C:\\Users\\Marcelo\\Downloads\\tengwar_quenya\\QUENCAP1.TFF";
FontFactory.register(filename);
Font fonte = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED);
try {
PdfWriter.getInstance(document,
new FileOutputStream(filename+ "HelloWorld.pdf"));
document.open();
document.add(new Paragraph("A Hello World PDF document.", fonte));
document.close(); // no need to close PDFwriter?
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
It compiles fine, just not with the font I selected. If it is a glyph instead of a caracter, will it be a problem?
I have Googled for the font you mention. I have downloaded it and I have made a small SSCCE that you can download here: TengwarQuenya1
This is the code:
public static final String FONT = "resources/fonts/QUENCAP1.TTF";
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font f1 = FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
document.add(new Paragraph("A Hello World PDF document.", f1));
document.close();
}
This is the result: tengwarquenya1.pdf
I'm not sure what the resulting text means, but it doesn't look like the default font to me.
In other words: I can't reproduce the problem. Note that you don't need to register a font if you pass its file path to the FontFactory. Obviously, my font path is different from yours. I think that yours is wrong. Try putting the ".TTF" file in another location.
I can successfully generate a pdf from an html string, but the problem is that it doesn't take the css script. How can I generate the pdf with css style?
Please help! I have tried cssresolver als
My code is here:
{String result = "failed";
try
{
String html2 ="<html>"+.....+"</html>" ;
long timemilli = System.currentTimeMillis();
String filename = "EastAfriPack2014_Ticket_"+timemilli;
String writePath = Global.PDF_SAVE_PATH + filename ;
System.out.println("----------writePath--------------"+ writePath);
OutputStream file = new FileOutputStream(new File(writePath+".pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(k.getBytes());
CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(false);
cssResolver.addCss("table {color: red; background-color: blue; } ", true);
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
System.out.println("pdf created");
result = filename;
return filename;
} catch (Exception e) {
e.printStackTrace();
return result;
}
}
I don't think your approach works. I tried it before because, its the easiest way to create a PDF from HTML, but got bitten by same problem.
You either provide the styles inline via style attribute for the table
or
use the HTML, CSS files separately and send them to the HelperClass
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream("myhtmlFile.html"),
new FileInputStream("myCSSFile.css"));
the HTML part can also be an inputStream you made above in the code.
I have HTML file with an external CSS. I want to create PDF from the HTML file, but the endcoing doesn't work. HTML file works fine, but after transfering to PDF, some characters in PDF are missing. (čřě...) It happens even if I set the Charset in PDFWriter constructor.
How do I solve this, please?
public void createPDF() {
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(username + ID + ".pdf"));
document.open();
String hovinko = username + ID + ".html";
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(hovinko), Charset.forName("UTF-8"));
document.close();
System.out.println("PDF Created!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Did you try to convert your special characters before writing them to your PDF?
yourHTMLString.replaceAll(oldChar, newChar);
ć = ć
ř = ř
ě = ě
If you need more special characters, visit this link.
EDIT: Then try this out, it worked for me:
BaseFont basefont = BaseFont.createFont("C:/Windows/Fonts/ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(basefont, 12);
document.add(new Paragraph("čřě", font));
Try it with below logic. It worked for me:
InputStream is = new ByteArrayInputStream(hovinko.getBytes(Charset.forName("UTF-8")));
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, Charset.forName("UTF-8"));
I used xmlworker version 5.5.12 and itextpdf version 5.5.12.
I was strugling with sam problem (Polish special signs).
For me solution was to write a good font-family in html code.