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