I am trying to put mathmatical symbols into my PDF. The error, java.io.IOException: resources/fonts/FreeSans.ttf not found as file or resource.
public class CreateTable {
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static void main(String[] args) throws FileNotFoundException, DocumentException {
BaseFont bf = null;
try {
bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (IOException e) {
e.printStackTrace();
}
Font f = new Font(bf, 12);
Document document = new Document(); // Whole page is consider as docuemnt so we need object .
PdfPTable table = new PdfPTable(7); //Create Table Object
//Adding alignment and cells with defining rows
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("");
table.addCell("Age \u00AC");
table.addCell("Location");
table.addCell("Anotherline");
table.setHeaderRows(1);}
}
The file is in the resources folder and under fonts. Did I do something wrong?
From your error it seems your program not getting file so throwing exception.
IOException has sub classes such as FileNotFoundException . Make sure file path is correct and resource directory has ttf file .
Related
I am new to Java. This is a code for OCR from image/pdf to text using tess4j. I just want to modify it so that it takes OCR_file.png/OCR_file.pdf from input in the command line and not by specifying path as below-
package tess4j;
import java.io.File;
import java.io.*;
import net.sourceforge.tess4j.*;
public class Test{
public static void main(String[] args) {
// ImageIO.scanForPlugins(); // for server environment
File imageFile = new File("//home//desktop//OCR_file.png");
ITesseract instance = new Tesseract(); // JNA Interface Mapping
// ITesseract instance = new Tesseract1(); // JNA Direct Mapping
instance.setDatapath("//home//desktop//tessdata"); // replace <parentPath> with path to parent directory of tessdata
// instance.setLanguage("eng");
try {
String result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
}
}
that's what the (String[] args) is for, if I'm reading your question correctly.
if you run test.java -"//home//desktop//OCR_file.pdf" in command line/terminal it should be saved to args[0].
so if you rewrite the imageFile initialization to:
File imageFile = new File(args[0]);
that should work since args[0] will be "//home//desktop//OCR_file.pdf"
I also noticed that your imageFile initialization has a typo. the extension at the end is png, but you mentioned that it's supposed to be pdf.
Command - java tess4j.Test "/home/desktop/OCR_file.png"
public static void main(String[] args) {
// ImageIO.scanForPlugins(); // for server environment
String path = args[0];
File imageFile = new File(path);
ITesseract instance = new Tesseract(); // JNA Interface Mapping
// ITesseract instance = new Tesseract1(); // JNA Direct Mapping
instance.setDatapath("//home//desktop//tessdata"); // replace <parentPath> with path to parent directory of tessdata
// instance.setLanguage("eng");
try {
String result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
}
I have html file with Japan language, I converted pdf file. But it don't show text Japanese.
This is my code :
final Charset charset = Charset.forName("UTF-8");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("html.pdf"));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(filename), charset);
document.close();
example :
I have a line text : "ユーザロールを持つユーザだけが利用できるコンテンツ"
I want to add in pdf file by java then show in pdf file.
You need to registry JAPANESE Font
public static final String DEST = "results/fonts/chinese.pdf";
public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
public static final String KOREAN = "\ube48\uc9d1";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new NotoExample().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph(TEXT, font);
document.add(p);
document.add(new Paragraph(CHINESE, font));
document.add(new Paragraph(JAPANESE, font));
document.add(new Paragraph(KOREAN, font));
document.close();
}
}
I have some problem adding unicode emoticon on pdf created with itext pdf. I tried with this and itextpdf core 5.5.13
public class MathSymbols {
public static final String DEST = "EXAMPLE.pdf";
public static final String FONT = "/res/fonts/arialuni.ttf";
public static String TEXT ;
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
TEXT = "this "+Character.toChars(0x1F600)+" string \uD83D\uDE00 contains \ud83d\ude00 special \u2609 characters like this \u2208, \u2229, \u2211, \u222b, \u2206";
new MathSymbols().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font f = new Font(bf,12);
Paragraph p = new Paragraph(TEXT, f);
document.add(p);
document.close();
}}
I have the same proble on itextpdf 7.x with this snippet
public class Main {
public static final String DEST = "example.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
new Main().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
PdfFont f = PdfFontFactory.createFont("/resources/arialuni.ttf", PdfEncodings.IDENTITY_H,true);
Paragraph p = new Paragraph("H\u2082SO\u2074 1 \uD83D\uDE00 contains \ud83d\ude00 spe \u2702 cial \u2609 characters like this \u2208, \u2229, \u2211, \u222b, \u2206").setFont(f).setFontSize(10);
document.add(p);
document.close();
}}
I tried with different fonts and different way in java like here.
But I only obtaing a withe space, or a square, in the pdf and no emoticon
I have an word document
Docx file
As you can see in the word document there are a number of questions with Bullet Points. Right now I am trying to extract each paragraph from the file using apache POI. Here is my current code
public static String readDocxFile(String fileName) {
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
String whole = "";
for (XWPFParagraph para : paragraphs) {
System.out.println(para.getText());
whole += "\n" + para.getText();
}
fis.close();
document.close();
return whole;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
The problem with above method is that it is printing each line instead of paragraphs. Also the bullet points are also gone from extracted whole string. The whole is returned a plain string.
Can anyone explain what I am doing wrong. Also please suggest if you have a better idea to solve it.
Above code is correct and I ran your code on my system that giving each and every paragraphs , I think problem with writting content on docx file whenever I wrote content in bullet points and uses 'enter' key than that breaks my current bullet points and above code make that breaked-line as saparate paragraph.
I am writting below code sample may be It's useful for you take a look here I am using Set datastructure for ignoring duplicate questions from docx .
Dependency of apache poi is below
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.7</version>
</dependency>
Code Sample :
package com;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.util.ObjectUtils;
public class App {
public static void main(String...strings) throws Exception{
Set<String> bulletPoints = fileExtractor();
bulletPoints.forEach(point -> {
System.out.println(point);
});
}
public static Set<String> fileExtractor() throws Exception{
FileInputStream fis = null;
try {
Set<String> bulletPoints = new HashSet<>();
File file = new File("/home/deskuser/Documents/query.docx");
fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
paragraphs.forEach(para -> {
System.out.println(para.getText());
if(!ObjectUtils.isEmpty(para.getText())){
bulletPoints.add(para.getText());
}
});
fis.close();
return bulletPoints;
} catch (Exception e) {
e.printStackTrace();
throw new Exception("error while extracting file.", e);
}finally{
if(!ObjectUtils.isEmpty(fis)){
fis.close();
}
}
}
}
I couldn't find which version of apache POI you are using. If it's the latest version (3.17), the XWPFParagraph object used in your code has a getNumFmt() method. From the apache poi documentation (https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFParagraph.html) this method will return the string "bullet" if the paragraph starts with a bullet. So regarding the second point of your question (what happens to the bullets), you can resolve with something like the following:
public class TestPoi {
private static final String BULLET = "•";
private static final String NEWLINE = "\n";
public static void main(String...args) {
String test = readDocxFile("/home/william/Downloads/anesthesia.docx");
System.out.println(test);
}
public static String readDocxFile(String fileName) {
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
StringBuilder whole = new StringBuilder();
for (XWPFParagraph para : paragraphs) {
if ("bullet".equals(para.getNumFmt())) {
whole.append(BULLET);
}
whole.append(para.getText());
whole.append(NEWLINE);
}
fis.close();
document.close();
return whole.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
Regarding your first point, what is the expected output? I ran your code with the provided docx and apart from the missing bullets you mentioned, it looked okay stepping through with the debugger.
I am playing around with the jexcel libary
I have tried to code a small program which does the following:
Read an xls File
Make some computaitons in the sheet and write it to another place
public class DataProcessor {
private String inputFile;
private String outputFile;
private Sheet sheet;
private Workbook w;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public void setOutputFile(String outputFile) {
this.outputFile = outputFile;
}
public void read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
sheet = w.getSheet(0);
} catch (BiffException e) {
e.printStackTrace();
}
}
#SuppressWarnings("deprecation")
public void write() throws IOException, WriteException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Lolonator", 0);
workbook.createSheet("Lolonator123", 1);
workbook.copy(w);
workbook.write();
workbook.close();
}
public static void main(String[] args) throws IOException, WriteException {
ReadExcel test = new ReadExcel();
test.setInputFile("C:/Users/Desktop/sheet1.xls");
test.read();
System.out.println("####################################################");
System.out.println("File read!");
// Write
System.out.println("####################################################");
System.out.println("Start to write the file!");
WriteExcel out = new WriteExcel();
out.setOutputFile("C:/Users/Desktop/sheet2.xls");
out.write();
System.out.println("Please check the result file!");
}
}
However, this does not work. I do not get any output in my sheet, even though my program runs without exception to the end. I really appreciate your answer!!!
In your write function, you are using "inputFile" as parameter to File constructor but you are not initializing it after you create the out object.
So the following line in the write function
File file = new File(inputFile);
should be
File file = new File(outputFile);
Also are you sure that you do not see any errors after running this code. It should be throwing a null pointer exception.
Hope this helps...