如何从 Kotlin 程序中使用 Java 库?

huangapple go评论77阅读模式
英文:

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:

  1. Replace println("I am generating a PDF for $value :)") with println("I am generating a PDF for $value :)").
  2. You need to define the dest variable with the path where you want to save the generated PDF.
  3. In the line val writer: PdfWriter(dest), it should be val 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.

huangapple
  • 本文由 发表于 2020年8月17日 21:41:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63452071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定