Open PDF File with Adobe Acrobat inside Java Application (via OLE) - java

I would like to open a PDF File inside an existing Java(SWT)-Application. The PDF should not opened in an external frame. I tried via the OLE interface but without success.
clientSite = new OleControlSite(frame, SWT.NONE, "AcroExch.App", fileName);
automation = new OleAutomation(clientSite);
clientSite.doVerb(OLE.OLEIVERB_OPEN);

you can use the IE Interface and then navigate to the PDF file.
Pseudocode:
$Obj = ObjCreate("Shell.Explorer.2")
$Obj.Navigate('D:\Test.pdf')
or the Reader Web Interface
Pseudocode:
$oReader = ObjCreate("AcroPDF.PDF.1")
$oReader.Load("D:\Test.pdf")
How you bind the object into your Java-GUI you may ask under java
HTH, Reinhard
PS: the "Shell.Explorer.2" option mostly works for me better.

Related

Java TESSERACT create byte[] instead of pdf file - tessInstance.createDocuments()

Is it possible to generate with Tess4j the byte[] of a PDF with OCR instead of a physical file?
I need to make PDF files searchable via OCR, it works but I would like to avoid this step.
Tesseract tessInst = new Tesseract();
tessInst.setDatapath("C:\\Tess4J");
List<RenderedFormat> list = new ArrayList<RenderedFormat>();
list.add(RenderedFormat.PDF);
tessInst.createDocuments(inputFile.getPath(), "C:\\a\\b\\b\\Tess4J\\filename", list); // i dont want to create this, i just need a byte[]!
Thx!
No, Tesseract does not support it. TessPDFRendererCreate expects a string for file path as input.
https://tesseract-ocr.github.io/tessapi/5.x/a00008.html

Is it possible to implement a command in CodenameOne that will display a PDF once a button is pressed without using a URL

I am trying to make a program using the CodenameOne plugin that will display various PDF's when buttons are pressed, but I can't find a way to do this without using a specific URL for each PDF. Is there any possible way to accomplish this by placing the PDF files into a package and calling them when needed? I would assume that I need to use an ActionListener but I really don't know what to do. This is what I have tried so far.
b1L1.addActionListener((e)->File file = new File("/path/to/file.pdf");
Desktop.getDesktop().open(file));
Desktop or other AWT related API's are unavailable. Even java.io.File doesn't make sense on Codename One due to mobile OS restrictions on file systems that java.io wasn't designed to handle see this for the long form explanation.
We have a sample that does exactly this taken from here:
Form hi = new Form("PDF Viewer", BoxLayout.y());
Button devGuide = new Button("Show PDF");
devGuide.addActionListener(e -> {
FileSystemStorage fs = FileSystemStorage.getInstance();
String fileName = fs.getAppHomePath() + "pdf-sample.pdf";
if(!fs.exists(fileName)) {
Util.downloadUrlToFile("http://www.polyu.edu.hk/iaee/files/pdf-sample.pdf", fileName, true);
}
Display.getInstance().execute(fileName);
});
hi.add(devGuide);
hi.show();

Spring web application save file to server

I build a web application.
I would like to read files from server, then generate PDF file ( with itText) then save it to the server.
I don't know how to locate the files from the server then save the file to the server.
I read from my PC and write data to my PC perfectly.
The above code works properly but just on my computer not at server.
String jspPath = "C:\\Users\\dave\\Desktop\\eclipse\\project\\";
String fileName = "CV.txt";
InputStreamReader ir = new InputStreamReader(new FileInputStream(jspPath+filename), "UTF-8");
// Then generate the PDF with iText
//and
FileOutputStream fs = new FileOutputStream(jspPath+"generated.pdf");
PdfWriter pdfWriter = new PdfWriter(fs);
PdfDocument pdfdoc = new PdfDocument(pdfWriter);
JSP path references to my folder not the link with the generated pdf.
I would like :
put CV.txt to the server and read it.
Generate pdf ( it will work).
Save the generated PDF to server
A link to the generated PDF which i can download.
Thanks in Advance
Here are few things which might help you.
You can use FormData to pass text file from Frontend to Backend.
Use ajax post call to pass data.
you will have entire file on backend in the RequestContext parameter as FileItem object. you can start reading file using InputStreamReader.
than convert it into pdf file.
you can save pdf file to java temporary directory
String temporaryDir = System.getProperty("java.io.tmpdir");
this will return path for java temporary directory and you can delete this pdf file later
you will have to create ResponseBuilder with content-type='application/pdf' to download as a pdf file and return it to the UI. read this post
Hope this information helps you to solve your issue!

Using iText to open PDF documents after writing to them

Im using iText and the Document class in a JFrame to write PDFs but if i try to use the Runtime class to run it after creation i get an exception that i cant open it due to the locks still on it and if i run Unlocker on it, my JFrame has a lock token on it. How do i open the PDF if i want to write to it?
Document d = new Document();
.... code
d.close();
Runtime.getRuntime().exec("D:/PDFChartStuff.pdf");
Why does this post not meet stackoverflow's "quality standards"?
If you are using windows you can use rundll32 to launch a pdf file.
Try something like this
String pdfFile="D:/PDFChartStuff.pdf";
if (pdfFile.toString().endsWith(".pdf")) {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
} else {
//For cross platform use
Desktop desktop = Desktop.getDesktop();
desktop.open(pdfFile);
}

Open "byte array document" from a Java applet

I've a signed applet that retrieves a PDF document from a web service, then stores it on a temp folder, and opens it on Adobe Reader. I would like to avoid storing the file locally, but I really don't know how to achieve it (I'm a newbie with Java applets).
If it were a web application (i.e. a simple servlet), I could just write the PDF content over the ServletResponse; then the browser would store it on its temporary folder, and open it with Adobe Reader (or whatever application is associated with the MIME type).
Is there a similar way to do this... on a Java applet?
This is my code so far:
public class MyListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Retrieve the document contents
byte[] content = webService.getPdfDocument(...);
// Write to file
File f = new File("my-document-filename.pdf");
FileOutputStream fos = new FileOutputStream(f);
fos.write(content);
fos.close();
// Open the file
Desktop.getDesktop().open(new File("my-document-filename.pdf"));
}
}
Any alternative to Desktop.open(File), allowing me to pass a byte[] instead of a File?
Adobe reader can handle URL:s, so it could be a way forward to create a temporary (?) URL for the document.
Otherwise you can create a temporary file use File.createTempFile, from the API:
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:
The file denoted by the returned abstract pathname did not exist before this method was invoked, and
Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method.
So in your case, instead of creating a new file yourself you can use this method:
File f = File.createTempFile("tmp", ".pdf");
f.deleteOnExit(); // deletes the file on exit
...

Categories