简体   繁体   English

使用Java BufferedWriter在Word文档中保存一个http链接

[英]Save a http link in word document with java BufferedWriter

i want to save a http link in word document using java application (file.doc) .i succesfully write the link to the word document using BufferedWriter class but the link in word document is just simple line of text . 我想使用Java应用程序(file.doc)在Word文档中保存一个http链接。我使用BufferedWriter类将链接成功写入该Word文档,但是Word文档中的链接只是简单的文本行。 if you press enter button on the document word it turns to http link and that's what i want to do with my java application , save as http format 如果您按文档单词上的回车按钮,它将变成http链接,这就是我要使用我的java应用程序做的,另存为http格式

here is my code : 这是我的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.JTextField;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Save_file extends JFrame {

    private JPanel contentPane;
    public static JTextField textField;
    JFileChooser chooser;
    File file;
    public static FileWriter fw;
    public static BufferedWriter output;
    String the_past;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Save_file frame = new Save_file();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Save_file() {
        setResizable(false);

        setIconImage(Toolkit.getDefaultToolkit().getImage(Save_file.class.getResource("/Buttons/LogoIcon.png")));
        setTitle("Saving file");
        this.setSize(435, 300);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //  setBounds(100, 100, 435, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("Select a File :");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblNewLabel.setBounds(12, 13, 152, 25);
        contentPane.add(lblNewLabel);

        JButton btnNewButton = new JButton("");
        btnNewButton.setIcon(new ImageIcon(Save_file.class.getResource("/Buttons/loaddisk (2).gif")));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                try{


                    output = new BufferedWriter(fw);
                    output.write(textField.getText()+"\n");
                    output.write(" ");
                    JOptionPane.showMessageDialog(null, "Saving succes", "Save", JOptionPane.INFORMATION_MESSAGE);
                    //textField.setText("");
                    output.write("\r");
                    output.flush();
                    output.close();


                }
                catch(Exception io){
                    JOptionPane.showMessageDialog(null, "Saving Failed", "Error", JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        btnNewButton.setBounds(142, 211, 32, 32);
        contentPane.add(btnNewButton);

        JLabel lblLink = new JLabel("Link   ");
        lblLink.setToolTipText("Just click once to past");
        lblLink.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblLink.setBounds(12, 107, 97, 25);
        contentPane.add(lblLink);

        textField = new JTextField();
        textField.setToolTipText("Just click once to past");
        textField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                the_past= textField.getText();
                if (the_past.isEmpty()){
                textField.paste();
                }
                //else
                    //textField.setText("");
            }
        });
        textField.setColumns(10);
        textField.setBounds(12, 154, 393, 33);
        contentPane.add(textField);

        JButton btnOpenFile = new JButton("Open File");
        btnOpenFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

               try{

                    chooser = new JFileChooser();
                    int retval = chooser.showOpenDialog(Save_file.this);


                    if (retval == JFileChooser.APPROVE_OPTION) {
                        //... The user selected a file, get it, use it.
                        File file = chooser.getSelectedFile();
                        System.out.println("File equals"+file);
                        fw = new FileWriter(file, true);


                    }


                }
                catch(Exception io){


                }
            }

        });
        btnOpenFile.setBounds(12, 53, 97, 25);
        contentPane.add(btnOpenFile);

        JButton button = new JButton("");
        button.setIcon(new ImageIcon(Save_file.class.getResource("/Buttons/cc.jpg.png")));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        button.setBounds(243, 211, 35, 35);
        contentPane.add(button);
    }
}

保存示例链接的结果结果应该是

No, you are not doing that. 不,您没有这样做。

You are writting a text (ASCII or UTF) file with the String that you use, and assign a .doc extension to the file. 您正在使用所使用的字符串编写文本(ASCII或UTF)文件,并为文件指定.doc扩展名。 A .doc extension does not make your file a word document . .doc扩展名不会使您的文件成为word文档 MS Word can read automatically text files and show it, but you are not writting (I repeat) a Word file. MS Word可以自动读取并显示文本文件,但是您不是在写(我重复)Word文件。

Search for a API that allows manipulating/creating Word files to do that (I think Apache has one of them). 搜索允许操作/创建Word文件的API(我认为Apache就是其中之一)。

As it has already been stated, you create a simple plain-text document, which doesn't support hyperlinks. 如前所述,您将创建一个不支持超链接的简单纯文本文档。 You should take a look at Apache POI library to create a Rich Text Format document. 您应该查看Apache POI库以创建RTF格式文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM