英文:
How set header to PDF at the beginning and how set align center it
问题
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();
英文:
In this picture I want set header of my pdf with library itextpdf (version 7.0.2), but as a result:
Code:
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();
答案1
得分: 2
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("顶部左侧"), 30, 800, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("顶部右侧"), 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("页码 " + document.getPageNumber()), 550, 30, 0);
}
}
iText提供了PdfPageEventHelper
类,该类具有不同的事件方法,在编写PDF时调用这些方法。要添加页眉和页脚,我们需要创建一个继承自PdfPageEventHelper
的类,并覆盖onStartPage
和onEndPage
方法。将此类与PdfWriter注册。
创建一个事件类HeaderFooterPageEvent
,继承自PdfPageEventHelper
。
PdfPageEventHelper类具有不同的PDF事件方法。它实现了PdfPageEvent
接口。PdfPageEventHelper
的一些事件方法如下:
- onOpenDocument
- onStartPage
- onEndPage
- onCloseDocument
- onParagraph
- onParagraphEnd
<details>
<summary>英文:</summary>
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 provides `PdfPageEventHelper` class that has different event methods that is called while writing PDF. To add header and footer we need to create a class extending `PdfPageEventHelper` and override `onStartPage` and `onEndPage` methods. Register this class with PdfWriter.
Create an Event Class `HeaderFooterPageEvent` Extending `PdfPageEventHelper`
PdfPageEventHelper class has different event methods for PDF. It implements `PdfPageEvent` interface. Some event methods of `PdfPageEventHelper` are as below
onOpenDocument
onStartPage
onEndPage
onCloseDocument
onParagraph
onParagraphEnd
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论