gpt4 book ai didi

java - 如何在开始时将标题设置为PDF以及如何设置居中对齐

转载 作者:行者123 更新时间:2023-12-02 00:58:09 25 4
gpt4 key购买 nike

在这张图片中,我想使用库itextpdf(版本7.0.2)设置我的pdf标题,但结果是:

enter image description here

代码:

    String file = "C:/Users/User/Desktop/generator/Test.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));
Document doc = new Document(pdfDoc);
float[] pointColumnWidths = {450, 200F};
Table table = new Table(pointColumnWidths);

Text text1 = new Text("BLABLA BLABLA");
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
text1.setFont(font);
text1.setFontSize(20);
text1.setBackgroundColor(Color.BLUE, 170, 20, 170, 20);
text1.setFontColor(Color.WHITE);
Paragraph paragraph1 = new Paragraph();
paragraph1.add(text1);
doc.add(paragraph1);


Cell c1 = new Cell();
c1.add("BlaBla:");
c1.setBorder(Border.NO_BORDER);
c1.setTextAlignment(TextAlignment.LEFT);
table.addCell(c1);


Cell c2 = new Cell();
String number = "45875454";
c2.setBorder(Border.NO_BORDER);
c2.add(number);
c2.setTextAlignment(TextAlignment.LEFT);
table.addCell(c2);

Cell c3 = new Cell();
c3.add("BlaBla:");
c3.setBorder(Border.NO_BORDER);
c3.setTextAlignment(TextAlignment.LEFT);
table.addCell(c3);


Cell c4 = new Cell();
String date = "06.04.2020";
c4.setBorder(Border.NO_BORDER);
c4.add(date);
c4.setTextAlignment(TextAlignment.LEFT);
table.addCell(c4);

Cell c5 = new Cell();
c5.add("BlaBla:");
c5.setBorder(Border.NO_BORDER);
c5.setTextAlignment(TextAlignment.LEFT);
table.addCell(c5);


Cell c6 = new Cell();
String index = "56";
c6.setBorder(Border.NO_BORDER);
c6.add(index);
c6.setTextAlignment(TextAlignment.LEFT);
table.addCell(c6);

Cell c7 = new Cell();
c7.add("BlaBla:");
c7.setBorder(Border.NO_BORDER);
c7.setTextAlignment(TextAlignment.LEFT);
table.addCell(c7);


Cell c8 = new Cell();
String blaBla = "06.04.2020";
c8.setBorder(Border.NO_BORDER);
c8.add(blaBla);
c8.setTextAlignment(TextAlignment.LEFT);
table.addCell(c8);

doc.add(table);

doc.close();

最佳答案

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class HeaderFooterPageEvent extends PdfPageEventHelper {

public void onStartPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Top Left"), 30, 800, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Top Right"), 550, 800, 0);
}

public void onEndPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Bla Bla"), 110, 30, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("page " + document.getPageNumber()), 550, 30, 0);
}

}

iText 提供了 PdfPageEventHelper 类,该类具有在编写 PDF 时调用的不同事件方法。要添加页眉和页脚,我们需要创建一个扩展 PdfPageEventHelper 的类并重写 onStartPageonEndPage 方法。使用 PdfWriter 注册此类。

创建事件类HeaderFooterPageEvent扩展PdfPageEventHelperPdfPageEventHelper 类对于 PDF 有不同的事件方法。它实现了PdfPageEvent接口(interface)。 PdfPageEventHelper的一些事件方法如下

onOpenDocument
onStartPage
onEndPage
onCloseDocument
onParagraph
onParagraphEnd

关于java - 如何在开始时将标题设置为PDF以及如何设置居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61057326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com