Java 打印PPT幻灯片

本篇文章将介绍通过Java程序打印PPT幻灯片的方法。包括打印幻灯片的所有页、打印幻灯片中的指定页面。

使用工具:Free Spire.Presentation for Java (免费版)

Jar文件获取及导入:

方法1:通过官网下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Presentation.jar文件导入java程序。

方法2可通过maven仓库安装导入。可参考导入方法

Java示例代码

【示例1】静默打印PPT所有页面

import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintPPT {
    public static void main(String[] args) throws Exception {
    String inputFile = "Sample.pptx";

    //加载文档
    Presentation presentation = new Presentation();
    presentation.loadFromFile(inputFile);

    //使用默认打印机打印所有文档中的所有幻灯片
    PresentationPrintDocument document = new PresentationPrintDocument(presentation);
    document.print();
    presentation.dispose();

    }
}

【示例2】不连续打印部分幻灯片

import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintPPT {
    public static void main(String[] args) throws Exception {
        String inputFile = "Sample.pptx";

        //加载文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile(inputFile);
        
        PresentationPrintDocument document = new PresentationPrintDocument(presentation);

        //选择需要打印的幻灯片
        document.selectSlidesForPrint("1", "2-6");
        document.print();
        presentation.dispose();

        }
    }

 

(本文完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; import java.io.*; public class SR_Slider extends Frame implements ActionListener, WindowListener, Runnable { public static void main(String Pagli[]) { new SR_Slider(); } Thread time; //new thread File fl; URL dir; MediaTracker mt; //here you can use an array Image p1, p2, p3, p4, p5, p6, p7; About ab; //instance of About class Panel bottom; AudioClip b_n, rfs; Toolkit tk; Dimension dim; Button back, next, refresh, auto, abt, ext; Color sky; Font f; int count, x; public SR_Slider() { setTitle("SR-Slider 1.0"); setLayout(new BorderLayout()); bottom = new Panel(); time = new Thread(this); sky = new Color(0,140,255); //color - skyblue f = new Font("Courier",Font.BOLD,16); count = 0; x = 0; mt=new MediaTracker(this); try { //getting all images and add them to the MediaTracker p1 = Toolkit.getDefaultToolkit().getImage("Image/p1.jpg"); mt.addImage(p1, 0); p2 = Toolkit.getDefaultToolkit().getImage("Image/p2.jpg"); mt.addImage(p2, 1); p3 = Toolkit.getDefaultToolkit().getImage("Image/p3.jpg"); mt.addImage(p3, 2); p4 = Toolkit.getDefaultToolkit().getImage("Image/p4.jpg"); mt.addImage(p4, 3); p5 = Toolkit.getDefaultToolkit().getImage("Image/p5.jpg"); mt.addImage(p5, 4); p6 = Toolkit.getDefaultToolkit().getImage("Image/p6.jpg"); mt.addImage(p6, 5); p7 = Toolkit.getDefaultToolkit().getImage("Image/p7.jpg"); mt.addImage(p7, 6); fl = new File("user.dir"); dir = fl.toURL(); b_n = Applet.newAudioClip(new URL(dir, "Sound/bk_nxt.au")); rfs = Applet.newAudioClip(new URL(dir, "Sound/rfsh.au")); } catch(MalformedURLException e) { //do nothing! } //designing back button back=new Button("Back"); back.setFont(f); back.setBackground(sky); back.setForeground(Color.white); //designing next button next=new Button("Next"); next.setFont(f); next.setBackground(sky); next.setForeground(Color.white); //designing refresh button refresh=new Button("Reset"); refresh.setFont(f); refresh.setBackground(sky); refresh.setForeground(Color.white); //designing auto button auto=new Button("SlideShow"); auto.setFont(f); auto.setBackground(sky); auto.setForeground(Color.white); //designing abt button abt=new Button("About"); abt.setFont(f); abt.setBackground(sky); abt.setForeground(Color.white); //designing ext button ext=new Button("Exit"); ext.setFont(f); ext.setBackground(sky); ext.setForeground(Color.white); //adding ActionListener to all buttons back.addActionListener(this); next.addActionListener(this); refresh.addActionListener(this); auto.addActionListener(this); abt.addActionListener(this); ext.addActionListener(this); //adding all components to the Frame bottom.add(back); bottom.add(next); bottom.add(refresh); bottom.add(auto); bottom.add(abt); bottom.add(ext); add(bottom, BorderLayout.SOUTH); addWindowListener(this); tk = this.getToolkit(); dim = tk.getScreenSize(); //getting the current screen resolution setBounds(dim.width/6, dim.height/6, 540, 420); setResizable(false); setVisible(true); } public void paint(Graphics g) { if(count < 1) g.drawImage(p1, 0, 0, this); if(count == 1) g.drawImage(p2, 0, 0, this); if(count == 2) g.drawImage(p3, 0, 0, this); if(count == 3) g.drawImage(p4, 0, 0, this); if(count == 4) g.drawImage(p5, 0, 0, this); if(count == 5) g.drawImage(p6, 0, 0, this); if(count == 6) g.drawImage(p7, 0, 0, this); } public void update(Graphics g) { paint(g); } public void actionPerformed(ActionEvent ae) { if(ae.getSource() == ext) { dispose(); setVisible(false); System.exit(0); } if(ae.getSource() == next) { b_n.play(); if(count < 7) { count++; repaint(); } } if(ae.getSource() == back) { b_n.play(); if(count == 0) count=0; if(count > 0) { count--; repaint(); } } if(ae.getSource() == refresh) { rfs.play(); time.suspend(); //suspends the thread count=0; repaint(); } if(ae.getSource() == auto) { b_n.play(); if(x < 1) { time.start(); x++; } else { time.resume(); //resumes the thread } } if(ae.getSource() == abt) { ab = new About(); } } public void run() { try { for(int i=1;i>0;i++) { b_n.play(); time.sleep(3000); count++; if(count > 6) { time.suspend(); //suspends the thread count = 0; } repaint(); } } catch(InterruptedException e) { //do nothing! } } //handling window events public void windowClosing(WindowEvent we) { dispose(); setVisible(false); System.exit(0); } public void windowClosed(WindowEvent we) { //do nothing! } public void windowOpened(WindowEvent we) { //do nothing! } public void windowActivated(WindowEvent we) { //do nothing! } public void windowDeactivated(WindowEvent we) { //do nothing! } public void windowIconified(WindowEvent we) { //do nothing! } public void windowDeiconified(WindowEvent we) { //do nothing! } class About extends Frame implements WindowListener { Font f, f2; private About() { setTitle("About"); setLayout(new FlowLayout()); f = new Font("Verdana", Font.BOLD, 20); f2 = new Font("Verdana", Font.PLAIN, 12); addWindowListener(this); tk = this.getToolkit(); dim = tk.getScreenSize(); //getting the current screen resolution setBounds(dim.width/4, dim.height/4, 300, 190); setResizable(false); setVisible(true); } public void paint(Graphics g) { g.setColor(Color.red); g.setFont(f); g.drawString("SR-Slider 1.0", 10, 60); g.setColor(Color.black); g.setFont(f2); g.drawString("Author: 雪碧的朋友", 10, 90); g.drawString("email:jing_feng70@163.com", 10, 110); g.drawString("谢谢使用", 10, 130); g.drawString("(C) ShuvoRim Pvt. Ltd.", 10, 150); g.drawString("All rights reserved.", 10, 170); } //handling window events public void windowClosing(WindowEvent we) { dispose(); setVisible(false); } public void windowClosed(WindowEvent we) { //do nothing! } public void windowOpened(WindowEvent we) { //do nothing! } public void windowActivated(WindowEvent we) { //do nothing! } public void windowDeactivated(WindowEvent we) { //do nothing! } public void windowIconified(WindowEvent we) { //do nothing! } public void windowDeiconified(WindowEvent we) { //do nothing! } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值