java
doc,xlsx,pptx to pdf
public class Word2Pdf {
static final int wdDoNotSaveChanges = 0;
static final int wdFormatPDF = 17;
static final int ppSaveAsPDF = 32;
public static void main(String[] args) throws IOException {
String source1 = "c:/test/test2.docx";
String source2 = "c:/test/test1.xlsx";
String source3 = "c:/test/test3.pptx";
String target1 = "c:/test/test1.pdf";
String target2 = "c:/test/test2.pdf";
String target3 = "c:/test/test3.pdf";
Word2Pdf pdf = new Word2Pdf();
pdf.word2pdf(source1, target1);
pdf.excel2pdf(source2, target2);
pdf.ppt2pdf(source3, target3);
// pdf.imgToPdf("e:/12345.jpg","e:/xu.pdf");
}
public void word2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs,//
"Open", //
source,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
target, // FileName
wdFormatPDF);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
}
public void ppt2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Powerpoint.Application");
Dispatch presentations = app.getProperty("Presentations")
.toDispatch();
Dispatch presentation = Dispatch.call(presentations,//
"Open", source, // FileName
true,// ReadOnly
true,// Untitled
false // WithWindow
).toDispatch();
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation,//
"SaveAs", //
target, // FileName
ppSaveAsPDF);
Dispatch.call(presentation, "Close");
long end = System.currentTimeMillis();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (app != null)
app.invoke("Quit");
}
}
public void excel2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); //
try {
app.setProperty("Visible", false);
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
System.out.println(" source " + source);
Dispatch workbook = Dispatch.invoke(
workbooks,
"Open",
Dispatch.Method,
new Object[] { source, new Variant(false),
new Variant(false) }, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
target, new Variant(57), new Variant(false),
new Variant(57), new Variant(57), new Variant(false),
new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) }, new int[1]);
Variant f = new Variant(false);
System.out.println(" target " + target);
Dispatch.call(workbook, "Close", f);
long end = System.currentTimeMillis();
System.out.println((end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (app != null) {
app.invoke("Quit", new Variant[] {});
}
}
}
public boolean imgToPdf(String imgFilePath, String pdfFilePath)
throws IOException {
File file = new File(imgFilePath);
if (file.exists()) {
Document document = new Document();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(document, fos);
document.addAuthor("arui");
document.addSubject("test pdf.");
document.setPageSize(PageSize.A4);
document.open();
// document.add(new Paragraph("JUST TEST ..."));
Image image = Image.getInstance(imgFilePath);
float imageHeight = image.getScaledHeight();
float imageWidth = image.getScaledWidth();
int i = 0;
while (imageHeight > 500 || imageWidth > 500) {
image.scalePercent(100 - i);
i++;
imageHeight = image.getScaledHeight();
imageWidth = image.getScaledWidth();
System.out.println("imageHeight->" + imageHeight);
System.out.println("imageWidth->" + imageWidth);
}
image.setAlignment(Image.ALIGN_CENTER);
// image.setAbsolutePosition(0, 0);
// image.scaleAbsolute(500, 400);
document.add(image);
} catch (DocumentException de) {
System.out.println(de.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
document.close();
fos.flush();
fos.close();
return true;
} else {
return false;
}
}
}
pom.xml
<dependency> <groupId>net.sf.jacob-project</groupId> <artifactId>jacob</artifactId> <version>1.14.3</version> </dependency>
댓글 쓰기
0 댓글