我在使用 lowagie pdf 和 Report Making 时遇到了麻烦。我不能在第一页上包含页眉页脚
I have a trouble with lowagie pdf and Report Making. I cant include headerfooter on the first page
This is an image of the problem 我正在 Java Spring 开发中使用 lowagie PDF 库。我需要做一份横向报告。我还添加了一个 header 页脚。报告有 5 页。我已经包含了一个 table。在第 2、3、4 和 5 页上,一切似乎都运行良好。但在第 1 页中,我无法实现。 header页脚和我添加的段落没有出现。我累极了。我不知道还能做什么。有人以前遇到过同样的情况吗?非常感谢您的帮助。
/*
* 要更改此许可证 header,请在项目属性中选择许可证 Headers。
* 要更改此模板文件,请选择工具 |模板
* 并在编辑器中打开模板。
*/
package com.gambbate.faqu.viewresolver;
import com.gambbate.faqu.model.Usuario;
import com.itextpdf.text.BaseColor;
import com.lowagie.text.Chunk;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javafx.scene.layout.Border;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;
/**
*
* @author Pisner-Lichtenstein
*/
public class PdfView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> map,
Document dcmnt,
PdfWriter writer,
HttpServletRequest hsr,
HttpServletResponse hsr1) throws Exception {
List<Usuario> listadoDeUsuarios = (List<Usuario>) map.get("listadoDeUsuarios");
PdfPTable tabla = new PdfPTable(8);
tabla.setWidthPercentage(100);
tabla.setWidths(new float[]{2.0f, 2.0f, 2.0f, 4.0f, 2.0f, 1.5f, 2.0f, 2.0f});
PdfPCell encabezadoCelda = new PdfPCell();
encabezadoCelda.setBackgroundColor(Color.WHITE);
encabezadoCelda.setHorizontalAlignment(Element.ALIGN_CENTER);
encabezadoCelda.setPadding(5);
encabezadoCelda.setBorderColor(Color.LIGHT_GRAY);
encabezadoCelda.setBorderWidth(0.15f);
Font fuente = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
fuente.setSize(10);
fuente.setColor(Color.BLACK);
encabezadoCelda.setFixedHeight(30);
encabezadoCelda.setPadding(5);
encabezadoCelda.setVerticalAlignment(Element.ALIGN_MIDDLE);
encabezadoCelda.setPhrase(new Phrase("Nombre", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Apellido", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Fecha de Nac.", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Email", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Celular", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Interno", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Gerencia", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Puesto", fuente));
tabla.addCell(encabezadoCelda);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
int colorDeFondo = 0;
for (Usuario listadoDeUsuario : listadoDeUsuarios) {
PdfPCell cd = new PdfPCell();
cd.setBorderColor(Color.LIGHT_GRAY);
cd.setBorderWidth(0.15f);
fuente = FontFactory.getFont(FontFactory.HELVETICA);
fuente.setColor(Color.DARK_GRAY);
fuente.setSize(10);
cd.setVerticalAlignment(Element.ALIGN_MIDDLE);
cd.setPadding(5);
cd.setFixedHeight(30);
if (colorDeFondo % 2 == 0) {
cd.setBackgroundColor(Color.WHITE);
} else {
cd.setBackgroundColor(new Color(245, 245, 245));
}
colorDeFondo++;
cd.setPhrase(new Phrase(listadoDeUsuario.getNombre(), fuente));
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getApellido(), fuente));
tabla.addCell(cd);
cd.setPhrase(new Phrase(sdf.format(listadoDeUsuario.getFechaDeNacimiento()), fuente));
cd.setHorizontalAlignment(Element.ALIGN_RIGHT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getEmailCorporativo(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_LEFT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getCelular(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_RIGHT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getInterno(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_RIGHT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getGerencia().getGerencia(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_LEFT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getPuesto().getPuesto(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_LEFT);
tabla.addCell(cd);
}
tabla.setSpacingBefore(25);
tabla.setHeaderRows(1);
Paragraph encabezado = new Paragraph(new Chunk("Gambbate - Faqu Soft", FontFactory.getFont(FontFactory.HELVETICA, 15)));
Paragraph reporteTitulo = new Paragraph(new Chunk("Recursos Humanos - Listado de Uuarios", FontFactory.getFont(FontFactory.HELVETICA, 15)));
Paragraph autor = new Paragraph(new Chunk("Emitido por:" + map.get("nombreDeUsuario"), FontFactory.getFont(FontFactory.HELVETICA, 15)));
Paragraph fecha = new Paragraph(new Chunk("Fecha de Emision:" + new Date(), FontFactory.getFont(FontFactory.HELVETICA, 15)));
HeaderFooter header = new HeaderFooter(new Phrase("fewfwfewwwe"), false);
HeaderFooter footer = new HeaderFooter(new Phrase("aaaaaaaaaaaa"), false);
dcmnt.setHeader(header);
dcmnt.setFooter(footer);
dcmnt.setPageSize(PageSize.A4.rotate());
dcmnt.setMargins(60, 30, 30, 30);
dcmnt.add(encabezado);
dcmnt.add(reporteTitulo);
dcmnt.add(tabla);
}
}
正如问题的评论中已经提到的,如果在创建页面时已经需要页面设置(例如它的尺寸和方向),并且如果您想在第一页上更改它,您需要在 打开 Document
之前设置 属性 因为第一页是在打开例程中创建的。
在手头的案例中,PageSize
和 Margins
很有趣。要在第一页更改它们,您可以这样做:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("landscape.pdf"));
document.setPageSize(PageSize.A4.rotate());
document.setMargins(60, 30, 30, 30);
document.open();
document.add(new Paragraph("Test string for a landscape PDF."));
document.close();
结果是:
如你所见
- 第一页已经是横向的并且
- 第一页使用预定义的边距,特别是左边距是默认情况下不是顶部边距的两倍。
因此,您所要做的就是搜索您的buildPdfDocument
方法的调用代码,在其中找到Document
和PdfWriter
实例化(类似于
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("landscape.pdf"));
),并设置 PageSize
和 Margins
(例如
document.setPageSize(PageSize.A4.rotate());
document.setMargins(60, 30, 30, 30);
) 在调用 Document
方法 open
之前(例如
document.open();
).
在你的情况下,代码在 AbstractPdfView
class 的方法 renderMergedOutputModel
中,你的 class 是从中派生的:
[...]
Document document = getDocument();
PdfWriter writer = newWriter(document, baos);
// Apply preferences and build metadata.
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
[...]
但是你不必为这个基础打补丁class;如您所见,某些方法在 document.open()
之前被调用,特别是 buildPdfMetadata
,记录为:
/**
* Populate the iText Document's meta fields (author, title, etc.).
* <br>Default is an empty implementation. Subclasses may override this method
* to add meta fields such as title, subject, author, creator, keywords, etc.
* This method is called after assigning a PdfWriter to the Document and
* before calling <code>document.open()</code>.
* @param model the model, in case meta information must be populated from it
* @param document the iText document being populated
* @param request in case we need locale etc. Shouldn't look at attributes.
* @see com.lowagie.text.Document#addTitle
* @see com.lowagie.text.Document#addSubject
* @see com.lowagie.text.Document#addKeywords
* @see com.lowagie.text.Document#addAuthor
* @see com.lowagie.text.Document#addCreator
* @see com.lowagie.text.Document#addProducer
* @see com.lowagie.text.Document#addCreationDate
* @see com.lowagie.text.Document#addHeader
*/
protected void buildPdfMetadata(Map model, Document document, HttpServletRequest request)
此方法比 prepareWriter
更适合您的用例,因为它具有 Document document
作为参数。
因此,您所要做的就是在 class:
中覆盖该方法
protected void buildPdfMetadata(Map model, Document document, HttpServletRequest request) {
super.buildPdfMetadata(model, document, request);
document.setPageSize(PageSize.A4.rotate());
document.setMargins(60, 30, 30, 30);
}
This is an image of the problem 我正在 Java Spring 开发中使用 lowagie PDF 库。我需要做一份横向报告。我还添加了一个 header 页脚。报告有 5 页。我已经包含了一个 table。在第 2、3、4 和 5 页上,一切似乎都运行良好。但在第 1 页中,我无法实现。 header页脚和我添加的段落没有出现。我累极了。我不知道还能做什么。有人以前遇到过同样的情况吗?非常感谢您的帮助。
/* * 要更改此许可证 header,请在项目属性中选择许可证 Headers。 * 要更改此模板文件,请选择工具 |模板 * 并在编辑器中打开模板。 */
package com.gambbate.faqu.viewresolver;
import com.gambbate.faqu.model.Usuario;
import com.itextpdf.text.BaseColor;
import com.lowagie.text.Chunk;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javafx.scene.layout.Border;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;
/**
*
* @author Pisner-Lichtenstein
*/
public class PdfView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> map,
Document dcmnt,
PdfWriter writer,
HttpServletRequest hsr,
HttpServletResponse hsr1) throws Exception {
List<Usuario> listadoDeUsuarios = (List<Usuario>) map.get("listadoDeUsuarios");
PdfPTable tabla = new PdfPTable(8);
tabla.setWidthPercentage(100);
tabla.setWidths(new float[]{2.0f, 2.0f, 2.0f, 4.0f, 2.0f, 1.5f, 2.0f, 2.0f});
PdfPCell encabezadoCelda = new PdfPCell();
encabezadoCelda.setBackgroundColor(Color.WHITE);
encabezadoCelda.setHorizontalAlignment(Element.ALIGN_CENTER);
encabezadoCelda.setPadding(5);
encabezadoCelda.setBorderColor(Color.LIGHT_GRAY);
encabezadoCelda.setBorderWidth(0.15f);
Font fuente = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
fuente.setSize(10);
fuente.setColor(Color.BLACK);
encabezadoCelda.setFixedHeight(30);
encabezadoCelda.setPadding(5);
encabezadoCelda.setVerticalAlignment(Element.ALIGN_MIDDLE);
encabezadoCelda.setPhrase(new Phrase("Nombre", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Apellido", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Fecha de Nac.", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Email", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Celular", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Interno", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Gerencia", fuente));
tabla.addCell(encabezadoCelda);
encabezadoCelda.setPhrase(new Phrase("Puesto", fuente));
tabla.addCell(encabezadoCelda);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
int colorDeFondo = 0;
for (Usuario listadoDeUsuario : listadoDeUsuarios) {
PdfPCell cd = new PdfPCell();
cd.setBorderColor(Color.LIGHT_GRAY);
cd.setBorderWidth(0.15f);
fuente = FontFactory.getFont(FontFactory.HELVETICA);
fuente.setColor(Color.DARK_GRAY);
fuente.setSize(10);
cd.setVerticalAlignment(Element.ALIGN_MIDDLE);
cd.setPadding(5);
cd.setFixedHeight(30);
if (colorDeFondo % 2 == 0) {
cd.setBackgroundColor(Color.WHITE);
} else {
cd.setBackgroundColor(new Color(245, 245, 245));
}
colorDeFondo++;
cd.setPhrase(new Phrase(listadoDeUsuario.getNombre(), fuente));
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getApellido(), fuente));
tabla.addCell(cd);
cd.setPhrase(new Phrase(sdf.format(listadoDeUsuario.getFechaDeNacimiento()), fuente));
cd.setHorizontalAlignment(Element.ALIGN_RIGHT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getEmailCorporativo(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_LEFT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getCelular(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_RIGHT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getInterno(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_RIGHT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getGerencia().getGerencia(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_LEFT);
tabla.addCell(cd);
cd.setPhrase(new Phrase(listadoDeUsuario.getPuesto().getPuesto(), fuente));
cd.setHorizontalAlignment(Element.ALIGN_LEFT);
tabla.addCell(cd);
}
tabla.setSpacingBefore(25);
tabla.setHeaderRows(1);
Paragraph encabezado = new Paragraph(new Chunk("Gambbate - Faqu Soft", FontFactory.getFont(FontFactory.HELVETICA, 15)));
Paragraph reporteTitulo = new Paragraph(new Chunk("Recursos Humanos - Listado de Uuarios", FontFactory.getFont(FontFactory.HELVETICA, 15)));
Paragraph autor = new Paragraph(new Chunk("Emitido por:" + map.get("nombreDeUsuario"), FontFactory.getFont(FontFactory.HELVETICA, 15)));
Paragraph fecha = new Paragraph(new Chunk("Fecha de Emision:" + new Date(), FontFactory.getFont(FontFactory.HELVETICA, 15)));
HeaderFooter header = new HeaderFooter(new Phrase("fewfwfewwwe"), false);
HeaderFooter footer = new HeaderFooter(new Phrase("aaaaaaaaaaaa"), false);
dcmnt.setHeader(header);
dcmnt.setFooter(footer);
dcmnt.setPageSize(PageSize.A4.rotate());
dcmnt.setMargins(60, 30, 30, 30);
dcmnt.add(encabezado);
dcmnt.add(reporteTitulo);
dcmnt.add(tabla);
}
}
正如问题的评论中已经提到的,如果在创建页面时已经需要页面设置(例如它的尺寸和方向),并且如果您想在第一页上更改它,您需要在 打开 Document
之前设置 属性 因为第一页是在打开例程中创建的。
在手头的案例中,PageSize
和 Margins
很有趣。要在第一页更改它们,您可以这样做:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("landscape.pdf"));
document.setPageSize(PageSize.A4.rotate());
document.setMargins(60, 30, 30, 30);
document.open();
document.add(new Paragraph("Test string for a landscape PDF."));
document.close();
结果是:
如你所见
- 第一页已经是横向的并且
- 第一页使用预定义的边距,特别是左边距是默认情况下不是顶部边距的两倍。
因此,您所要做的就是搜索您的buildPdfDocument
方法的调用代码,在其中找到Document
和PdfWriter
实例化(类似于
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("landscape.pdf"));
),并设置 PageSize
和 Margins
(例如
document.setPageSize(PageSize.A4.rotate());
document.setMargins(60, 30, 30, 30);
) 在调用 Document
方法 open
之前(例如
document.open();
).
在你的情况下,代码在 AbstractPdfView
class 的方法 renderMergedOutputModel
中,你的 class 是从中派生的:
[...]
Document document = getDocument();
PdfWriter writer = newWriter(document, baos);
// Apply preferences and build metadata.
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
[...]
但是你不必为这个基础打补丁class;如您所见,某些方法在 document.open()
之前被调用,特别是 buildPdfMetadata
,记录为:
/**
* Populate the iText Document's meta fields (author, title, etc.).
* <br>Default is an empty implementation. Subclasses may override this method
* to add meta fields such as title, subject, author, creator, keywords, etc.
* This method is called after assigning a PdfWriter to the Document and
* before calling <code>document.open()</code>.
* @param model the model, in case meta information must be populated from it
* @param document the iText document being populated
* @param request in case we need locale etc. Shouldn't look at attributes.
* @see com.lowagie.text.Document#addTitle
* @see com.lowagie.text.Document#addSubject
* @see com.lowagie.text.Document#addKeywords
* @see com.lowagie.text.Document#addAuthor
* @see com.lowagie.text.Document#addCreator
* @see com.lowagie.text.Document#addProducer
* @see com.lowagie.text.Document#addCreationDate
* @see com.lowagie.text.Document#addHeader
*/
protected void buildPdfMetadata(Map model, Document document, HttpServletRequest request)
此方法比 prepareWriter
更适合您的用例,因为它具有 Document document
作为参数。
因此,您所要做的就是在 class:
中覆盖该方法protected void buildPdfMetadata(Map model, Document document, HttpServletRequest request) {
super.buildPdfMetadata(model, document, request);
document.setPageSize(PageSize.A4.rotate());
document.setMargins(60, 30, 30, 30);
}