In our application, we have velocity code which generates csv and html reports.
Now we want to add pdf support to it.
So i tried exploring way. But found that we can not generate pdf from velocity.
so it will be something like first create docx and then from docx to pdf.
I am ok with this way also.
I already have .vm file which is something like:
#if ($file.getRecords().size() != 0)
bla bla bla....
My java code replaces $file with
VelocityContext context = new VelocityContext();
context.put("file", fileCatalog);
where filecatalog is clonnable class and has mmethod getrecords ...etc etc.
this setup is perfectly working for csv and html output generation.
but for docx generation, its saying docx is corrupt and cannot be opened.
any idea whats wrong in it?
I wanted pdf as final output. since its not possible directly, so chose making docx first but docx is also giving problems....
request you to please guide....
TIA!
Related
Dear brothers Hope you all right?
I'm designing a document program, however, rather to save file .text extension or using any other MS-Office API in java, i want to create my custom file format such as ".sad" extension so that this sort of file can only be read by my programs, how this can be possible?
Your requirement seems ambiguous. Are you looking to make a program that creates MS Office Word documents or plain text files with a custom file extension?
In the case of the former, you can't have a custom extension as MS Word documents, by definition, have a .doc / .docx extension.
However, if you are looking to create a program that produces text files then you can easily have a custom extension. Just look at this tutorial: How to create a file in Java
I already stated why this is a bad idea. Yet I have a solution for you (more like a how-not-to-do-it)
Take your plain text you want to save, convert it to bytes and apply this "highly enthusiastic encryption nobody will ever be able to break" on it:
string plainText = "yadayada";
bytes[] bytesFromText = toBytes(plainText);
bytes[] encrypted = new Array(sizeof(bytesFromText)*2);
for(int i = 0; i < sizeof(bytesFromText); i++){
if((i modulo 2) == 0){
encrypted.push(toByte(Math.random modulo 255));
}
encrypted.push(bytesFromText[i]);
}
I let it up to you to figure out why this is a bad idea and how to decrypt it. ;)
You can create file with any extension
For example,
File f = new File("confidential.sad");
Hope this will work for you :)
Working with custom files in Java
Here is the tutorial that will help you in getting the concept about how to create your own files with custom extension such as .doc or .sad with some information embedded in it and after saving the file you want to read that information form the file.
ZIP
Similar applications often use archives to store data. Consider MS-Word and its documents >with the .docx file extension. If you change the extension of any .docx file to .zip, you >will find that the document is actually a zip archive, with only a different extension.
https://www.ict.social/java/files/working-with-custom-files-in-java-zip-archive
I have published a library that saves files, and handles everything with one line of code only, you can find it here along with its documentation
Github repository
and the answer to your question is so easy
String path = FileSaver
.get()
.save(file,"file.custom");
I'm trying to populate a Word content control with XML data using docx4j (version 3.2.1). I'm evaluating this in order to use it for invoice generation. The documents we want to generate are not very complicated so this looks like a good approach to me.
I have created the content control through Word 2010 dev tools. This is how I try to inject the XML into the docx (taken from this example):
WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(input_DOCX));
FileInputStream xmlStream = new FileInputStream(new File(input_XML));
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML & Docx4J.FLAG_BIND_BIND_XML);
I get the following exception:
org.docx4j.openpackaging.exceptions.Docx4JException: Couldn't find CustomXmlDataStoragePart! exiting..
at org.docx4j.Docx4J.bind(Docx4J.java:300)
at org.docx4j.Docx4J.bind(Docx4J.java:271)
How can I add the CustomXmlDataStoragePart with docx4j, if it doesn't exist yet? Or should/can I do this in Word directly?
Note: I decided to prepare templates in Word directly, because later on these templates must be edited by non-technical users and I don't want to burden them with extra tools, if possible.
You say you "created the content control through Word 2010 dev tools". Unless you mean the content control toolkit, you need to use that or better, either of the OpenDoPE Word addins. Not both.
These tools add a custom xml part into the docx, and allow you to associate it with your content controls via XPath data bindings.
Then, when at runtime you invoke Docx4J.bind, docx4j finds that existing custom xml part, and replaces it with the xml file you provide which contains your runtime data.
I am trying to add header to the documnet using below java code,
The header tag works fine, if I save as .html, but not working when i save as .doc.. when i tried saving as .docx , the file is not opening in word.
fw1.flush();
//fw1.write(str);
fw1.write("<html><body>");
fw1.write("<header align = 'center'>My header</header><table><thead><tr><td>Your header goes here</td></tr></thead><tbody><tr><td>Page body in here -- as long as it needs to be</td></tr></tbody> <tfoot><tr><td>Your footer goes here</td></tr></tfoot></table>");
fw1.write("</body></html>");
fw1.close();
Editing a word doc using java isn't the most straightforward thing to do. Most folks will use c# or vba to do this. You could try out docx4j (http://java.dzone.com/articles/create-complex-word-docx) though if you really want to use java!
I am trying to load some data from an internal .txt file. After some efforts with FileHandle the only I thing I've accomplished is to put this .txt file into a string variable. Instead of this string I need the integers that are stored inside:
FileHandle handle = Gdx.files.internal("txt/questions.txt");
String lines = handle.readString(); `
Part of txt file:0
#a)1! b)0,350,190,185! c)180,1247,180,153! d)710,970,124,101! e)615,1105,175,120! //sheep
#a)2! b)208,344,248,191! c)403,957,142,127! d)655,1250,142,130! e)0,1075,263,150! // elafi
#a)3! b)460,344,164,200! c)10,1232,165,155! d)245,915,150,133! e)268,1083,235,145! //elephant
#a)4! b)624,344,234,190! c)835,260,150,55! d)500,1228,155,172! e)800,1117,185,108! //horse
#a)5! b)858,330,167,203! c)10,890,220,174! d)822,1235,178,145! e)575,943,128,141! //rabbit
You need to "parse" your text file. You could write a simple parser for your text file format (there is nothing special in Libgdx to support parsing text files, so any standard Java features like Java - Parsing Text File OR http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html might help).
Alternatively, it might be simpler put your text file in a format that is easy for existing Libgdx code to parse. That generally means "JSON". JSON is not a Libgdx file format, so there are lots of tools and tutorials explaining JSON. (This format makes more sense if your file is generated by a tool and isn't maintained by a human directly.)
I have the path to my XML file on my computer, but how can I use selenium (web automation tool) to inject the XML file ?
Usually how it is done (manually) is navigate to the URL and COPY AND PASTE the entire XML text into the provided text box..
Any ideas how to inject the file using automation ? There is no way to "drag" the XML file to the text box and I believe the way I'm thinking that it will work is very complicated.
I think this is actually what you want -
File xml = new File("xmlpath");
String url = xml.getAbsolutePath();
url = url.replace('\\', '/');
url = url.replace(" ", "%20");
String actual = "file:/" + url;
selenium.open(actual);
Then you should be able to get the xml using String theXML = selenium.getText("//rootxmlnode"); Then do what you will with it.
Check out the topic of Data Driven Testing to get you started. Something like this should get you going.
Selenium tool allows you to create an automatically generated code in Java.
So, you need to place any text in the provided text box and generate this Java-test code.
Next step is modifying of the generated test. You have to manually write a simplest code, which will read your XML file, get it contents and paste into the text box. The last thing is replacement (in the generated Java code of test!) of the mentioned above text-block to the contents of read XML.
A simplest way for reading file into a string is using Apache commons-io library.
For example: FileUtils.readFileToString(File file, String encoding) gives you a string object with contents of the file.