Use pdfbox with Notepad ++ and windows cmd? - java

I don't want to use large programs like Netbeans Eclipse and so on and should only do a small thing with pdfbox (https://pdfbox.apache.org/) but it is not working. Do I have to use maven etc?
I have downloaded pdfbox-2.0.20.jar and pdfbox-app-2.0.20.jar
Added PATH to environment variable in Windows.
Created this (Document_Creation.java) in Notepad++
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
public class Document_Creation {
public static void main (String args[]) throws IOException {
//Creating PDF document object
PDDocument document = new PDDocument();
//Saving the document
document.save("C:/folder/my_doc.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
}
}
Open Windows CDM and type: javac Document_Creation.java
But get this error
Document_Creation.java:2: error: package org.apache.pdfbox.pdmodel does not exist
import org.apache.pdfbox.pdmodel.PDDocument;
^
Document_Creation.java:9: error: cannot find symbol
PDDocument document = new PDDocument();
^
symbol: class PDDocument
location: class Document_Creation
Document_Creation.java:9: error: cannot find symbol
PDDocument document = new PDDocument();
^
symbol: class PDDocument
location: class Document_Creation
3 errors
Looks like I can't access pdfbox with import org.apache.pdfbox.pdmodel.PDDocument;
How can I make this code work with latest java (working, tested with println without pdfbox), Notepad++ and Windows CMD?

As noted here, the solution is in the syntax order, requiring classpath items before the class:
java -cp .;"C:\Program Files\Java\pdfBox\pdfbox-app-2.0.24.jar" Document_Creation
I also put in the full path where I have pdfBox jar located.
---TLDR---
I've used pdfbox years ago as command line tool format with success, but wanted to step up to using it in a java application. After cleaning up stuff (deleting old java installations [to get java and javac on same version], cleaning up paths, figuring out how to create a macro in notepad++ that allowed for compilation and running of the application, and restarting notepad++) to get to the point of this question, I was almost ready to give up until my search result pulled up the solution. Now I feel ready to dive deeper and hopefully get to use pdfBox for my actual application.

Related

JAVA XWPFDocument script gives java.lang.Error

I am sorry, I am absolutely new to JAVA and no (until now) nothing about JAVA, and only little to coding. I wanted to learn simple document creation with the XWPFDocument Library. I got a sample code from https://www.geeksforgeeks.org/java-program-to-write-a-paragraph-in-a-word-document/ but the code does not work.
I am also sorry for not being able to format the code as a code. I made spaces with my hand, but not everywhere. I know it's horrible, but I don't know how to do to properly. A have a brand new Mac and a microsoft keyboard which drives me crazy + I also do not know how to do it the advice strg + k
I get the error:
at GFG.main(docgen_V0.1.java:17)
js#MacBook-Pro-von-Jonathan ~ % /usr/bin/env /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home/bin/java --enable-preview -XX:+Show
CodeDetailsInExceptionMessages -cp /private/var/folders/fq/f_v1db_d17qc749gnnmrbch00000gn/T/vscodesws_2d1b0/jdt_ws/jdt.ls-java-project/bin G
FG
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
XWPFDocument cannot be resolved to a type
XWPFDocument cannot be resolved to a type
XWPFParagraph cannot be resolved to a type
XWPFRun cannot be resolved to a type
XWPFRun cannot be resolved to a type
XWPFRun cannot be resolved to a type
The Code is:
public class docgen_V0.2 {
// Java Programming to Write a paragraph in a Word Document
// Importing required packages
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Create a blank document
XWPFDocument xwpfdocument = new XWPFDocument();
// Create a blank file at C:
File file = new File("/Users/Shared/Arbeit/Codes/Speachdoc/addParagraph.docx");
// Create a file output stream connection
FileOutputStream ostream
= new FileOutputStream(file);
/* Create a new paragraph using the document */
// CreateParagraph() method is used
// to instantiate a new paragraph
XWPFParagraph para = xwpfdocument.createParagraph();
// CreateRun method appends a new run to the
// paragraph created
XWPFRun xwpfrun = para.createRun();
// SetText sets the text to the run
// created using XWPF run
xwpfrun.setText(
"Geeks for Geeks is a computer science portal which aims "
+ "to provide all in one platform for learning and "
+ "practicing.We can learn multiple programming languages here. ");
// Write content set using XWPF classes available
xwpfdocument.write(ostream);
// Close connection
ostream.close();
}
}
}
I just tried to run it. I expect to create a document with a little paragraph that says:
" Geeks for Geeks is a computer science portal which aims to provide all in one platform for learning and practicing.We can learn multiple programming languages here."

In Codename One, why can I not get FileInputStream to import or compile?

Here are my imports:
import com.codename1.ui.*;
import com.codename1.ui.util.*;
import com.codename1.ui.plaf.*;
import com.codename1.ui.events.*;
import com.codename1.io.*;
import com.codename1.ui.layouts.*;
import java.io.*;
I cannot get this code to compile:
InputStream in = new FileInputStream("users.csv");
Here is the error:
C:\Users\Isaac\Documents\NetBeansProjects\CodenameOne_TESTING\src\com\fakecompany\testapp\MyApplication.java:119: error: cannot find symbol
InputStream in = new FileInputStream("users.csv");
symbol: class FileInputStream
location: class MyApplication
I thought this might be a problem with the imports, and sure enough, when I specifically imported java.io.FileImputStream it gave me an additional error:
C:\Users\Isaac\Documents\NetBeansProjects\CodenameOne_TESTING\src\com\fakecompany\testapp\MyApplication.java:13: error: cannot find symbol
import java.io.FileInputStream;
symbol: class FileInputStream
location: package java.io
What is going on? Is there a different way I am supposed to import files in Codename One? Let me know if this is not enough of my code to find the error.
PS: I need to get an input stream implemented so I can parse the csv file:
CSVParser parser = new CSVParser();
String[][] data = parser.parse(in);
It looks like Codename One has omitted that class - and others, I suspect.
Judging by the documentation, I suspect you want to use the com.codename1.io.FileSystemStorage class and its openInputStream method.
You may well want to watch the video on storing data to persistent storage too.
Jon's answer is correct but partial. The question is where is the CSV file actually stored...
If the file is in the src folder (part of your jar) use Display.getInstance().getResourceAsStream(getClass(), "/filename");.
If you downloaded it, then its very likely you downloaded to storage and not necessarily file system (slightly different things in mobile). Both have rather detailed API's to open/write and the Util class has a nice download API. Keep in mind that you can't just "put" a file on the device like you can in a computer, the filesystem is quite different.
As a sidenote, Codename One has a builtin CSVParser class which could be useful for you.

Dom manipulation [duplicate]

How would one go about converting a SVG file to a PDF programatically? (I need to alter the SVG in certain respects before generating the PDF so simply pre-converting it using a tool won't be sufficient.)
Ideally using Java but Perl or PHP would be fine too.
Obviously I am basically considering Apache FOP and Batik with Java. However no matter how long I search I cannot find a simple introduction on how to do it. Things like SVGConverter have descriptions like "Defines the interface for classes that are able to convert part or all of a GraphicContext", but I don't really know what that means.
I have this feeling there must be an API to do this quite simply, provided by FOP or Batik, but I'm just not able to find it at the moment (or perhaps it really doesn't exist.)
In terms of the supported SVG features I need, the file has some paths which are filled with some linear gradients.
Ideally if I could pass the SVG in as a DOM Document that would be ideal; then I would load my template SVG file, change it as specified by the user, and then generate the PDF.
Thanks to Adrian for showing how the Batik rasterizer API is supposed to be used. However, I needed a more lightweight solution--- I can't write to temporary files, and I want fewer dependencies. So, starting from the methods he pointed to, I found a way to access the lower-level code to do the conversion and nothing else.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.fop.svg.PDFTranscoder;
public class Test {
public static void main(String[] argv) throws TranscoderException, FileNotFoundException {
Transcoder transcoder = new PDFTranscoder();
TranscoderInput transcoderInput = new TranscoderInput(new FileInputStream(new File("/tmp/test.svg")));
TranscoderOutput transcoderOutput = new TranscoderOutput(new FileOutputStream(new File("/tmp/test.pdf")));
transcoder.transcode(transcoderInput, transcoderOutput);
}
}
The compile-and-run commands are
javac -cp batik-rasterizer.jar -d build Test.java
java -cp build:batik-rasterizer.jar Test
The important point is that TranscoderInput and TranscoderOutput can work with any InputStream and OutputStream, not just file streams. Note that one of the constructors takes a org.w3c.dom.Document, which means that you don't even need to serialize an SVG DOM into an SVG string, saving an additional step.
This version also doesn't write anything to stdout/stderr, unlike the high-level API.
For JPEG, PNG, or TIFF output, replace org.apache.fop.svg.PDFTranscoder with org.apache.batik.transcoder.image.JPEGTranscoder, PNGTranscoder, or TIFFTranscoder (note that these raster formats are in a different package).
(I'm not quite sure how Java finds the org.apache.batk.transcoder.* and org.apache.fop.svg.PDFTranscoder classes, since I don't see them in the batik-rasterizer.jar.)
Edit:
Although the simple commandline-compilation works with the batik-rasterizer.jar only, it's doing some sort of classloader magic to find all the necessary classes. In a more realistic case (building a project with Ant), you have to find the classes by hand. They can be found in batik-1.7.zip from the Batik project and fop-1.1.zip from the FOP project. From Batik, you need to compile with batik-transcoder.jar and run with
batik-transcoder.jar
batik-anim.jar
batik-awt-util.jar
batik-bridge.jar
batik-css.jar
batik-dom.jar
batik-ext.jar
batik-gvt.jar
batik-parser.jar
batik-script.jar
batik-svg-dom.jar
batik-util.jar
batik-xml.jar
xml-apis-ext.jar
From FOP, you need to compile with fop.jar and run with
fop.jar
avalon-framework-4.2.0.jar
xmlgraphics-commons-1.5.jar
I finally managed to find the appropriate lines of code to solve this using the Batik.
You need to have the SVG file and the resulting PDF as files on the disk, i.e. I couldn't find a way to do it in-memory (I am writing a HTTP Servlet so I have no intrinsic need to write anything as a file, ideally I would stream the result to the HTTP client). I used File.createTemporaryFile to create a file to dump out my SVG to a file, and for the resulting PDF to be written to.
So the lines I used are the following:
import org.apache.batik.apps.rasterizer.DestinationType;
import org.apache.batik.apps.rasterizer.SVGConverter;
import ...
// SVG available as a DOM object (created programatically by my program)
Document svgXmlDoc = ...
// Save this SVG into a file (required by SVG -> PDF transformation process)
File svgFile = File.createTempFile("graphic-", ".svg");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source2 = new DOMSource(svgXmlDoc);
FileOutputStream fOut = new FileOutputStream(svgFile);
try { transformer.transform(source2, new StreamResult(fOut)); }
finally { fOut.close(); }
// Convert the SVG into PDF
File outputFile = File.createTempFile("result-", ".pdf");
SVGConverter converter = new SVGConverter();
converter.setDestinationType(DestinationType.PDF);
converter.setSources(new String[] { svgFile.toString() });
converter.setDst(outputFile);
converter.execute();
And I have the following JARs (search using Google to find the projects and download them):
avalon-framework-4.2.0.jar
batik-all-1.7.jar
commons-io-1.3.1.jar
commons-logging-1.0.4.jar
fop-0.95.jar
log4j-1.2.15.jar
xml-apis-ext.jar
xmlgraphics-commons-1.3.1.jar
you will need a libray for rendering svg's and pdf's.
I recommend SVG salamander for the former, and iText for the latter. With svg salamander you can to read the svg and create an image object, and with itext you can write that image to a pdf.
I use Altsoft Xml2PDF. If I understood correctly all your needs and requirement, you'd better try their Server version of Xml2PDF.
All you need is phantomjs. You don't need the unwieldy Batik for this at all; just get to a point where you can run phantomjs, calling rasterize.js, using the url of the pdf as a source, and a location as the output. Depending on what you want to do with the .pdf, you don't even need Java.
http://phantomjs.org/screen-capture.html
Look at the part starting with "Beside PNG format, PhantomJS supports JPEG, GIF, and PDF."

How to avoid java.lang.NoClassDefFoundError

I have a code for adding the texts to existing .doc file and it'll save that as another name by using apache POI.
The following is the code I have tried so far
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFTable;
public class FooterTableWriting {
public static void main(String args[])
{
String path="D:\\vignesh\\AgileDocTemplate.doc";
String attch="D:\\Attach.doc";
String comment="good";
String stat="ready";
String coaddr="xyz";
String cmail="abc#gmail.com";
String sub="comp";
String title="Globematics";
String cat="General";
setFooter(path, attch, comment, stat, coaddr, cmail, sub, title, cat);
}
private static void setFooter(String docTemplatePath,String attachmentPath,String comments,String status,String coAddress,String coEmail,String subject,String title,String catagory)
{
try{
InputStream input = new FileInputStream(new File(docTemplatePath));
XWPFDocument document=new XWPFDocument(input);
XWPFHeaderFooterPolicy headerPolicy =new XWPFHeaderFooterPolicy(document);
XWPFFooter footer = headerPolicy.getDefaultFooter();
XWPFTable[] table = footer.getTables();
for (XWPFTable xwpfTable : table)
{
xwpfTable.getRow(1).getCell(0).setText(comments);
xwpfTable.getRow(1).getCell(1).setText(status);
xwpfTable.getRow(1).getCell(2).setText(coAddress);
xwpfTable.getRow(1).getCell(3).setText(coEmail);
xwpfTable.getRow(1).getCell(4).setText(subject);
xwpfTable.getRow(1).getCell(5).setText(title);
xwpfTable.getRow(1).getCell(6).setText(catagory);
}
File f=new File (attachmentPath.substring(0,attachmentPath.lastIndexOf('\\')));
if(!f.exists())
f.mkdirs();
FileOutputStream out = new FileOutputStream(new File(attachmentPath));
document.write(out);
out.close();
System.out.println("Attachment Created!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The following is what I got
org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: error: The document is not a document#http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager#http://schemas.openxmlformats.org/drawingml/2006/main
at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:124)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:200)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:74)
at ext.gt.checkOut.FooterTableWriting.setFooter(FooterTableWriting.java:32)
at ext.gt.checkOut.FooterTableWriting.main(FooterTableWriting.java:25)
Caused by: org.apache.xmlbeans.XmlException: error: The document is not a document#http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager#http://schemas.openxmlformats.org/drawingml/2006/main
at org.apache.xmlbeans.impl.store.Locale.verifyDocumentType(Locale.java:458)
at org.apache.xmlbeans.impl.store.Locale.autoTypeDocument(Locale.java:363)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1279)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1263)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source)
at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:92)
... 4 more
I have added all the jar files corresponding to this but still I can't find the solution.I'm new to this apache poi so please help me with some explanations and examples.
Thanks
Copied from my comment done to the question:
Looks like you need poi-ooxml-schemas.jar that comes in the Apache POI distribution. Just adding a single jar doesn't mean that you have all the classes of the framework.
After solving the problem based on my comment (or another people answers), you have this new Exception
org.apache.xmlbeans.XmlException: error: The document is not a document#http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager#http://schemas.openxmlformats.org/drawingml/2006/main
Reading Apache POI - HWPF - Java API to Handle Microsoft Word Files, it looks like you're using the wrong class to handle 2003- word documents: HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java ... The partner to HWPF for the new Word 2007 .docx format is XWPF.. This means that you need HWPFDocument class to handle the document or change your document from Word 2003- to Word 2007+.
IMO I find Apache POI as a good solution to handling Excel files, but I would look another options to handling Word documents. Check this question to get more related info.
This is the dependency hierarchy for poi-ooxml-3.9.jar.
Which means any of them can be used at runtime even if they aren't used at compile-time.
Make sure you have all the jars in the classpath of your project.
Add this dependency on your config file:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
or
System couldn’t find the
poi-ooxml-schemas-xx.xx.jar
Please add the library to your classpath.
The class org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.Factory is located in the jar ooxml-schemas-1.0.jar which can be downloaded here
You're getting that error because you don't have the proper dependency for the XWPFDocument. ooxml-schemas requires xmlbeans, and ooxml requires poi and ooxml-schemas, etc...
Check here: http://poi.apache.org/overview.html#components
Thought I would report my experience with this error. I started getting it out of the blue, and hadn't changed anything in my workspace. Turns out that it occurs while trying to read an Excel file that has more than 1 sheet (second sheet was a pivot table, large amount of data. Not quit sure if it's due to the size of the data (I suspect so, because I HAVE read Excel files that contain more than 1 worksheet). When I deleted that second sheet, it worked. No changes to classpath needed.
org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: Element themeManager#http://schemas.openxmlformats.org/drawingml/2006/main is not a valid workbook#http://schemas.openxmlformats.org/spreadsheetml/2006/main document or a valid substitution.
Solution :- use .xlsx format instead of .xls
FWIW I had to add this:
compile 'org.apache.poi:ooxml-schemas:1.3'
For my case I had different versions of poi(s). poi-scratchpad was of 3.9 and all others - poi, poi-ooxml,poi-ooxml-schemas were of 3.12. I changed version of poi-scratchpad to 3.12 as well and everything started working.
If you are not using maven for your project dependencies. You should have the following jars in your classpath

Converting and OPC (Open Packaging Convention) file to and flat XML file in JAVA

So I am trying to get an flat file XML version of an OPC document.
I am using OPCPackage from org.apache.poi.openxml4j
In C++ you there is a call that creates flat XML file from this zipped file.
Anyone knows how to do that in Java?
Also any good read related to OPC and Java would be awesome.
Thanks a lot
Cheers
UPDATE: related to the comment i made to only answer...
code
// imports
import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
// code snippet
WordprocessingMLPackage wmlPkg = null;
try
{
wmlPkg = WordprocessingMLPackage.load(inFile);
}
catch (Docx4JException ex)
{
//...
}
FlatOpcXmlCreator flatOpcWorker = new FlatOpcXmlCreator((wmlPkg));
flatOpcWorker.marshal(new FileOutputStream(tmpFlatFile.getAbsolutePath()));
So thats code snippet and it results in compile error:
cannot find symbol symbol: method marshal(java.io.FileOutputStream) location: variable flatOpcWorker of type org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator
My project docx4j has a FlatOpcXmlCreator which does this; see the ConvertOutFlatOpenPackage sample
If you want to use it with POI (which uses XML Beans, not JAXB), I guess you could port it. Both projects are ASL, and both do the OPC part based on OpenXML4J.

Categories