搜索
您的当前位置:首页正文

用JODConverter和openoffice生成PDF文档时候的PAGESIZE设置问题

来源:抵帆知识网
用JODConverter和openoffice生成PDF文档时候的

PAGESIZE设置问题

生成PDF的方法有很多

其中一个方法就是JODConverter和openoffice 来生成。 一般的如何转换这里就不介绍了。可以看看其他文章。例如: 这里要说的是如果我们要转换的excel等的纸张大小不是默认的A4的情况下如何处理。

一般转换的时候会有部分代码是下面这样。 Java代码 1. // convert

2. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

3. converter.convert(inputFile, outputFile); 用上面的代码转换的时候,

无论输入文档的纸张定义成多大,都会被当成默认的A4来进行的转换,转换后的PDF也是A4的。

转换的具体过程,其实跟手动操作是一样的,openoffice打开要转换的文档,再点转换PDF按钮。

打开文档后,默认A4大小,我们调整纸张大小,转换后可以得到希望大小的PDF文件。

经过查看,其实转换PDF时候的参数设置里面并没有设置纸张大小的选择。所以只能从加载文档的地方想办法。

查看源代码OpenOfficeDocumentConverter的convert方法的源代码,可以看到其中调用到OpenOfficeDocumentConverter的下面的方法:

Java代码

1. private void loadAndExport(String inputUrl, Map/**/ loadProperties, String outputUrl, Map/**/ storeProperties)

上面的方法主要三个内容:

Java代码

1. document = loadDocument(inputUrl, loadProperties); 2. refreshDocument(document);

3. storeDocument(document, outputUrl, storeProperties);

loadDocument是加载office文档的,通过loadProperties传递的参数。支持的参数都定义在jar包里面的document-formats.xml里面了,没有 纸张设置的参数。

我们要做的只能增加几个参数。这个参数的具体内容需要查阅openoffice的文档。

因为loadDocument方法是private的,我们只能想别的办法,好在 refreshDocument不是private

我们可以新建一个class继承OpenOfficeDocumentConverter 并override refreshDocument方法。

Java代码

1. public final static Size A5, A4, A3; 2. public final static Size B4, B5, B6; 3. public final static Size KaoqinReport; 4. 5. static {

6. A5 = new Size(14800, 21000);

7. A4 = new Size(21000, 29700); 8. A3 = new Size(29700, 42000); 9.

10. B4 = new Size(25000, 35300); 11. B5 = new Size(17600, 25000); 12. B6 = new Size(12500, 17600); 13.

14. KaoqinReport = new Size(25400, 27940); 15. } 16. 17. /*

18. * XComponent:xCalcComponent 19. *

20. * @seecom.artofsolving.jodconverter.openoffice.converter.

21. * AbstractOpenOfficeDocumentConverter

22. * #refreshDocument(com.sun.star.lang.XComponent) 23. */

24. @Override

25. protected void refreshDocument(XComponent document) {

26. super.refreshDocument(document); 27.

28. // The default paper format and orientation is A4 and portrait. To

29. // change paper orientation 30. // re set page size

31. XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);

32. PropertyValue[] printerDesc = new PropertyValue[2];

33.

34. // Paper Orientation

35. // printerDesc[0] = new PropertyValue(); 36. // printerDesc[0].Name = \"PaperOrientation\"; 37. // printerDesc[0].Value = PaperOrientation.PORTRAIT;

38.

39. // Paper Format

40. printerDesc[0] = new PropertyValue(); 41. printerDesc[0].Name = \"PaperFormat\"; 42. printerDesc[0].Value = PaperFormat.USER; 43.

44. // Paper Size

45. printerDesc[1] = new PropertyValue(); 46. printerDesc[1].Name = \"PaperSize\"; 47. printerDesc[1].Value = KaoqinReport; 48. 49. try {

50. xPrintable.setPrinter(printerDesc); 51. } catch (IllegalArgumentException e) { 52. e.printStackTrace(); 53. } 54. 55. }

如果是excel有多个sheet,上面的部分只会影响第一个sheet,其他sheet还会以A4的大小输出。

上面的代码在JODConverter v2.x下面测试通过

因篇幅问题不能全部显示,请点此查看更多更全内容

Top