Autoprinting an iText-generated PDF - java

Is there a way to send a newly iText-generated PDF directly to the printer ? Preferably in the same method that is used to generate it and save it to disc ?

I tried and this works fine after you have saved the file in the disk.
import com.lowagie.tools.Executable;
try{
Executable ex = new Executable();
ex.openDocument(fileName);
ex.printDocument(fileName);
}catch(IOException e){
e.printStackTrace();
}
For more info, see this Daniweb forum conversation.

Related

How to open an Excel file in default .xslx app using Java?

I created an excel file using POI but now I need to open it using the default .xslx app (MS Excel in my case). I want to specify that I don't need to parse it or read its contents, but just open it in MS Excel.
I figured it out after a while, putting this here in case anyone googles it and this page comes up :
try {
File excelFile = new File(filePath);
Desktop.getDesktop().open(excelFile);
} catch (IOException e) {
e.printStackTrace();
}

Issues reading/writing images

I have been working on code to encrypt and decrypt images via a Spring webapp in which i use Thymeleaf. I have tested each method involved in the process and each one is sound. The way the process flows is supposed to be:
User1 uploads image and key, app breaks down image into pixels and then into 128-bit stateHexes, stateHexes are encrypted using AES, and rebuilt into encrypted image to be displayed to User1 for download. User1 then sends image to User2 who then downloads image and decrypts it using the same process.
When I treat it like the above scenario the image somehow changes after storing it. However, if i treat it as follows the file encrypts and decrypts perfectly.
So I assume the issue must lie in either saving the file or retrieving the file, or some other issue pertaining to file handling.
Below I will show code snippets of how i load and save the images.
This is how I get the image
// Get the filename and path for the image passed in from user
Path fileNameandPath = Paths.get(uploadDirectory, file.getOriginalFilename());
// Generate the file and write the image onto it
File imageFile = fileNameandPath.toFile();
BufferedImage image = null;
try {
Files.write(fileNameandPath, file.getBytes());
image = ImageIO.read(imageFile);
Files.delete(fileNameandPath);
} catch (IOException e) {
e.printStackTrace();
System.out.println("COULDNT READ IMAGE AT = " + fileNameandPath);
}
And this is how I create the outputImage
String outputFilePath = new File("src/main/resources/static").getAbsolutePath() + "\\output.jpg";
File outputFile = new File(outputFilePath);
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("FILE ALREADY EXISTS");
}
// Write the image to the outputfile
try {
ImageIO.write(outputImage, "jpg", outputFile);
} catch (IOException e) {
e.printStackTrace();
}
I pass in an all black image (each pixel's hex code is 000000) and it reads the image and encrypts as expected, but when decrypting it reads in the previous image wrong, leading me to believe that it's not the code but most likely how the file is stored/retrieved? I only have one guess: that somehow the file is getting mixed up. If anyone has any clues as to why this happening, any help or leads would be appreciated.
I think it may have to do with how I write to the image. Reading the image is no problem, but how I write to the image seems to alter it in a way.
After getting some sleep, I realized that because I need the image to remain the same when reading and writing, I was losing bits of the image due to the lossy nature of .jpg files. Now that I save my file as a .png I get no errors. Hope this helps someone down the road!

Is there a way to view rptdesign as PDF without creating PDF file first?

Currently I'm viewing birt rptdesign as PDF by creating to PDF file first as follow :
design = engine.openReportDesign("c:/temp/customer.rptdesign");
task = engine.createRunAndRenderTask(design);
PDFRenderOption options = new PDFRenderOption();
options.setOutputFileName("c:/temp/customer.PDF");
options.setOutputFormat("PDF");
Then I open it with desktop as follow:
File file = new File("c:/temp/customer.PDF");
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {e.printStackTrace();}
Is there a way to view it as PDF directly without creating PDF file first?
I'm using jdk-8u181-windows-i586 and birt-runtime-4.8.0-20180626.
Thanks.
You can use the sample servlet, or write your own servlet.

copy database file from MyDocuments

i have a sample code to copy a file from one location to another. am using that as a backup for an SQLite database file which copies the file from My Documents folder to the desktop.. the sample code is below:
try{
DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyy/MM/dd HHmmss");
String sys =System.getProperty("user.home");
String fileurl = sys+"\\Desktop\\school database backup";
File dest = new File(fileurl+date.format(java.time.LocalDateTime.now()));dest.mkdir();
String sys1 =System.getProperty("user.home");
File source = new File(sys1+"\\Documents\\hyper-DB\\intellect");
FileUtils.copyDirectory(source, dest);
JOptionPane.showMessageDialog(null,"Backup Successful");
}
catch(IOException e){
e.getMessage();
} finally{
try{
rs.close();
pst.close();
}
catch(Exception e){
}
}
this code works perfectly on my my pc which I used to create the project. but when I install it on another user's pc. it doesn't work. and am sure its unable to locate the database in My Documents folder though its there. please what do I do to make it work on another pc. thank you
As with every programming problem there are multiple ways to solve the problem. I will try to explain a couple of ways I would solve your problem. If you are heck bent on using the copy function from FileUtils. Have the entire project in a dedicated folder, with all the files in, you can even have sub-folders, this will keep thing consistent and you won't have to worry about file permissions, like the ones found on school computers. Another way to achieve the same out come is to read the main database and copy the data to the backup database one element at a time. This can also let you have the main database in the same directory as the backup database. It wouldn't hurt to have it all in a single folder too.

Detecting a PDF Package or Portfolio in code

Does anyone know of a way to detect whether a given PDF file is a PDF Portfolio or a PDF Package, rather than a "regular" PDF? I'd prefer Java solutions, although since I haven't yet found any information on detecting the specific type of PDF, I'll take what I can get and they try to figure out the Java solution afterwards.
(In searching past questions, it appears that a bunch of folks don't know that such things as PDF Portfolios and PDF Packages exist. Generally, they're both ways that Adobe allows multiple, discrete PDFs to be packaged into a single PDF file. Opening a PDF Package in Reader shows the user a list of the embedded PDFs and allows further viewing from there. PDF Portfolios appear to be a bit more complicated -- they also include Flash-based browser for the embedded files, and then allow users to extract the discrete PDFs from there. My issue with them, and the reason I'd like to be able to detect them in code, is because OS X's built-in Preview.app can't read these files -- so I'd like to at least warn users of a web app of mine that uploading them can lead to diminished compatibility across platforms.)
This question is old, but in-case someone wants to know, it is possible. It can be done with Acrobat and JavaScript by using the following command.
if (Doc.collection() != null)
{
//It Is Portfolio
}
Acrobat JavaScript API says, "A collection object is obtained from the Doc.collection property. Doc.collection returns a null value when there is no PDF collection (also called PDF package and PDF portfolio).The collection object is used to set the initial document in the collection, set the initial view of the collection, and to get, add, and remove collection fields (or categories)."
I'm also facing same problem while extracting data through kofax, but i got solution and its working fine need to add extra jar for Document class.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class PDFPortfolio {
/**
* #param args
*/
public static void main(String[] args) {
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("e:/pqr1.pdf");
// get collection of embedded files
com.aspose.pdf.EmbeddedFileCollection embeddedFiles = pdfDocument.getEmbeddedFiles();
// iterate through individual file of Portfolio
for(int counter=1; counter<=pdfDocument.getEmbeddedFiles().size();counter++)
{
com.aspose.pdf.FileSpecification fileSpecification = embeddedFiles.get_Item(counter);
try {
InputStream input = fileSpecification.getContents();
File file = new File(fileSpecification.getName());
// create path for file from pdf
// file.getParentFile().mkdirs();
// create and extract file from pdf
java.io.FileOutputStream output = new java.io.FileOutputStream("e:/"+fileSpecification.getName(), true);
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer)))
output.write(buffer, 0, n);
// close InputStream object
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Categories

Resources