I have created a PDF file dynamically using iText Library, Now I want to add Header and Footer in PDF's pages, for this one I have added given code:
document.addHeader("My Header Title", "My Header Details");
But in my PDF's pages this header couldn't set. What is issue that I don't know, If you have any idea related to it,please share your thoughts.
In case you use a current iText version (i.e. 5.4.x as of now) have a look at the sample MovieHistory2 from iText in Action — 2nd Edition which shows how to add headers (different ones for odd and even pages) to a PDF while creating it.
Most essential is a PdfPageEventHelper implementation
/** Inner class to add a header and a footer. */
class HeaderFooter extends PdfPageEventHelper {
/** Alternating phrase for the header. */
Phrase[] header = new Phrase[2];
/** Current page number (will be reset for every chapter). */
int pagenumber;
/**
* Initialize one of the headers.
* #see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onOpenDocument(PdfWriter writer, Document document) {
header[0] = new Phrase("Movie history");
}
/**
* Initialize one of the headers, based on the chapter title;
* reset the page number.
* #see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float,
* com.itextpdf.text.Paragraph)
*/
public void onChapter(PdfWriter writer, Document document,
float paragraphPosition, Paragraph title) {
header[1] = new Phrase(title.getContent());
pagenumber = 1;
}
/**
* Increase the page number.
* #see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onStartPage(PdfWriter writer, Document document) {
pagenumber++;
}
/**
* Adds the header and the footer.
* #see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
switch(writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, header[0],
rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, header[1],
rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
}
which is registered like this:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
HeaderFooter event = new HeaderFooter();
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
EDIT: As requested in the comments, a simpler variant of the onEndPage method with a static header instead of the alternating one:
public void onEndPage(PdfWriter writer, Document document)
{
Rectangle rect = writer.getBoxSize("art");
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, new Phrase("My static header text"),
rect.getRight(), rect.getTop(), 0);
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
please refer this site.......
https://www.coderanch.com/how-to/java/ItextExample
Please first refer to the accepted answer of this question.
That answer is very helpful (and It helped me to).
Just in case you are programming in C#, here is the SAME accepted answer but in C# version
/// <summary>
/// Inner class to add a header and a footer.
/// </summary>
internal class HeaderFooter : PdfPageEventHelper
{
private Phrase[] header = new Phrase[2];
private int pageNumber;
public override void OnOpenDocument(PdfWriter writer, Document document)
{
header[0] = new Phrase("Smares in Header");
}
public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
{
header[1] = new Phrase(title.Content);
pageNumber = 1;
}
public override void OnStartPage(PdfWriter writer, Document document)
{
pageNumber++;
}
public override void OnEndPage(PdfWriter writer, Document document)
{
Rectangle rect = writer.GetBoxSize("art");
switch (writer.PageNumber % 2)
{
case 0:
ColumnText.ShowTextAligned(writer.DirectContent,
Element.ALIGN_RIGHT, header[0],
rect.Right, rect.Top, 0);
break;
case 1:
ColumnText.ShowTextAligned(writer.DirectContent,
Element.ALIGN_LEFT, header[1],
rect.Left, rect.Top, 0);
break;
}
ColumnText.ShowTextAligned(writer.DirectContent,
Element.ALIGN_CENTER, new Phrase(String.Format("page {0}", pageNumber)),
(rect.Left + rect.Right) / 2, rect.Bottom - 18, 0);
}
}
and the registration of the event will be :
using (MemoryStream ms = new MemoryStream())
{
using (Document doc = new Document(PageSize.A4, -30, -30, 45, 45))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc, ms))
{
HeaderFooter ev = new HeaderFooter();
writer.SetBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.PageEvent = ev;
// continue your code here
}
}
}
NOTE : this is just a conversion of the accepted answer from java to C#.
but you can customize this according to your needs, as I did with it.
You can add like that
HeaderFooter header = new HeaderFooter(new Phrase("Add Header Part Here"), false);
HeaderFooter footer = new HeaderFooter(new Phrase("Add Footer Here"), new Phrase("."));
document.setHeader(header);
document.setFooter(footer);
Related
I'm trying to generate a PDF using iText7 with header and footer. However the codes does not work as expected. Here is my code.
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.io.IOException;
import java.util.Properties;
class TextHeaderEventHandler implements IEventHandler {
#Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfCanvas pdfCanvas = new PdfCanvas(docEvent.getPage());
Rectangle rectangle = new Rectangle(35, 740, 520, 100);
pdfCanvas.rectangle(rectangle);
Canvas canvas = new Canvas(pdfCanvas, rectangle).setFontSize(7);
// load logo image here and add
// canvas.add(image);
canvas.add(new Paragraph("My custom header line goes here."));
// bottom line
canvas.add(new Paragraph("---------------------------------------------------------------------"));
}
}
class TextFooterEventHandler implements IEventHandler {
#Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfCanvas pdfCanvas = new PdfCanvas(docEvent.getPage());
Rectangle rectangle = new Rectangle(35, 30, 520, 50);
pdfCanvas.rectangle(rectangle);
Canvas canvas = new Canvas(pdfCanvas, rectangle).setFontSize(7);
// bottom line
canvas.add(new Paragraph("---------------------------------------------------------------------"));
// footer text
canvas.add(new Paragraph("My custom footer line goes here."));
}
}
/**
* https://turkogluc.com/java-creating-pdf-reports-with-itext/
*/
public class HeaderFooter {
private static final Logger logger = LogManager.getLogger(HeaderFooter.class);
public static void main(String[] args) throws IOException {
Properties log4jProperties = new Properties();
log4jProperties.put("log4j.appender.ConsoleAppender", "org.apache.log4j.ConsoleAppender");
log4jProperties.put("log4j.appender.ConsoleAppender.layout", "org.apache.log4j.PatternLayout");
log4jProperties.put("log4j.appender.ConsoleAppender.layout.ConversionPattern", "%d [%t] %-5p %c - %m%n");
log4jProperties.put("log4j.rootLogger", "DEBUG, ConsoleAppender");
PropertyConfigurator.configure(log4jProperties);
HeaderFooter main = new HeaderFooter();
main.generatePdf();
}
public void generatePdf() throws IOException {
// Creating a PdfWriter
String dest = "/tmp/example.pdf";
PdfWriter writer = new PdfWriter(dest);
// Creating a PdfDocument
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document
Document document = new Document(pdfDoc);
document.setFontSize(10);
pdfDoc.addNewPage(PageSize.A4);
document.setMargins(80, 36, 80, 36);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new TextHeaderEventHandler());
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler());
// Adding a new page
pdfDoc.addNewPage(PageSize.A4);
// document.setMargins(80, 36, 60, 36);
String content = "Lorem ipsum dolor sit amet...jjjj";
Paragraph paragraph = new Paragraph(content);
paragraph.setFontSize(14);
paragraph.setTextAlignment(TextAlignment.CENTER);
paragraph.setBorder(Border.NO_BORDER);
paragraph.setFirstLineIndent(20);
paragraph.setItalic();
paragraph.setBold();
paragraph.setBackgroundColor(new DeviceRgb(245, 245, 245));
paragraph.setMargin(10);
paragraph.setPaddingLeft(10);
paragraph.setPaddingRight(10);
paragraph.setWidth(1000);
paragraph.setHeight(100);
document.add(paragraph);
int listIndex = 1;
List list = new List();
for (int i = 0; i < 10; i++, listIndex++) {
list.add("Java --> " + listIndex);
list.add("Go");
list.add("React");
list.add("Apache Kafka");
list.add("Jenkins");
list.add("Elastic Search");
}
document.add(list);
// adding a table adds a rectangle into the header part
Table table = new Table(new float[]{150F, 150F, 150F, 150F});
table.addCell(new Cell().add(new Paragraph("Id")));
table.addCell(new Cell().add(new Paragraph("Name")));
table.addCell(new Cell().add(new Paragraph("Location")));
table.addCell(new Cell().add(new Paragraph("Date")));
table.addCell(new Cell().add(new Paragraph("1000")));
table.addCell(new Cell().add(new Paragraph("Item-1")));
table.addCell(new Cell().add(new Paragraph("Istanbul")));
table.addCell(new Cell().add(new Paragraph("01/12/2020")));
table.addCell(new Cell().add(new Paragraph("1005")));
table.addCell(new Cell().add(new Paragraph("Item-2")));
table.addCell(new Cell().add(new Paragraph("Warsaw")));
table.addCell(new Cell().add(new Paragraph("05/12/2020")));
document.add(table);
// extra list
list = new List();
for (int i = 0; i < 10; i++, listIndex++) {
list.add("Java --> " + listIndex);
list.add("Go");
list.add("React");
list.add("Apache Kafka");
list.add("Jenkins");
list.add("Elastic Search");
}
document.add(list);
// Closing the document
document.close();
}
}
I see following issues.
Header is not generated for first page but footer is generated.
When I have a table, a rectangle is added to the header randomly on any
page.
When I remove table the rectangle goes away. But this way will be able to add table in
the pdf but I definitely want to add.
Am I missing something? Can anybody help here.
According to a comment you essentially have two questions:
Thanks for the info #BagusTesa, I've 2 questions. 1. Why is the header not getting added to Page 1 2. why is a rectangles added to header when I use tables in the pdf.
1. Why is the header not getting added to Page 1
You first add the new page and only thereafter register the event listener:
pdfDoc.addNewPage(PageSize.A4);
document.setMargins(80, 36, 80, 36);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new TextHeaderEventHandler());
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler());
Thus, when the START_PAGE event for that first page is triggered, there is no listener yet to draw the header.
If you want that TextHeaderEventHandler to also work for the first page, register it before creating any pages.
Actually you are adding content only via the Document instance which creates the required pages automatically. Why would you call addNewPage at all? Best try without that.
2. why is a rectangles added to header when I use tables in the pdf.
Well, first of all there is a rectangle because you create a rectangle path:
Rectangle rectangle = new Rectangle(35, 740, 520, 100);
pdfCanvas.rectangle(rectangle);
It usually doesn't show because you don't do anything with that rectangle path you created. You don't fill the path or stroke the path, you merely define it and let it dangle in the air. If later content you add causes the then current path to be stroked, this might make PDF viewers eventually also stroke the dangling path you defined. Probably adding the table includes such a path stroking instruction.
Applying these findings to your code
Originally your code produces this:
One clearly sees the missing header on page 1 and the rectangle lines around the header on page 2.
Now we remove the two
pdfDoc.addNewPage(PageSize.A4);
lines. The result:
One sees that on page 1 something happened in the header area: Where on page 2 there is a stroked rectangle, we here now have a filled rectangle.
Thus, let's deal with the rectangles now and remove the two
pdfCanvas.rectangle(rectangle);
lines from the event listener. The result:
We see the expected headers on all pages and no weird stroked or filled rectangles.
How can I draw specific underline between :
String s = "This text is underlined with a dashed line";
Paragraph paragraph = new Paragraph();
Text text;
for (int i = 0; i <s.length() ; i++) {
text = new Text(String.valueOf(s.charAt(i)));
paragraph.add(text);
text.setNextRenderer(new DashedLineTextRenderer(text));
}
doc.add(paragraph);
doc.close();
private static class DashedLineTextRenderer extends TextRenderer {
public DashedLineTextRenderer(Text textElement) {
super(textElement);
}
// If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
// If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
// renderer will be created
#Override
public IRenderer getNextRenderer() {
return new DashedLineTextRenderer((Text) modelElement);
}
#Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
Rectangle rect = this.getOccupiedAreaBBox();
PdfCanvas canvas = drawContext.getCanvas();
canvas.moveTo(rect.getLeft(), rect.getBottom());
canvas.curveTo(rect.getLeft()+100,rect.getBottom()+5,
rect.getLeft()+150,rect.getBottom()-2,rect.getLeft()+200,rect.getBottom()-5);
canvas.stroke();
}
}
if i do with single element text it works:
enter image description here
How i can define where draw canvas if there are several text elements
The question is underspecified (see clarifying comment). Basically to avoid the overlap as on the screenshot:
You can customize the renderer for the Paragraph, not for the Text:
private static class WaveUnderlinedParagraphRenderer extends ParagraphRenderer {
public WaveUnderlinedParagraphRenderer(Paragraph paragraph) {
super(paragraph);
}
#Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
Rectangle rect = this.getOccupiedAreaBBox();
PdfCanvas canvas = drawContext.getCanvas();
canvas.moveTo(rect.getLeft(), rect.getBottom());
canvas.curveTo(rect.getLeft() + 100, rect.getBottom() + 5,
rect.getLeft() + 150, rect.getBottom() - 2, rect.getLeft() + 200, rect.getBottom() - 5);
canvas.stroke();
}
#Override
public IRenderer getNextRenderer() {
return new WaveUnderlinedParagraphRenderer((Paragraph) modelElement);
}
}
Document doc = new Document(pdfDocument);
String s = "This text is underlined with a dashed line";
Paragraph paragraph = new Paragraph();
Text text;
for (int i = 0; i <s.length() ; i++) {
text = new Text(String.valueOf(s.charAt(i)));
paragraph.add(text);
}
paragraph.setNextRenderer(new WaveUnderlinedParagraphRenderer(paragraph));
doc.add(paragraph);
doc.close();
And get the following result:
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I was trying to create a PDF using iText.
My objective is to make a question paper. I was able to only add the questions and answers to the PDF.
Then i tried modifying it by adding page numbers, watermarks etc. by adding Header/Footer. Now it gives me NullPointerException i cannot figure out what went wrong.
Error I'm getting
Caused by: java.lang.NullPointerException
at
edu.ijse.gdse41.ams.other.HeaderFooter.onEndPage(HeaderFooter.java:57)
at com.itextpdf.text.pdf.PdfDocument.newPage(PdfDocument.java:902)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:837) at
com.itextpdf.text.Document.close(Document.java:416) at
edu.ijse.gdse41.ams.view.CreateAssignmentController.createPDF(CreateAssignmentController.java:644)
at
edu.ijse.gdse41.ams.view.CreateAssignmentController.proceedBtnClicked(CreateAssignmentController.java:292)
... 58 more
createPDF() method in CreateAssignment.java class
private void createPDF(ArrayList<Assignment_QuesDTO> questionPaper) throws DocumentException, BadElementException, IOException {
try {
OutputStream outputStream = null;
Document doc = new Document();
outputStream = new FileOutputStream(new File("C:\\Users\\Dell\\Documents\\NetBeansProjects\\AssignmentManagementSystem\\src\\PDF\\mypdf.pdf"));
PdfWriter writer = PdfWriter.getInstance(doc, outputStream);
PdfPageEventHelper eventHelper = new HeaderFooter(doc);
writer.setPageEvent(eventHelper);
doc.open();
Font fontTitle = new Font(Font.getFamily("TIMES_ROMAN"), 15);
Paragraph title = new Paragraph("ABC", fontTitle);
title.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(title);
for (int i = 0; i < 3; i++) {
doc.add(Chunk.NEWLINE);
}
Paragraph subTitle = new Paragraph(questionPaper.get(1).getAssignment().getAssignName());
subTitle.setAlignment(Paragraph.ALIGN_CENTER);
Paragraph subTitle2 = new Paragraph(questionPaper.get(1).getAssignment().getDate());
subTitle2.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(subTitle);
doc.add(subTitle2);
List orderedList = new List(List.ORDERED);
for (Assignment_QuesDTO questionPaper1 : questionPaper) {
Paragraph question = new Paragraph(questionPaper1.getQuestion().getQues());
question.setAlignment(Paragraph.ALIGN_JUSTIFIED);
orderedList.add(question);
List desc = new List(List.UNORDERED);
desc.setIndentationLeft(36);
desc.setListSymbol(new Chunk(" "));
desc.add(new Phrase("\t\t" + questionPaper1.getQuestion().getQuesDesc()));
orderedList.add(desc);
orderedList.add(Chunk.NEWLINE);
List answers = new List(List.ORDERED,List.ALPHABETICAL);
answers.setIndentationLeft(72);
for (AnswerDTO answer : questionPaper1.getQuestion().getAnswers()) {
answers.add(" " + answer.getAnswer());
}
orderedList.add(answers);
orderedList.add(Chunk.NEWLINE);
}
doc.add(orderedList);
doc.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(CreateAssignmentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
HeaderFooter.java class
public class HeaderFooter extends PdfPageEventHelper {
Phrase[] header = new Phrase[2];
int pageNum;
Image watermark;
public HeaderFooter(Document doc) throws BadElementException, IOException {
this.watermark = Image.getInstance("C:\\Users\\Dell\\Documents\\NetBeansProjects\\AssignmentManagementSystem\\src\\edu\\ijse\\gdse41\\ams\\resources\\images\\watermark.png");
watermark.rotate();
watermark.scaleToFit(doc.getPageSize());
watermark.setRotationDegrees(30);
}
#Override
public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
header[1] = new Phrase(title.getContent());
pageNum = 1;
}
#Override
public void onEndPage(PdfWriter writer, Document document) {
try {
Rectangle rect = writer.getBoxSize("art");
switch (writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header[0], rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header[1], rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("Page %d", pageNum)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
PdfContentByte content = writer.getDirectContent();
content.addImage(watermark);
} catch (DocumentException ex) {
Logger.getLogger(HeaderFooter.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void onStartPage(PdfWriter writer, Document document) {
pageNum++;
}
#Override
public void onOpenDocument(PdfWriter writer, Document document) {
header[0] = new Phrase("ABC");
}
}
In your page event, you assume that the PDF you are creating has an /ArtBox boundary:
Rectangle rect = writer.getBoxSize("art");
However, when I look at the code that creates your PDF, I don't see you creating such a page boundary anywhere. This means that rect is null, and that methods such as rect.getRight(), rect.getTop(),... throw a NullPointerException.
I have written an android app to do inspections. It collects the data and then formats it and places it on a PDF document. I am having a problem creating a 2nd page of the PDF before I save it and email it. I have commented out "PAGE 2 OF PDF". This section of code up to the declaration of pdfName is where the problem is. I do not want to use anything like iText or Apose. Can anyone help???
public void createPDF(){
// Create a object of PdfDocument
PdfDocument document = new PdfDocument();
// content view is TableLayout of data
View content = findViewById(id.final_table_layout_for_pdf_page_1);
// create a page info with attributes as below
// page number, height and width
// i have used height and width to that of pdf content view
int pageNumber = 1;
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(content.getWidth(),
content.getHeight() - 20, pageNumber).create();
// create a new page from the PageInfo
PdfDocument.Page page = document.startPage(pageInfo);
// repaint the user's text into the page
content.draw(page.getCanvas());
// do final processing of the page
document.finishPage(page);
/* PAGE 2 OF PDF
content = findViewById(id.final_table_layout_for_pdf_page_2);
// create a page info with attributes as below
// for 2nd page
// i have used height and width to that of pdf content view
pageNumber = 2;
pageInfo = new PdfDocument.PageInfo.Builder(content.getWidth(),
content.getHeight() - 20, pageNumber).create();
// create a new page from the PageInfo
page = document.startPage(pageInfo);
// repaint the user's text into the page
content.draw(page.getCanvas());
// do final processing of the page
document.finishPage(page);*/
// saving pdf document to root dir
String pdfName = "pdf_inspection_demo.pdf";
// all created files will be saved at path /sdcard/PDFDemo_AndroidSRC/
File outputFile = new File("/storage/emulated/0/", pdfName);
try {
outputFile.createNewFile();
OutputStream out = new FileOutputStream(outputFile);
document.writeTo(out);
document.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
errorString = e.getMessage();
}
}
private void generatePDF() {
PdfDocument pdfDocument = new PdfDocument();
criNewPag(1,"ان شاء الله ",pdfDocument);
criNewPag(2,"ان شاء الله ربي ",pdfDocument);
File file = new File(Environment.getExternalStorageDirectory(), "GFG.pdf");
try {
pdfDocument.writeTo(new FileOutputStream(file));
Toast.makeText(getApplicationContext(), "PDF file generated successfully.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();
}
private void criNewPag(int numpag,String text, PdfDocument pdfDocument ){
Paint paint = new Paint();
Paint title = new Paint();
PdfDocument.PageInfo mypageInfo = new PdfDocument.PageInfo.Builder(pagewidth, pageHeight, numpag).create();
PdfDocument.Page myPage = pdfDocument.startPage(mypageInfo);
Canvas canvas = myPage.getCanvas();
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gfgimage);
scaledbmp = Bitmap.createScaledBitmap(bmp, 140, 140, false);
canvas.drawBitmap(scaledbmp, 56, 40, paint);
title.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
title.setTextSize(15);
title.setColor(ContextCompat.getColor(this, R.color.purple_200));
canvas.drawText("A portal for IT professionals.", 209, 100, title);
canvas.drawText("Geeks for Geeks", 209, 80, title);
title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
title.setColor(ContextCompat.getColor(this, R.color.purple_200));
title.setTextSize(15);
title.setTextAlign(Paint.Align.CENTER);
canvas.drawText(text, 150, 240, title);
pdfDocument.finishPage(myPage);
}
I generate an event for adding header and footer to every page on my pdf document, the problem is that when I add a new image to the page, the new image appears under the header image. I had tried to find the solution, but I can´t find it, I have tried with image on png with Alpha channel set, but the problem don´t disappear.
class PieCabecera extends PdfPageEventHelper{
public int numeroPagina;
public Image imagen;
public PdfPTable tabla;
public PdfTemplate tpl;
public Phrase cabecera;
Font smallBold = new Font(Font.FontFamily.HELVETICA, 1, Font.BOLD);
/**
*
* #param writer
* #param documento
*/
#Override
public void onStartPage(PdfWriter writer, Document documento){
numeroPagina++;
try{
imagen = Image.getInstance("D:/Users/Operador/Documents/NetBeansProjects/ServiciosWeb-dev/web/img/logoPDF.jpg");
imagen.setAbsolutePosition(50, 0);
PdfContentByte cbCabecera = writer.getDirectContent();
tpl = cbCabecera.createTemplate(600, 250);
tpl.addImage(imagen);
cbCabecera.addTemplate(tpl, 0, 750);
cabecera = new Phrase(cbCabecera + ".", smallBold);
documento.add(cabecera);
Paragraph parrafo0 = new Paragraph();
parrafo0.setSpacingBefore(12);
parrafo0.setSpacingAfter(14);
documento.add(parrafo0);
/*Línea de separación*/
LineSeparator ls = new LineSeparator();
documento.add(new Chunk(ls));
Paragraph parrafo = new Paragraph();
parrafo.setSpacingBefore(4);
documento.add(parrafo);
}catch(BadElementException e){
LOGGER.log(Level.SEVERE, "Error: {0}", e.getStackTrace());
}catch( IOException e){
LOGGER.log(Level.SEVERE, "Error: {0}", e.getStackTrace());
}catch( DocumentException e){
LOGGER.log(Level.SEVERE, "Error: {0}", e.getStackTrace());
}
}
/**
*
* #param writer
* #param documento
*/
#Override
public void onEndPage(PdfWriter writer, Document documento){
Rectangle rect = writer.getBoxSize("art");
//header
ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, cabecera, rect.getRight(), rect.getTop(), 0);
//footer
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("Página %d", numeroPagina)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
}
Thanks in advance for your help.
Have you set the margins of the Document so that you take into account the height of both header and footer?