如何在开始时设置PDF的页眉,并将其内容居中对齐。

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

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:

如何在开始时设置PDF的页眉,并将其内容居中对齐。

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的类,并覆盖onStartPageonEndPage方法。将此类与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(&quot;Top Left&quot;), 30, 800, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(&quot;Top Right&quot;), 550, 800, 0);
}
public void onEndPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(&quot;Bla Bla&quot;), 110, 30, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(&quot;page &quot; + 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>

huangapple
  • 本文由 发表于 2020年4月6日 18:07:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057326.html
匿名

发表评论

匿名网友

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

确定