英文:
How to use java library from kotlin program?
问题
import com.itextpdf.kernel.pdf.PdfDocument
import com.itextpdf.kernel.pdf.PdfWriter
import com.itextpdf.layout.Document
import com.itextpdf.layout.element.Paragraph
class PDFService {
fun generateSimplePdf(value: String) {
println("I am generating a PDF for $value :)")
val dest = "path_to_your_pdf_file.pdf" // Replace with actual file path
val writer = PdfWriter(dest)
val pdfDoc = PdfDocument(writer)
val doc = Document(pdfDoc)
doc.add(Paragraph("Hello World!"))
doc.close()
}
}
Regarding your question about using Java inside Kotlin, the code you provided should work correctly since Kotlin is fully interoperable with Java. The key is to ensure that you're using the correct syntax for Kotlin.
However, there are a few corrections needed in your code snippet:
- Replace
println("I am generating a PDF for $value :)")
withprintln("I am generating a PDF for $value :)")
. - You need to define the
dest
variable with the path where you want to save the generated PDF. - In the line
val writer: PdfWriter(dest)
, it should beval writer = PdfWriter(dest)
.
As for general guidance on using Java inside Kotlin, the official Kotlin documentation provides information on how Kotlin interoperates with Java: Mixing Kotlin and Java. Additionally, you can refer to Java libraries' documentation and guides for usage in Kotlin, as the basic principles remain the same due to their interoperability.
英文:
I am programming in Kotlin.
I need to use a library, which is written in java and comes from maven central.
I put the dependency in my pom.xml
and can import the library in my Kotlin code.
However, I am not able to understand how I can use that java library from Kotlin.
From the tutorials, in Java, the library should be used as (it is used to generate PDF):
//Initialize writer
PdfWriter writer = new PdfWriter(dest);
//Initialize document
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
//Add paragraph to the document
doc.add(new Paragraph("Hello World!"));
//Close document
doc.close();
But how should I do in Kotlin, I tried the following:
import com.itextpdf.kernel.pdf.PdfDocument
import com.itextpdf.kernel.pdf.PdfWriter
class PDFService {
fun generateSimplePdf(value: String) {
println("I am generating a PDF for $value :)")
val writer: PdfWriter(dest)
val document: PdfDocument(writer)
}
}
But the arguments of PdfWriter and PdfDocument give: "Unexpected token".
How should I do that ? In a more general way, is there a reference on how to use java inside kotlin ? (This documentation is not very helpfull).
答案1
得分: 0
来自评论中的Tenfour04:
> 这与在 Kotlin 中使用 Java 类无关。你的 Kotlin
> 语法是错误的。使用 val write = PdfWriter(dest)
。注意是 =
,不是 :
。
> 在你的 document 部分也是同样的情况。冒号用于指定类型。
> 等号用于赋值。类型通常可以省略,
> 因为它可以从你最初分配的内容中推断出来。
英文:
From Tenfour04 from the comments:
> This has nothing to do with using Java classes in Kotlin. Your Kotlin
> syntax is incorrect. Use val write = PdfWriter(dest)
. Note =
, not :
.
> Same with your document line. The colon is for specifying the type. An
> equal sign is for assigning a value. Type can usually be omitted
> because it can be inferred from what you initially assign.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论