-1

Hi When I am generating form using java with itext I want to add form number on top left of the document above the header.Please let me know the ways to do it.

PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100);
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell1.setBorder(0);
cell2.setBorder(0);
cell3.setBorder(0);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);

How can I set the table alignment to start of the page margin.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
rajkumar chilukuri
  • 235
  • 1
  • 3
  • 8
  • What did you try? Are you creating a document from scratch or are you filling out an existing form? Did you use the `showTextAligned()` method? What went wrong? You need to clarify. In its current state, your question can't be answered because you are not giving us sufficient information. – Bruno Lowagie Dec 02 '14 at 06:52
  • @Lowagie How can apply showTextAligned() to a table. – rajkumar chilukuri Dec 02 '14 at 07:39

1 Answers1

0

Your question is very confusing. You say you are creating a form, but when you say form, you don't seem to be referring to an interactive form, but to an ordinary PDF containing a table.

You say you want to add a number above the header, but you are not telling us what you mean by header. You are assuming that the people reading your question can read your mind.

I guess you want to use a page event to add a String in the top left corner of each page. That would make your question almost a duplicate of itextsharp: How to generate a report with dynamic header in PDF using itextsharp?

You can create a subclass of PdfPageEventHelper like this:

public class Header extends PdfPageEventHelper {

    protected Phrase header;

    public void setHeader(Phrase header) {
        this.header = header;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContentUnder();
        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, header, 36, 806, 0);
    }
}

You can then use this Header class like this:

public void createPdf(String filename) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    Header event = new Header();
    writer.setPageEvent(event);
    // step 3
    document.open();
    // step 4
    List<Integer> factors;
    for (int i = 2; i < 301; i++) {
        factors = getFactors(i);
        if (factors.size() == 1) {
            document.add(new Paragraph("This is a prime number!"));
        }
        for (int factor : factors) {
            document.add(new Paragraph("Factor: " + factor));
        }
        event.setHeader(new Phrase(String.format("THE FACTORS OF %s", i)));
        document.newPage();
    }
    // step 5
    document.close();
}

In your case, you wouldn't have:

event.setHeader(new Phrase(String.format("THE FACTORS OF %s", i)));

You'd have something like:

event.setHeader(new Phrase(number));

Where number is the number you want to add at the coordinate x = 36, y = 806.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165