1 package org.simantics.export.core.pdf;
\r
4 import java.io.FileNotFoundException;
\r
5 import java.io.FileOutputStream;
\r
6 import java.io.IOException;
\r
7 import java.util.ArrayList;
\r
8 import java.util.List;
\r
9 import java.util.StringTokenizer;
\r
10 import java.util.concurrent.atomic.AtomicBoolean;
\r
11 import java.util.regex.Pattern;
\r
13 import org.eclipse.core.runtime.IProduct;
\r
14 import org.eclipse.core.runtime.Platform;
\r
15 import org.eclipse.core.runtime.preferences.DefaultScope;
\r
16 import org.eclipse.core.runtime.preferences.InstanceScope;
\r
17 import org.osgi.service.prefs.Preferences;
\r
18 import org.simantics.databoard.Accessors;
\r
19 import org.simantics.databoard.Bindings;
\r
20 import org.simantics.databoard.Datatypes;
\r
21 import org.simantics.databoard.accessor.RecordAccessor;
\r
22 import org.simantics.databoard.accessor.UnionAccessor;
\r
23 import org.simantics.databoard.accessor.error.AccessorConstructionException;
\r
24 import org.simantics.databoard.accessor.error.AccessorException;
\r
25 import org.simantics.databoard.accessor.error.ReferenceException;
\r
26 import org.simantics.databoard.accessor.reference.ChildReference;
\r
27 import org.simantics.databoard.accessor.reference.LabelReference;
\r
28 import org.simantics.databoard.binding.mutable.Variant;
\r
29 import org.simantics.databoard.forms.DataboardForm;
\r
30 import org.simantics.databoard.type.DoubleType;
\r
31 import org.simantics.databoard.type.RecordType;
\r
32 import org.simantics.databoard.type.StringType;
\r
33 import org.simantics.databoard.type.UnionType;
\r
34 import org.simantics.export.core.ExportContext;
\r
35 import org.simantics.export.core.error.ExportException;
\r
36 import org.simantics.export.core.intf.FormatClass;
\r
37 import org.simantics.export.core.manager.Content;
\r
38 import org.simantics.export.core.util.ExporterUtils;
\r
39 import org.simantics.utils.page.MarginUtils;
\r
40 import org.simantics.utils.page.MarginUtils.Margin;
\r
41 import org.simantics.utils.page.MarginUtils.Margins;
\r
42 import org.simantics.utils.page.PageDesc;
\r
43 import org.simantics.utils.page.PageOrientation;
\r
45 import com.lowagie.text.Document;
\r
46 import com.lowagie.text.DocumentException;
\r
47 import com.lowagie.text.FontFactory;
\r
48 import com.lowagie.text.Rectangle;
\r
49 import com.lowagie.text.pdf.DefaultFontMapper;
\r
50 import com.lowagie.text.pdf.PdfBoolean;
\r
51 import com.lowagie.text.pdf.PdfCopy;
\r
52 import com.lowagie.text.pdf.PdfName;
\r
53 import com.lowagie.text.pdf.PdfWriter;
\r
56 * Represents the actions to Pdf File format in Export Core.
\r
58 * @author toni.kalajainen@semantum.fi
\r
60 public class ExportPdfFormat implements FormatClass {
\r
62 public static final Pattern PATTERN_PDF = Pattern.compile(".*\\.pdf$", Pattern.CASE_INSENSITIVE);
\r
64 // Options references
\r
65 public static String S_PDF = "Portable Document Format (PDF)";
\r
66 public static ChildReference P_PDF_TITLE = ChildReference.parsePath(S_PDF+"/Title");
\r
67 public static ChildReference P_PDF_AUTHOR = ChildReference.parsePath(S_PDF+"/Author");
\r
68 public static ChildReference P_PDF_SUBJECT = ChildReference.parsePath(S_PDF+"/Subject");
\r
69 public static ChildReference P_PDF_KEYWORDS = ChildReference.parsePath(S_PDF+"/Keywords");
\r
70 public static ChildReference P_PDF_COMPRESSION = ChildReference.parsePath(S_PDF+"/Compression");
\r
72 public static String S_SIGN = "Digital Signature";
\r
73 public static LabelReference P_SIGN = new LabelReference( S_SIGN );
\r
74 public static ChildReference P_SIGN_KEYSTORE = ChildReference.concatenate(P_SIGN, new LabelReference("Keystore") );
\r
75 public static ChildReference P_SIGN_KEYSTOREPASSWORD = ChildReference.concatenate(P_SIGN, new LabelReference("Keystore Password"));
\r
76 public static ChildReference P_SIGN_PRIVATEKEYPASSWORD = ChildReference.concatenate(P_SIGN, new LabelReference("Private Key Password"));
\r
77 public static ChildReference P_SIGN_LOCATION = ChildReference.concatenate(P_SIGN, new LabelReference("Sign Location"));
\r
78 public static ChildReference P_SIGN_REASON = ChildReference.concatenate(P_SIGN, new LabelReference("Sign Reason"));
\r
80 public static String S_PAGE = "Page Settings";
\r
81 public static ChildReference P_PAGE = new LabelReference( S_PAGE );
\r
82 public static ChildReference P_PAGE_PAPERSIZE = ChildReference.concatenate(P_PAGE, new LabelReference("Size"));
\r
83 public static ChildReference P_PAGE_ORIENTATION = ChildReference.concatenate(P_PAGE, new LabelReference("Orientation"));
\r
84 public static ChildReference P_PAGE_MARGIN_LEFT = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Left"));
\r
85 public static ChildReference P_PAGE_MARGIN_RIGHT = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Right"));
\r
86 public static ChildReference P_PAGE_MARGIN_TOP = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Top"));
\r
87 public static ChildReference P_PAGE_MARGIN_BOTTOM = ChildReference.concatenate(P_PAGE, new LabelReference("Margin Bottom"));
\r
89 // Preference page IDs
\r
90 public static String PAGE_PLUGIN_ID = "org.simantics.modeling.ui";
\r
92 public static String P_DEFAULT_PAGE_SIZE = "page.default.size";
\r
94 private static final AtomicBoolean fontFactoryInitialized = new AtomicBoolean();
\r
96 public static final RecordType pdfOptions;
\r
99 pdfOptions = new RecordType();
\r
100 DoubleType mm = new DoubleType();
\r
104 RecordType pdfRecord = new RecordType();
\r
105 pdfRecord.addComponent("Title", Datatypes.STRING );
\r
106 pdfRecord.addComponent("Author", Datatypes.STRING );
\r
107 pdfRecord.addComponent("Subject", Datatypes.STRING );
\r
108 pdfRecord.addComponent("Keywords", DataboardForm.TEXTBOX );
\r
109 // Formatter options
\r
110 pdfRecord.addComponent("Compression", UnionType.newEnum("0 (No compression)", "1", "2", "3", "4", "5", "6", "7", "8", "9 (Best)"));
\r
113 RecordType pdfSign = new RecordType();
\r
114 StringType certificate = DataboardForm.fileOpenDialog("PKCS#12 keystore (.p12)", "*.p12", "PFX (.pfx)", "*.pfx"); //, "Privacy Enhanced Mail (.pem)", ".pem"
\r
115 pdfSign.addComponent("Keystore", certificate);
\r
116 pdfSign.addComponent("Keystore Password", DataboardForm.PASSWORD);
\r
117 pdfSign.addComponent("Private Key Password", DataboardForm.PASSWORD);
\r
118 pdfSign.addComponent("Sign Location", Datatypes.STRING);
\r
119 pdfSign.addComponent("Sign Reason", Datatypes.STRING);
\r
121 PageDesc[] descs = PageDesc.PDF_ITEMS;
\r
122 String[] pageSizesStr = new String[ descs.length ];
\r
123 for ( int i=0; i<descs.length; i++ ) pageSizesStr[i] = descs[i].getText();
\r
124 UnionType pageSizes = UnionType.newEnum("A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10");
\r
125 UnionType orientation = UnionType.newEnum("Portrait", "Landscape");
\r
126 RecordType pagePrefs = new RecordType();
\r
127 pagePrefs.addComponent("Size", pageSizes);
\r
128 pagePrefs.addComponent("Orientation", orientation);
\r
129 pagePrefs.addComponent("Margin Left", mm);
\r
130 pagePrefs.addComponent("Margin Right", mm);
\r
131 pagePrefs.addComponent("Margin Top", mm);
\r
132 pagePrefs.addComponent("Margin Bottom", mm);
\r
133 //pagePrefs.metadata.put("style", "dialog");
\r
135 pdfOptions.addComponent(S_PDF, pdfRecord);
\r
136 pdfOptions.addComponent(S_SIGN, pdfSign);
\r
137 pdfOptions.addComponent(S_PAGE, pagePrefs);
\r
141 public RecordType options(ExportContext context) throws ExportException {
\r
142 return pdfOptions;
\r
146 public Object createFile(ExportContext ctx, File outputFile, Variant options) throws ExportException {
\r
148 RecordAccessor ra = Accessors.getAccessor( options );
\r
150 if (fontFactoryInitialized.compareAndSet(false, true)) {
\r
151 // Only register directories once.
\r
152 FontFactory.registerDirectories();
\r
155 ExportPdfWriter writer = new ExportPdfWriter();
\r
157 Rectangle pageSize = null;
\r
158 writer.document = new Document(pageSize);
\r
159 writer.outputFile = outputFile;
\r
160 writer.fos = new FileOutputStream(writer.outputFile);
\r
161 writer.pdfCopy = new PdfCopy(writer.document, writer.fos);
\r
162 writer.pdfCopy.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
\r
163 writer.fontMapper = new DefaultFontMapper();
\r
164 writer.options = options;
\r
167 String creator = getCreator();
\r
168 writer.document.addCreator(creator);
\r
171 s = (String) ra.getValue(P_PDF_TITLE, Bindings.STRING);
\r
172 if ( s!=null ) writer.document.addTitle(s);
\r
174 s = (String) ra.getValue(P_PDF_AUTHOR, Bindings.STRING);
\r
175 if ( s!=null ) writer.document.addAuthor(s);
\r
177 s = (String) ra.getValue(P_PDF_SUBJECT, Bindings.STRING);
\r
178 if ( s!=null ) writer.document.addSubject(s);
\r
180 s = (String) ra.getValue(P_PDF_KEYWORDS, Bindings.STRING);
\r
181 if ( s!=null ) writer.document.addKeywords(s);
\r
183 // Compression Level
\r
185 UnionAccessor ua = ra.getComponent(P_PDF_COMPRESSION);
\r
186 int tag = ua.getTag();
\r
187 writer.pdfCopy.setCompressionLevel( tag );
\r
188 writer.compressionLevel = tag;
\r
189 } catch (ReferenceException re) {
\r
194 Margins margins = MarginUtils.MARGINS_10mm;
\r
195 Double left = (Double) ra.getValue(P_PAGE_MARGIN_LEFT, Bindings.DOUBLE);
\r
196 if ( left!=null ) margins = margins.withSide(Margins.LEFT, new Margin(0, 0, left));
\r
197 Double right = (Double) ra.getValue(P_PAGE_MARGIN_RIGHT, Bindings.DOUBLE);
\r
198 if ( right!=null ) margins = margins.withSide(Margins.RIGHT, new Margin(0, 0, right));
\r
199 Double bottom = (Double) ra.getValue(P_PAGE_MARGIN_BOTTOM, Bindings.DOUBLE);
\r
200 if ( bottom!=null ) margins = margins.withSide(Margins.BOTTOM, new Margin(0, 0, bottom));
\r
201 Double top = (Double) ra.getValue(P_PAGE_MARGIN_TOP, Bindings.DOUBLE);
\r
202 if ( top!=null ) margins = margins.withSide(Margins.TOP, new Margin(0, 0, top));
\r
204 String paperName = "A4";
\r
206 UnionAccessor ua = ra.getComponent(P_PAGE_PAPERSIZE);
\r
207 int tag = ua.getTag();
\r
208 paperName = ua.type().getComponent(tag).name;
\r
209 } catch (ReferenceException re) {
\r
212 PageOrientation pageOrientation = PageOrientation.Portrait;
\r
214 UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);
\r
215 int tag = ua.getTag();
\r
216 String str = ua.type().getComponent(tag).name;
\r
217 pageOrientation = PageOrientation.valueOf( str );
\r
218 } catch (ReferenceException re) {
\r
221 PageDesc pd = PageDesc.getByName(paperName);
\r
222 pd = pd.withOrientation(pageOrientation);
\r
223 pd = pd.withMargins(margins);
\r
224 writer.margins = margins;
\r
225 writer.defaultPageDesc = pd;
\r
226 writer.defaultRectangle = ExportPdfWriter.toRectangle( writer.defaultPageDesc );
\r
227 writer.document.setPageSize( writer.defaultRectangle );
\r
230 // Add Developer Extension - not used
\r
231 // PdfName companyName = new PdfName("SMTC");
\r
232 // PdfDeveloperExtension ext = new PdfDeveloperExtension(companyName, PdfWriter.PDF_VERSION_1_7, 3);
\r
233 // writer.addDeveloperExtension( ext );
\r
236 writer.document.open();
\r
237 writer.cb = writer.pdfCopy.getDirectContent();
\r
240 } catch (FileNotFoundException e) {
\r
241 throw new ExportException( e );
\r
242 } catch (DocumentException e) {
\r
243 throw new ExportException( e );
\r
244 } catch (AccessorConstructionException e) {
\r
245 throw new ExportException( e );
\r
246 } catch (AccessorException e) {
\r
247 throw new ExportException( e );
\r
252 public Object openFile(ExportContext context, File inputFile, Variant options) throws ExportException {
\r
254 return new ImportPdfReader(inputFile);
\r
255 } catch (IOException e) {
\r
256 throw new ExportException(e);
\r
261 public List<String> validate(ExportContext context, Variant options) throws ExportException {
\r
262 List<String> problems = new ArrayList<String>();
\r
265 RecordAccessor ra = Accessors.getAccessor( options );
\r
267 // Assert the keystore file exists, if set.
\r
268 String keystoreFileName = (String) ra.getValue(P_SIGN_KEYSTORE, Bindings.STRING);
\r
269 if ( keystoreFileName != null && !keystoreFileName.isEmpty() ) {
\r
270 File keystoreFile = new File( keystoreFileName );
\r
271 if ( !keystoreFile.exists() ) {
\r
272 problems.add( "Keystore file was not found: "+keystoreFile );
\r
276 } catch (AccessorConstructionException e) {
\r
277 throw new ExportException( e );
\r
278 } catch (AccessorException e) {
\r
279 throw new ExportException( e );
\r
286 public void closeFile(ExportContext context, Object handle) throws ExportException {
\r
287 if ( handle instanceof ImportPdfReader ) {
\r
288 ImportPdfReader reader = (ImportPdfReader) handle;
\r
292 if ( handle instanceof ExportPdfWriter ) {
\r
293 ExportPdfWriter writer = (ExportPdfWriter) handle;
\r
298 RecordAccessor ra = Accessors.getAccessor( writer.options );
\r
300 String keystoreFileName = (String) ra.getValue(P_SIGN_KEYSTORE, Bindings.STRING);
\r
301 String keystorePassword = (String) ra.getValue(P_SIGN_KEYSTOREPASSWORD, Bindings.STRING);
\r
302 String privateKeyPassword = (String) ra.getValue(P_SIGN_PRIVATEKEYPASSWORD, Bindings.STRING);
\r
303 String signLocation = (String) ra.getValue(P_SIGN_LOCATION, Bindings.STRING);
\r
304 String signReason = (String) ra.getValue(P_SIGN_REASON, Bindings.STRING);
\r
306 if ( keystoreFileName != null && !keystoreFileName.isEmpty() )
\r
308 File keystoreFile = new File( keystoreFileName );
\r
309 if ( !keystoreFile.exists() ) {
\r
310 throw new ExportException( "Keystore file was not found: "+keystoreFile );
\r
312 writer.sign(keystoreFile, keystorePassword, privateKeyPassword, signLocation, signReason);
\r
315 } catch (AccessorConstructionException e) {
\r
316 throw new ExportException( e.getClass().getName()+": "+e.getMessage() );
\r
317 } catch (AccessorException e) {
\r
318 throw new ExportException( e.getClass().getName()+": "+e.getMessage() );
\r
323 writer.setEncryption(
\r
324 new Certificate[] {},
\r
325 new int[] {PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_PRINTING},
\r
326 PdfWriter.STANDARD_ENCRYPTION_128);
\r
327 //writer.setEncryption(PdfWriter.STANDARD_ENCRYPTION_128, "", "password", PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_ASSEMBLY);
\r
329 int permission = PdfWriter.ALLOW_FILL_IN|PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_ASSEMBLY;
\r
330 PdfEncryption crypto = new PdfEncryption();
\r
331 //for (Certificate c : chain) crypto.addRecipient(c, permission);
\r
332 //crypto.addRecipient(chain[2], permission);
\r
333 crypto.setCryptoMode(PdfWriter.ENCRYPTION_AES_128, 0);
\r
334 crypto.setupByEncryptionKey(key.getEncoded(), key.getEncoded().length*8);
\r
335 crypto.getEncryptionDictionary();
\r
341 public void addAttachment(ExportContext ctx, Object handle, List<Content> attachments) throws ExportException {
\r
342 ExportPdfWriter writer = (ExportPdfWriter) handle;
\r
344 writer.pdfCopy.addViewerPreference(PdfName.USEATTACHMENTS, PdfBoolean.PDFTRUE);
\r
345 for ( Content content : attachments ) {
\r
346 writer.addAttachment( content );
\r
351 public void savePref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
\r
353 RecordAccessor ra = Accessors.getAccessor(options);
\r
356 s = (String) ra.getValue(P_PDF_TITLE, Bindings.STRING);
\r
357 if ( s!=null ) contentScopeNode.put(P_PDF_TITLE.tail().toString(), s);
\r
359 s = (String) ra.getValue(P_PDF_AUTHOR, Bindings.STRING);
\r
360 if ( s!=null ) workbenchScopeNode.put(P_PDF_AUTHOR.tail().toString(), s);
\r
362 s = (String) ra.getValue(P_PDF_SUBJECT, Bindings.STRING);
\r
363 if ( s!=null ) contentScopeNode.put(P_PDF_SUBJECT.tail().toString(), s);
\r
365 s = (String) ra.getValue(P_PDF_KEYWORDS, Bindings.STRING);
\r
366 if ( s!=null ) contentScopeNode.put(P_PDF_KEYWORDS.tail().toString(), s);
\r
368 s = (String) ra.getValue(P_SIGN_KEYSTORE, Bindings.STRING);
\r
369 if ( s!=null ) workbenchScopeNode.put(P_SIGN_KEYSTORE.tail().toString(), s);
\r
371 s = (String) ra.getValue(P_SIGN_KEYSTOREPASSWORD, Bindings.STRING);
\r
372 if ( s!=null ) workbenchScopeNode.put(P_SIGN_KEYSTOREPASSWORD.tail().toString(), s);
\r
374 s = (String) ra.getValue(P_SIGN_PRIVATEKEYPASSWORD, Bindings.STRING);
\r
375 if ( s!=null ) workbenchScopeNode.put(P_SIGN_PRIVATEKEYPASSWORD.tail().toString(), s);
\r
377 s = (String) ra.getValue(P_SIGN_LOCATION, Bindings.STRING);
\r
378 if ( s!=null ) workbenchScopeNode.put(P_SIGN_LOCATION.tail().toString(), s);
\r
380 s = (String) ra.getValue(P_SIGN_REASON, Bindings.STRING);
\r
381 if ( s!=null ) workbenchScopeNode.put(P_SIGN_REASON.tail().toString(), s);
\r
384 UnionAccessor ua = ra.getComponent(P_PDF_COMPRESSION);
\r
385 int tag = ua.getTag();
\r
386 workbenchScopeNode.putInt(P_PDF_COMPRESSION.tail().toString(), tag);
\r
387 } catch (ReferenceException re) {
\r
390 Double d = (Double) ra.getValue(P_PAGE_MARGIN_LEFT, Bindings.DOUBLE);
\r
391 if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_LEFT.tail().toString(), d);
\r
393 d = (Double) ra.getValue(P_PAGE_MARGIN_RIGHT, Bindings.DOUBLE);
\r
394 if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_RIGHT.tail().toString(), d);
\r
396 d = (Double) ra.getValue(P_PAGE_MARGIN_BOTTOM, Bindings.DOUBLE);
\r
397 if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_BOTTOM.tail().toString(), d);
\r
399 d = (Double) ra.getValue(P_PAGE_MARGIN_TOP, Bindings.DOUBLE);
\r
400 if ( d!=null ) contentScopeNode.putDouble(P_PAGE_MARGIN_TOP.tail().toString(), d);
\r
403 UnionAccessor ua = ra.getComponent(P_PAGE_PAPERSIZE);
\r
404 int tag = ua.getTag();
\r
405 contentScopeNode.putInt(P_PAGE_PAPERSIZE.tail().toString(), tag);
\r
406 } catch (ReferenceException re) {
\r
410 UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);
\r
411 int tag = ua.getTag();
\r
412 contentScopeNode.putInt(P_PAGE_ORIENTATION.tail().toString(), tag);
\r
413 } catch (ReferenceException re) {
\r
416 } catch (AccessorConstructionException e) {
\r
417 throw new ExportException(e);
\r
418 } catch (AccessorException e) {
\r
419 throw new ExportException(e);
\r
424 public void loadPref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
\r
426 RecordAccessor ra = Accessors.getAccessor(options);
\r
429 s = contentScopeNode.get(P_PDF_TITLE.tail().toString(), null);
\r
430 if ( s!=null ) ra.setValue(P_PDF_TITLE, Bindings.STRING, s);
\r
432 s = workbenchScopeNode.get(P_PDF_AUTHOR.tail().toString(), null);
\r
433 if ( s!=null ) ra.setValue(P_PDF_AUTHOR, Bindings.STRING, s);
\r
435 s = contentScopeNode.get(P_PDF_SUBJECT.tail().toString(), null);
\r
436 if ( s!=null ) ra.setValue(P_PDF_SUBJECT, Bindings.STRING, s);
\r
438 s = contentScopeNode.get(P_PDF_KEYWORDS.tail().toString(), null);
\r
439 if ( s!=null ) ra.setValue(P_PDF_KEYWORDS, Bindings.STRING, s);
\r
441 s = workbenchScopeNode.get(P_SIGN_KEYSTORE.tail().toString(), null);
\r
442 if ( s!=null ) ra.setValue(P_SIGN_KEYSTORE, Bindings.STRING, s);
\r
444 s = workbenchScopeNode.get(P_SIGN_KEYSTOREPASSWORD.tail().toString(), null);
\r
445 if ( s!=null ) ra.setValue(P_SIGN_KEYSTOREPASSWORD, Bindings.STRING, s);
\r
447 s = workbenchScopeNode.get(P_SIGN_PRIVATEKEYPASSWORD.tail().toString(), null);
\r
448 if ( s!=null ) ra.setValue(P_SIGN_PRIVATEKEYPASSWORD, Bindings.STRING, s);
\r
450 s = workbenchScopeNode.get(P_SIGN_LOCATION.tail().toString(), null);
\r
451 if ( s!=null ) ra.setValue(P_SIGN_LOCATION, Bindings.STRING, s);
\r
453 s = workbenchScopeNode.get(P_SIGN_REASON.tail().toString(), null);
\r
454 if ( s!=null ) ra.setValue(P_SIGN_REASON, Bindings.STRING, s);
\r
456 int tag = workbenchScopeNode.getInt(P_PDF_COMPRESSION.tail().toString(), -1);
\r
459 UnionAccessor ua = ra.getComponent(P_PDF_COMPRESSION);
\r
460 ua.setComponentValue(tag, Bindings.VOID, null);
\r
461 } catch (ReferenceException re) {
\r
466 s = contentScopeNode.get(P_PAGE_MARGIN_LEFT.tail().toString(), null);
\r
467 if ( s!=null ) ra.setValue(P_PAGE_MARGIN_LEFT, Bindings.DOUBLE, Double.valueOf(s));
\r
469 s = contentScopeNode.get(P_PAGE_MARGIN_RIGHT.tail().toString(), null);
\r
470 if ( s!=null ) ra.setValue(P_PAGE_MARGIN_RIGHT, Bindings.DOUBLE, Double.valueOf(s));
\r
472 s = contentScopeNode.get(P_PAGE_MARGIN_TOP.tail().toString(), null);
\r
473 if ( s!=null ) ra.setValue(P_PAGE_MARGIN_TOP, Bindings.DOUBLE, Double.valueOf(s));
\r
475 s = contentScopeNode.get(P_PAGE_MARGIN_BOTTOM.tail().toString(), null);
\r
476 if ( s!=null ) ra.setValue(P_PAGE_MARGIN_BOTTOM, Bindings.DOUBLE, Double.valueOf(s));
\r
478 tag = contentScopeNode.getInt(P_PAGE_PAPERSIZE.tail().toString(), -1);
\r
481 UnionAccessor ua = ra.getComponent(P_PAGE_PAPERSIZE);
\r
482 ua.setComponentValue(tag, Bindings.VOID, null);
\r
483 } catch (ReferenceException re) {
\r
486 tag = contentScopeNode.getInt(P_PAGE_ORIENTATION.tail().toString(), -1);
\r
489 UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);
\r
490 ua.setComponentValue(tag, Bindings.VOID, null);
\r
491 } catch (ReferenceException re) {
\r
495 } catch (AccessorConstructionException e) {
\r
496 throw new ExportException(e);
\r
497 } catch (AccessorException e) {
\r
498 throw new ExportException(e);
\r
503 public void fillDefaultPrefs( ExportContext ctx, Variant options) throws ExportException {
\r
505 RecordAccessor ra = Accessors.getAccessor(options);
\r
507 Preferences instPrefs = InstanceScope.INSTANCE.getNode( PAGE_PLUGIN_ID );
\r
508 Preferences defaultPrefs = DefaultScope.INSTANCE.getNode( PAGE_PLUGIN_ID );
\r
510 String value = ExporterUtils.getPrefString(instPrefs, defaultPrefs, P_DEFAULT_PAGE_SIZE);
\r
511 if ( value != null ) {
\r
513 StringTokenizer tok = new StringTokenizer( value );
\r
514 String w = tok.nextToken().trim();
\r
515 String h = tok.nextToken().trim();
\r
516 String orientation = tok.nextToken().trim();
\r
517 if ( orientation!=null ) {
\r
519 UnionAccessor ua = ra.getComponent(P_PAGE_ORIENTATION);
\r
520 int index = ua.type().getComponentIndex2(orientation);
\r
521 if ( index<0 ) index = 1;
\r
522 ua.setComponentValue(index, Bindings.VOID, new Object());
\r
523 } catch (ReferenceException re) {
\r
526 String centering = tok.nextToken().trim();
\r
527 String pagesize = tok.nextToken("\u0001").trim();
\r
528 ExporterUtils.setUnionValue(ra, P_PAGE_PAPERSIZE, pagesize);
\r
530 String margins = tok.nextToken().trim();
\r
531 if ( margins != null ) {
\r
532 String[] split = margins.split(";");
\r
533 if (split.length == 4) {
\r
534 for (int i=0; i<4; i++) {
\r
535 String[] tok2 = split[i].split(" ");
\r
536 String controlRelative = tok2[0];
\r
537 String controlAbsolute = tok2[1];
\r
538 String diagramAbsolute = tok2[2];
\r
540 ChildReference ref = null;
\r
542 case 0: ref = P_PAGE_MARGIN_TOP; break;
\r
543 case 1: ref = P_PAGE_MARGIN_BOTTOM; break;
\r
544 case 2: ref = P_PAGE_MARGIN_LEFT; break;
\r
545 case 3: ref = P_PAGE_MARGIN_RIGHT; break;
\r
547 double margin_mm = Double.valueOf(diagramAbsolute);
\r
548 ra.setValue(ref, Bindings.DOUBLE, margin_mm);
\r
552 } catch( Exception e ) {
\r
553 // This is not so important that pdf export should fail.
\r
554 e.printStackTrace();
\r
559 String username = System.getProperty("user.name");
\r
560 if ( username != null ) ra.setValue(P_PDF_AUTHOR, Bindings.STRING, username);
\r
562 ra.setValue(P_SIGN_REASON, Bindings.STRING, "Document Created");
\r
563 ExporterUtils.setUnionValue(ra, P_PDF_COMPRESSION, 9);
\r
565 } catch (AccessorConstructionException e) {
\r
566 throw new ExportException(e);
\r
567 } catch (AccessorException e) {
\r
568 throw new ExportException(e);
\r
572 public static String getCreator() {
\r
573 String creator = null;
\r
574 IProduct product = Platform.getProduct();
\r
575 if (product != null) {
\r
576 creator = product.getDescription();
\r
577 if (creator == null) {
\r
578 creator = product.getName();
\r
581 if (creator == null) {
\r
582 creator = "Simantics";
\r
587 public static boolean isPdf(String filename) {
\r
588 return PATTERN_PDF.matcher(filename).matches();
\r
591 public static boolean isPdf(File file) {
\r
592 return PATTERN_PDF.matcher(file.getName()).matches();
\r