The solution to this idea is:
1. Generate a PDF of Word first, using OpenOffice or Jacob
2. Then create a picture of the PDF
The specific code is as follows:
private void Officetopdf () {
Openofficeconnection connection = new socketopenofficeconnection (8100);
try {
Connection.connect ();
} catch (Connectexception e) {
E.printstacktrace ();
}
Documentconverter converter = new Openofficedocumentconverter (
connection);
Converter.convert (Officefile, pdffile);
Close the connection
Connection.disconnect ();
}
Convert files in PDF format to JPG files
private void Pdftojpg (String inputfile)
Throws IOException {
Load a PDF from a byte buffer
File File = new file (inputfile);
Randomaccessfile RAF = new Randomaccessfile (file, "R");
FileChannel channel = Raf.getchannel ();
This code channel establishes a map map, and if you want to delete the file then contact map
Bytebuffer buf = Channel.map (FileChannel.MapMode.READ_ONLY, 0,
Channel.size ());
Pdffile pdffile = new Pdffile (BUF);
int totalpage =pdffile.getnumpages ();
for (int i = 1; I <= totalpage; i++) {
if (i = = 1) {
Draw the first page to an image
Describe the homepage in the form of a picture
Pdfpage page = Pdffile.getpage (i);
Rectangle rect = new Rectangle (0, 0, (int) Page.getbbox ()
. GetWidth (), (int) Page.getbbox (). GetHeight ());
Generate the image
Create a picture
Image img = page.getimage (rect.width, Rect.height,//Width &
Height
Rect,//clip rect
NULL,//null for the ImageObserver
True,//fill background with white
True//block until drawing is done
);
BufferedImage tag = new BufferedImage (Rect.width, Rect.height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Tag.getgraphics (). DrawImage (Img.getscaledinstance (Rect.width, Rect.height, Image.scale_smooth), 0, 0, Rect.width, Rect.height,
NULL);
FileOutputStream out = new FileOutputStream (imagepath+ "\ \" +filename.substring (Filename.lastindexof ("/") +1)
+ ". jpg"); Output to file stream
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (tag); JPEG encoding
Turn off the output stream
Out.close ();
Break
}
}
Buf.clear ();
Channel.close ();
Raf.close ();
Unmap (BUF);
File.delete ();
}
Unlock map map
public static <T> void Unmap (final Object buffer) {
Accesscontroller.doprivileged (New privilegedaction<t> () {
@Override
Public T Run () {
try {
Method Getcleanermethod = Buffer.getclass (). GetMethod ("cleaner", new Class[0]);
Getcleanermethod.setaccessible (TRUE);
Sun.misc.Cleaner Cleaner = (sun.misc.Cleaner) getcleanermethod.invoke (buffer, new object[0]);
Cleaner.clean ();
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}
});
}
Note that I generated the picture after the deletion of the PDF, but the deletion failed, after Daniel's guidance plus the unmap is OK, so write down to share to everyone.
The required jar packages are Pdfrenderer.jar and Jodconverter-2.2.2.jar packages, and if you use Jacob you have to join Jacob.jar
Java generates a thumbnail image in Microsoft Excel