您的位置: 首页 > 技术文档 > 网络编程 > 使用OpenOffice.org将各类文档转为PDF
Visual Studio DSL 入门(二) 回到列表 UML建模
 使用OpenOffice.org将各类文档转为PDF

作者:老赵 时间: 2010-06-17 文档类型:转载 来自:老赵点滴

第 1 页 使用OpenOffice.org将各类文档转为PDF [1]
第 2 页 使用OpenOffice.org将各类文档转为PDF [2]
第 3 页 使用OpenOffice.org将各类文档转为PDF [3]
第 4 页 使用OpenOffice.org将各类文档转为PDF [4]

完整代码

在这里贴出“txt转pdf”完整的可运行的示例代码:

import java.lang._;
import java.io.File;
import ooo.connector.BootstrapSocketConnector;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.util.XCloseable;
object AnyToPdf extends Application {
  // get the remote office component context
  def createContext() : XComponentContext = {
    val oooExeFolder = "C:/Program Files/OpenOffice.org 3/program/"
    BootstrapSocketConnector.bootstrap(oooExeFolder)
  }
  def createComponentLoader(context: XComponentContext) : XComponentLoader = {
    // get the remote office service manager
    val mcf = context.getServiceManager()
    val desktop = mcf.createInstanceWithContext("com.sun.star.frame.Desktop", context)
    UnoRuntime.queryInterface(classOf[XComponentLoader], desktop)
  }
  def loadDocument(loader: XComponentLoader, inputFilePath: String) : Object = {
    // Preparing properties for loading the document
    val propertyValue = new PropertyValue()
    propertyValue.Name = "Hidden"
    propertyValue.Value = new Boolean(true)
    // Composing the URL by replacing all backslashs
    val inputFile = new File(inputFilePath)
    val inputUrl = "file:///" + inputFile.getAbsolutePath().replace('\\', '/')
    loader.loadComponentFromURL(inputUrl, "_blank", 0, Array(propertyValue))
  }
  def convertDocument(doc: Object, outputFilePath: String, convertType: String) {
    // Preparing properties for converting the document
    // Setting the flag for overwriting
    val overwriteValue = new PropertyValue()
    overwriteValue.Name = "Overwrite"
    overwriteValue.Value = new Boolean(true)
    // Setting the filter name
    val filterValue = new PropertyValue()
    filterValue.Name = "FilterName"
    filterValue.Value = convertType
    // Composing the URL by replacing all backslashs
    val outputFile = new File(outputFilePath)
    val outputUrl = "file:///" + outputFile.getAbsolutePath().replace('\\', '/')
    // Getting an object that will offer a simple way to store
    // a document to a URL.
    val storable = UnoRuntime.queryInterface(classOf[XStorable], doc)
    // Storing and converting the document
    storable.storeToURL(outputUrl, Array(overwriteValue, filterValue))
  }
  def closeDocument(doc: Object) {
    // Closing the converted document. Use XCloseable.clsoe if the
    // interface is supported, otherwise use XComponent.dispose
    val closeable = UnoRuntime.queryInterface(classOf[XCloseable], doc)
    if (closeable != null) {
      closeable.close(false)
    } else {
      val component = UnoRuntime.queryInterface(classOf[XComponent], doc)
      component.dispose()
    }
  }
  val inputFilePath = "D:\\convert\\input.txt"
  val outputFilePath = "D:\\convert\\output.pdf"
  // Getting the given type to convert to
  val convertType = "writer_pdf_Export"
  val context = createContext()
  println("connected to a running office ...")
  val loader = createComponentLoader(context)
  println("loader created ...")
  val doc = loadDocument(loader, inputFilePath)
  println("document loaded ...")
  convertDocument(doc, outputFilePath, convertType)
  println("document converted ...")
  closeDocument(doc)
  println("document closed ...")
}

很显然,这里不是我所厌恶的Java语言。这是一段Scala代码,就从最基本的代码使用上看,Scala也已经比Java代码要节省许多了。

总结

其实解决这个问题还是走了不少弯路的,究其原因可能是从示例代码出发去寻找解决方案,而并没有去系统地阅读各种资料。在这个过程中,我找到了一些比较重要的文档:

API/Tutorials/PDF export:对于PDF导出功能各种参数的详细解释。

Text Documents:关于文本文档相关操作的详细说明。

DocumentHanding:“文档操作”示例代码的解释,包括文档打印等等。

当然,最详细文档莫过于完整的开发人员指南了,如果您想要详细了解这方面的内容,这应该也属于必读内容之一。

有了OpenOffice.org,就相当于我们拥有了一套完整的文档操作类库,可以用来实现各种功能。除了转PDF以外,例如我们还可以将一篇数百万字的小说加载为文档,再每十页导出一份图片,方便用户在线预览顺便防点拷贝。此外,虽然我是在Windows下操作OOo,但是OOo和Java本身都是跨平台的,因此同样的代码也可以运行在Linux平台上。我目前正在尝试在Ubuntu Server上部署一份OOo和代码,如果有什么特别的情况,我也会另行记录。

事实上有一点我之前一直没有提到:如果您使用Windows及.NET进行开发,OOo也提供了C++/CLI接口,可以使用C#、F#进行编程,且代码与本文描述的几乎如出一辙(只要把queryInterface方法调用改成直接转换),在.NET 4.0中也可正常使用。

如果您有其他解决方案,也请一起交流一下。

本文链接:http://www.blueidea.com/tech/program/2010/7702.asp 

出处:老赵点滴
责任编辑:bluehearts

上一页 使用OpenOffice.org将各类文档转为PDF [3] 下一页

◎进入论坛网络编程版块参加讨论

相关文章
使用 iPhone 离线浏览电子文档
PDF、ZIP、DOC链接的标注
PDF印刷输出之江湖告急
Acrobat 3D界面抢鲜看
Flash Paper 2
关键字搜索 常规搜索 推荐文档
热门搜索:CSS Fireworks 设计比赛 网页制作 web标准 用户体验 UE photoshop Dreamweaver Studio8 Flash 手绘 CG
站点最新 站点最新列表
周大福“敬•自然”设计大赛开启
国际体验设计大会7月将在京举行
中国国防科技信息中心标志征集
云计算如何让安全问题可控
云计算是多数企业唯一拥抱互联网的机会
阿里行云
云手机年终巨献,送礼标配299起
阿里巴巴CTO王坚的"云和互联网观"
1499元买真八核 云OS双蛋大促
首届COCO桌面手机主题设计大赛
栏目最新 栏目最新列表
浅谈JavaScript编程语言的编码规范
如何在illustrator中绘制台历
Ps简单绘制一个可爱的铅笔图标
数据同步算法研究
用ps作简单的作品展示页面
CSS定位机制之一:普通流
25个最佳最闪亮的Eclipse开发项目
Illustrator中制作针线缝制文字效果
Photoshop制作印刷凹凸字体
VS2010中创建自定义SQL Rule
>> 分页 首页 前页 后页 尾页 页次:4/41个记录/页 转到 页 共4个记录

蓝色理想版权申明:除部分特别声明不要转载,或者授权我站独家播发的文章外,大家可以自由转载我站点的原创文章,但原作者和来自我站的链接必须保留(非我站原创的,按照原来自一节,自行链接)。文章版权归我站和作者共有。

转载要求:转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印,亦不能抹去我站点水印。

特别注意:本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有,文章若有侵犯作者版权,请与我们联系,我们将立即删除修改。

您的评论
用户名:  口令:
说明:输入正确的用户名和密码才能参与评论。如果您不是本站会员,你可以注册 为本站会员。
注意:文章中的链接、内容等需要修改的错误,请用报告错误,以利文档及时修改。
不评分 1 2 3 4 5
注意:请不要在评论中含与内容无关的广告链接,违者封ID
请您注意:
·不良评论请用报告管理员,以利管理员及时删除。
·尊重网上道德,遵守中华人民共和国的各项有关法律法规
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·本站评论管理人员有权保留或删除其管辖评论中的任意内容
·您在本站发表的作品,本站有权在网站内转载或引用
·参与本评论即表明您已经阅读并接受上述条款
推荐文档 | 打印文档 | 评论文档 | 报告错误  
专业书推荐 更多内容
网站可用性测试及优化指南
《写给大家看的色彩书1》
《跟我去香港》
众妙之门—网站UI 设计之道
《Flex 4.0 RIA开发宝典》
《赢在设计》
犀利开发—jQuery内核详解与实践
作品集 更多内容

杂⑦杂⑧ Gold NORMANA V2