以下是在iText中将文本对齐在单行中的Java代码示例: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class TextAlignmentExample { public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("TextAlignmentExample.pdf")); document.open(); Paragraph paragraph = new Paragraph("This is a sample text aligned in a single line."); paragraph.setAlignment(Element.ALIGN_CENTER); document.add(paragraph); document.close(); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们创建了一个`Paragraph`对象,将文本添加到其中,并使用`setAlignment()`方法将文本居中对齐。最后,我们将`Paragraph`对象添加到`Document`中并关闭`Document`。