将 jRuby 代码转换为 Ruby 代码
我有这样的代码:
require 'java'
require 'iText-5.0.6.jar'
module Pdf
include_package "com.itextpdf.text.pdf"
include_package "java.io"
def self.write
reader = PdfReader.new('application.pdf');
stamper = PdfStamper.new(reader, FileOutputStream.new('completed.pdf'))
form = stamper.acro_fields
puts "Form has these fields: #{form.fields.key_set.to_a}"
form.set_field("some_zipcode_field", "94115")
stamper.close
end
end
Pdf.write
在jRuby
中,我想使用rjb
转换为Ruby
,
现在有人知道我该怎么做吗?
i have this code:
require 'java'
require 'iText-5.0.6.jar'
module Pdf
include_package "com.itextpdf.text.pdf"
include_package "java.io"
def self.write
reader = PdfReader.new('application.pdf');
stamper = PdfStamper.new(reader, FileOutputStream.new('completed.pdf'))
form = stamper.acro_fields
puts "Form has these fields: #{form.fields.key_set.to_a}"
form.set_field("some_zipcode_field", "94115")
stamper.close
end
end
Pdf.write
in jRuby
and i want to translate to Ruby
, using rjb
anyone nows how can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以通过“RJB”(Ruby-Java 桥)使用常规 Ruby 中的 iText 等库。我不知道 RJB 中实现这一点的正确调用顺序,但这是可能的,而且我知道有人这样做。
另一种方法是使用纯 Ruby“虾”库,它在 JRuby 和 Ruby 上的工作方式相同
It's possible to use libraries like iText from regular Ruby via "RJB", the Ruby-Java bridge. I do not know the proper sequence of calls in RJB to make that happen, but it is possible and I know of folks doing it.
An alternative would be to use the pure-Ruby "prawn" library, which would work the same on both JRuby and Ruby
如果您使用的是 iText 这样的 Java 库,则不能。您需要修改代码以使用不同的 PDF 库,才能使其在 C Ruby 中工作。
If you're using a Java library like iText then you can't. You'll need to modify the code to use a different PDF library in order for it to work in C Ruby.
显然,rjb 网站将帮助您了解更多具体信息 RJB。不幸的是,我认为它并没有涵盖其所有功能。
我不认为你可以在 RJB 中加载整个包,但由于你只使用少数几个类,你可以进行单独的导入:
JString = Rjb::import('java.lang.String')
我也认为 Rjb 不可以拥有 Java 方法的所有快捷别名,因此您最终可能需要 setField 而不是 set_field 或 field=。我不是 Rjb 用户,但我真的很想与 Rjb 合作,使我们的两种语法更加兼容。 [注意:当您设置 Rjb 时,您可能想挑战这个建议......我只是从未见过其中包含快捷方式的示例]
Obviously, the rjb site will help with more specifics RJB. Unfortunately, I don't think it covers all of its features.
I don't think you can load whole packages in RJB, but since you are only using a handful of classes you can do individual imports:
JString = Rjb::import('java.lang.String')
I also think Rjb does not have all the shortcut aliases for Java methods so you may end up needing to setField instead of set_field or field=. I am not an Rjb user, but I would really like to collaborate with Rjb to make our two syntaxes more compatible. [note: You might want to challenge this advice when you get Rjb set up...I just have never seen an example with the shortcuts in them]