RTF to HTML for Libre Spreadsheet - Java - java

I'm trying to convert RTF to HTML using Libre. Its working fine for Libre Document but giving null value for Spreadsheet. Below is my java code how I do the parse.
ArrayList<String> styleValue = new ArrayList<String>();
String result;
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
DataFlavor dfTxt = DataFlavor.stringFlavor;
result = rtfToHtml(new StringReader(streamToString((InputStream) contents.getTransferData(dfRTF))));
rtfToHtml ()
public static String rtfToHtml(Reader rtf) throws IOException {
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
try {
kitRtf.read(rtf, p.getDocument(), 0);
kitRtf = null;
EditorKit kitHtml = p.getEditorKitForContentType("text/html");
Writer writer = new StringWriter();
kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
return writer.toString();
} catch (BadLocationException e) {
e.printStackTrace();
}
return null;
}
Appreciated if anyone could help me to fix this for Libre spreadsheet. Please advice.

Related

Csv to PDF PDFBOX?

I'm new to java, and I'm learning how to create PDF, I was using Itext to create PDF, but because it handles a license I stopped using it, and started using PDFbox. I've searched the internet for how to convert from CSV to PDF, but I can't find an example of how to use margins, or how to align the content.
Using Itext, this is my code using Itext7, it works perfectly but i want to migrate it to PDFbox
public static boolean isEmpty(File rutaCSV) {
if(rutaCSV.isDirectory()){
String[] csvFiles = rutaCSV.list();
if (csvFiles.length > 0) {
for (String cadena : csvFiles){
readCSV("path");
try{
File csvFile = new File("path");
if (csvFile.exists()){
csvFile.delete();
return false;
}
System.out.println(csvFile.delete());
}catch(Exception ex){
System.out.println(ex);
}
}
return true;
}else{
System.out.println("There's not files");
return true;
}
}
return true;
}
public static void readCSV(String file, String cadena){
try{
FileReader filereader = new FileReader(file);
// Configuración para leer el archivo con ;
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withCSVParser(parser)
.build();
Document pdfDocument = new Document();
pdfDocument.addTitle("Some");
pdfDocument.setMargins(0, 0,25,25);
String fileName = cadena.replaceFirst("[.][^.]+$", "");
PdfWriter.getInstance(pdfDocument, new FileOutputStream("path"+fileName+".pdf"));
pdfDocument.open();
PdfPTable tableData = new PdfPTable(5);
PdfPCell cells;
// Lista para guardar los datos leídos
List<String[]> allData = csvReader.readAll();
for (String[] row : allData) {
for (String cell : row) {
cells =new PdfPCell(new Phrase(cell));
tableData.addCell(cells);
}
}
pdfDocument.addTitle("Some");
pdfDocument.add(tableData);
pdfDocument.close();
}catch(Exception ex){
ex.printStackTrace();
System.out.println("error");
}
}
Using PDFBox
public static void main(String[] args) throws IOException, CsvException {
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page);
try{
String file = "C:\\Users\\brayan.milian\\Desktop\\pruebaFTP\\prueba.csv";
FileReader filereader = new FileReader(file);
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withCSVParser(parser)
.build();
List<String[]> allData = csvReader.readAll();
for (String row[] : allData) {
for (String cell : row){
content.beginText();
content.setFont(PDType1Font.TIMES_BOLD, 12);
content.showText(cell);
content.endText();
}
}
content.close();
}catch(Exception ex){
System.out.println("dja");
}
Can anyone help me?

How to POST File to an URL in Java CodeName One?

I wanna to upload an image in java and copied in a directory with WebService in Symfony
i tried it with Postman and it worked but when i did it in Java, it didn't work, i don't know how to pass a file like paramatre in the Url request
Please help me to find a solution
Symfony Code:
$file = $request->files->get('nomImage');
$status = array('status' => "success","fileUploaded" => false);
// If a file was uploaded
if(!is_null($file)){
// generate a random name for the file but keep the extension
$filename = uniqid().".".$file->getClientOriginalExtension();
$path = "C:\wamp64\www\pidev\web\uploads\images";
$file->move($path,$filename); // move the file to a path
$status = array('status' => "success","fileUploaded" => true);
}
return new JsonResponse($status);
Postman Screenshot:
I sent the URL with Postman and add the image in Body with nomImage like key and the image like value and it worked
Java Code:
This code is to connect to the URL and i wanted to get the image like file in the URL like in Postman
public void ajoutProduit(File image)
{
ConnectionRequest con = new ConnectionRequest();
con.setUrl("http://localhost/PIDEV/web/app_dev.php/Api/produit/ajout?nomImage="+image);
NetworkManager.getInstance().addToQueueAndWait(con);
}
This is my form and the uploading of the image and execute the Copy of the image which it didn't work
public class AjoutProduit {
private Form fAjout = new Form("", new BoxLayout(BoxLayout.Y_AXIS));
public AjoutProduit() {
TextField nomProduit = new TextField("", "Nom du produit");
TextField descProduit = new TextField("", "Description du produit");
ComboBox<String> opProduit = new ComboBox<>(
"",
"echanger",
"donner",
"recycler",
"reparer"
);
final String[] jobPic = new String[1];
Label jobIcon = new Label();
Button image = new Button("Ajouter une image ");
final String[] image_name = {""};
final String[] pathToBeStored={""};
/////////////////////Upload Image
image.addActionListener((ActionEvent actionEvent) -> {
Display.getInstance().openGallery(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ev) {
if (ev != null && ev.getSource() != null) {
String filePath = (String) ev.getSource();
int fileNameIndex = filePath.lastIndexOf("/") + 1;
String fileName = filePath.substring(fileNameIndex);
Image img = null;
try {
img = Image.createImage(FileSystemStorage.getInstance().openInputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
}
image_name[0] = System.currentTimeMillis() + ".jpg";
jobIcon.setIcon(img);
System.out.println(filePath);
System.out.println(image_name[0]);
try {
pathToBeStored[0] = FileSystemStorage.getInstance().getAppHomePath()+ image_name[0];
OutputStream os = FileSystemStorage.getInstance().openOutputStream(pathToBeStored[0]);
ImageIO.getImageIO().save(img, os, ImageIO.FORMAT_JPEG, 0.9f);
os.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}, Display.GALLERY_IMAGE);});
////////////Copied with URL Symfony
Button myButton = new Button("Valider");
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
ServiceProduit sp = new ServiceProduit();
ServiceEchange se = new ServiceEchange();
String path = "C:/Users/omark/.cn1/"+image_name[0];
File file = new File(path);
sp.ajoutProduit(file);
}
});
fAjout.addAll(nomProduit,descProduit,opProduit,jobIcon,myButton,image);
fAjout.show();
}
Try the x-www-url-form-encoded. If that works then use MultipartRequest to submit binary data to the server. It implicitly handles form encode submission for you. If something doesn't work use the network monitor tool in Codename One to inspect the outgoing request/response which often provide helpful information about the process.
This isn't correct:
ConnectionRequest con = new ConnectionRequest();
con.setUrl("http://localhost/PIDEV/web/app_dev.php/Api/produit/ajout?nomImage="+image);
NetworkManager.getInstance().addToQueueAndWait(con);
You're submitting a URL using the GET style argument passing. You need to submit the date of the image and not the image itself. You need to use addArgument() or addData() etc. to include the content in the request.
i resolved the problem, i modified the " Java Code ":
MultipartRequest cr = new MultipartRequest();
cr.setUrl("http://localhost/PIDEV/web/app_dev.php/Api/produit/ajout");
cr.setPost(true);
String mime = "image/png";
try {
cr.addData("file", filePath, mime);
} catch (IOException e) {
e.printStackTrace();
}
String fichernom = System.currentTimeMillis() + ".png";
cr.setFilename("file", fichernom);
InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
cr.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueueAndWait(cr);

Page count showing zero for APACHE POI .docx file

I have implemented Apache POI library for Page count of Doc pages, but it shows page count zero when I download Google Doc as .docx file.
Edit: My code is as follows
public Integer getPagesCount(byte[] docBytes, String type)
throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(docBytes);
String lowerFilePath = type.toLowerCase();
if (lowerFilePath.equals("docx")) {
#SuppressWarnings("resource")
XWPFDocument docx = new XWPFDocument(in);
return docx.getProperties().getExtendedProperties()
.getUnderlyingProperties().getPages();
} else if (lowerFilePath.equals("doc")) {
#SuppressWarnings("resource")
HWPFDocument wordDoc = new HWPFDocument(in);
return wordDoc.getSummaryInformation().getPageCount();
} else if (lowerFilePath.equals("ppt")) {
HSLFSlideShow document = new HSLFSlideShow(in);
return document.getSlides().size();
} else if (lowerFilePath.equals("pptx")) {
#SuppressWarnings("resource")
XMLSlideShow xslideShow = new XMLSlideShow(in);
return xslideShow.getSlides().size();
} else if (lowerFilePath.equals("pdf")) {
PDDocument doc = PDDocument.load(in);
return doc.getNumberOfPages();
}
return 0;
}

convert a word documents to HTML with embedded images by TIKA

I'm new in TIKA. I try to convert Microsoft word documents to HTML by using Tika. I'm using TikaOnDotNet wrapper to used TIKA on .Net framework. My conversion code is like following:
byte[] file = Files.toByteArray(new File(#"myPath\document.doc"));
AutoDetectParser tikaParser = new AutoDetectParser();
ByteArrayOutputStream output = new ByteArrayOutputStream();
SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
handler.setResult(new StreamResult(output));
ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler);
tikaParser.parse(new ByteArrayInputStream(file), handler1, new Metadata());
File ofile = new File(#"C:\toHtml\text.html");
ofile.createNewFile();
DataOutputStream stream = new DataOutputStream(new FileOutputStream(ofile));
output.writeTo(stream);
everything working well except the embedded images. The generated HTML contains image tag like:
<img src="embedded:image2.wmf" alt="image2.wmf"/>
but the image source does not exists. Please advise me
Credits goes to #Gagravarr.
please note that this is a simple implementation of code, the original codes are available in comment of the questions.
This implementation is based on TikaOnDotNet wrapper.....
public class DocToHtml
{
private TikaConfig config = TikaConfig.getDefaultConfig();
public void Convert()
{
byte[] file = Files.toByteArray(new File(#"filename.doc"));
AutoDetectParser tikaParser = new AutoDetectParser();
ByteArrayOutputStream output = new ByteArrayOutputStream();
SAXTransformerFactory factory = (SAXTransformerFactory)TransformerFactory.newInstance();
var inputStream = new ByteArrayInputStream(file);
// ToHTMLContentHandler handler = new ToHTMLContentHandler();
var metaData = new Metadata();
EncodingDetector encodingDetector = new UniversalEncodingDetector();
var encode = encodingDetector.detect(inputStream, metaData) ?? new UTF_32();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, encode.toString());
handler.setResult(new StreamResult(output));
ContentHandler imageRewriting = new ImageRewritingContentHandler(handler);
// ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler);
ParseContext context = new ParseContext();
context.set(typeof(EmbeddedDocumentExtractor), new FileEmbeddedDocumentEtractor());
tikaParser.parse(inputStream, imageRewriting, new Metadata(), context);
byte[] array = output.toByteArray();
System.IO.File.WriteAllBytes(#"C:\toHtml\text.html", array);
}
private class ImageRewritingContentHandler : ContentHandlerDecorator
{
public ImageRewritingContentHandler(ContentHandler handler) : base(handler)
{
}
public override void startElement(string uri, string localName, string name, Attributes origAttrs)
{
if ("img".Equals(localName))
{
AttributesImpl attrs;
if (origAttrs is AttributesImpl)
attrs = (AttributesImpl)origAttrs;
else
attrs = new AttributesImpl(origAttrs);
for (int i = 0; i < attrs.getLength(); i++)
{
if ("src".Equals(attrs.getLocalName(i)))
{
String src = attrs.getValue(i);
if (src.StartsWith("embedded:"))
{
var newSrc = src.Replace("embedded:", #"images\");
attrs.setValue(i, newSrc);
}
}
}
attrs.addAttribute(null, "width", "width","width", "100px");
base.startElement(uri, localName, name, attrs);
}
else
base.startElement(uri, localName, name, origAttrs);
}
}
private class FileEmbeddedDocumentEtractor : EmbeddedDocumentExtractor
{
private int count = 0;
public bool shouldParseEmbedded(Metadata m)
{
return true;
}
public void parseEmbedded(InputStream inputStream, ContentHandler contentHandler, Metadata metadata, bool outputHtml)
{
Detector detector = new DefaultDetector();
string name = metadata.get("resourceName");
MediaType contentType = detector.detect(inputStream, metadata);
if (contentType.getType() != "image") return;
var embeddedFile = name;
File outputFile = new File(#"C:\toHtml\images", embeddedFile);
try
{
using (FileOutputStream os = new FileOutputStream(outputFile))
{
var tin = inputStream as TikaInputStream;
if (tin != null)
{
if (tin.getOpenContainer() != null && tin.getOpenContainer() is DirectoryEntry)
{
POIFSFileSystem fs = new POIFSFileSystem();
fs.writeFilesystem(os);
}
else
{
IOUtils.copy(inputStream, os);
}
}
}
}
catch (Exception ex)
{
throw;
}
}
}
}

iText: generate PDF from ANY file(including .java, .xml, .php) and keep formatting

I am currently trying to create an application that converts any font code from a software project into a PDF. I'm using iText to generate the PDF but I can't keep the tabbing of the file.
Here's the method that reads the files(simple bufferedReader, but I get every read line and put it in a linkedList):
public static LinkedList<String> lerArquivoQualquerDeTexto(String URL) {
LinkedList<String> textoLido = new LinkedList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(URL)))
{
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null)
{
System.out.println(sCurrentLine);
textoLido.add(sCurrentLine + System.lineSeparator());
}
return textoLido;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
And here's the iText convertion of this LinkedList i just read using BufferedReader:
private static String FILE = "c:/temp/FirstPdf.pdf";
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 9,
Font.NORMAL, BaseColor.RED);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD);
private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
Font.BOLD);
private static Font smallFont = new Font(Font.FontFamily.TIMES_ROMAN, 9,
Font.NORMAL);
/**
* gera o PDF de UM único arquivo já lido
* #param textoLido texto lido pelo buffer
* #param nomeDoArquivoLido nome do arquivo lido, já obtido pelo LeitorArquivoTexto.java
* #return
*/
public static boolean gerarPDFDeString(LinkedList<String> textoLido, String nomeDoArquivoLido)
{
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
addContent(document, textoLido, nomeDoArquivoLido);
document.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// iText allows to add metadata to the PDF which can be viewed in your Adobe
// Reader
// under File -> Properties
private static void addMetaData(Document document) {
document.addTitle("My first project to register");
document.addSubject("Using iText");
document.addKeywords("Java, PDF, iText");
document.addAuthor("Lars Vogel");
document.addCreator("Lars Vogel");
}
private static void addTitlePage(Document document)
throws DocumentException {
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
preface.add(new Paragraph("Nome Do Projeto", redFont));
addEmptyLine(preface, 1);
// Will create: Report generated by: _name, _date
preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
smallBold));
addEmptyLine(preface, 3);
document.add(preface);
// Start a new page
document.newPage();
}
private static void addContent(Document document, LinkedList<String> textoArquivoLido, String nomeDoArquivoLido) throws DocumentException {
Anchor anchor = new Anchor(nomeDoArquivoLido, redFont);
anchor.setName(nomeDoArquivoLido);
// Second parameter is the number of the chapter
Chapter catPart = new Chapter(new Paragraph(anchor), 1);
for(int i = 0; i < textoArquivoLido.size(); i++)
{
String umaLinhaLida = textoArquivoLido.get(i);
Phrase umaLinhaDoPdf = new Phrase(umaLinhaLida, smallFont);
catPart.add(umaLinhaDoPdf);
}
// now add all this to the document
document.add(catPart);
}
I'm trying to convert a .java file from my java project, but the problem is: my output file(the .pdf file) keeps being written without any of my formatting tabs! Any ideas how to solve this?

Categories