]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.wiki.ui/src/org/simantics/wiki/ui/editor/WikiExportPDFAction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.wiki.ui / src / org / simantics / wiki / ui / editor / WikiExportPDFAction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.wiki.ui.editor;
13
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.FileDialog;
21 import org.eclipse.swt.widgets.Shell;
22
23 import com.lowagie.text.Document;
24 import com.lowagie.text.DocumentException;
25 import com.lowagie.text.PageSize;
26 import com.lowagie.text.pdf.PdfWriter;
27
28 public class WikiExportPDFAction extends Action {
29
30         final IExportable exportable;
31         
32         public WikiExportPDFAction(IExportable exportable) {
33                 this.exportable = exportable;
34         }
35         
36         public void run() {
37                 
38                 Shell shell = Display.getCurrent().getActiveShell();
39
40         FileDialog fd = new FileDialog(shell, SWT.SAVE);
41         fd.setText("Save PDF");
42         fd.setFilterPath("C:/");
43         String[] filterExt = { "*.pdf", "*.*" };
44         fd.setFilterExtensions(filterExt);
45         String filename = fd.open();
46         if (filename == null)
47             return;
48                 exportPDF(filename);
49                 
50         }
51         
52         protected void exportPDF(String filename) {             
53                 Document document = new Document(PageSize.A4,36,36,36,36);
54                 try {
55                         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
56                         document.open();
57                         exportable.export(document, writer);
58                 } catch (FileNotFoundException e) {
59                         // TODO Auto-generated catch block
60                         e.printStackTrace();
61                 } catch (DocumentException e) {
62                         // TODO Auto-generated catch block
63                         e.printStackTrace();
64                 } finally {
65                         document.close();
66                 }
67         }
68 }