Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:31:21+00:00 2026-06-11T15:31:21+00:00

I am generating pdf file using com.itextpdf.text.* following is my code which creates the

  • 0

I am generating pdf file using com.itextpdf.text.* following is my code which creates the pdf file with title and header higlighted and rows, what i wanted to do is, create a pdf file with image on the top and rows with alternate color, how to do this in using com.itextpdf.text.*

response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\"");
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document,response.getOutputStream());

        try {
            document.open();
            addTitlePage(document, reportName);

            //float[] colsWidth = {1.5f,3f,4f,4f,2f};

            List<Float> colsWidth = new ArrayList<Float>();
            int iterator = 1;
           while (iterator <= headerMap.size()) {
               if(iterator==1){
                   colsWidth.add(1.5f); 
               }else{
                colsWidth.add(3f); 
               }
                iterator++;
            }
           float[] floatArray = ArrayUtils.toPrimitive(colsWidth.toArray(new Float[0]), 0.0F);

           PdfPTable table = new PdfPTable(floatArray);
            table.setWidthPercentage(98);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            PdfPCell c1 = new PdfPCell();
            for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                String headerName = (String) headerMap.get(it.next());
                c1 = new PdfPCell(new Phrase(headerName, headerFont));
                c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(c1);
            }
            table.setHeaderRows(1);
            table = custDAO.creadPDFTable(query, table);
            document.add(table);
            document.addAuthor(userViewModel.getUsername());
            document.addCreationDate();
            document.addCreator("POC");
            document.close();
            response.flushBuffer();

public PdfPTable creadPDFTable(String query,PdfPTable table){
        int numberOfColumns=0,sno=1;
        Connection connection = getConnection();
        if (connection != null) {
            try {
                PreparedStatement reportTablePS = connection.prepareStatement(query);
                ResultSet reportTable_rst = reportTablePS.executeQuery();
                ResultSetMetaData reportTable_rsmd = reportTable_rst.getMetaData();
                numberOfColumns = reportTable_rsmd.getColumnCount();
                while (reportTable_rst.next()) {
                    table.addCell(new PdfPCell(new Paragraph(String.valueOf(sno), textFont)));
                            for (int columnIterator = 1; columnIterator <= numberOfColumns; columnIterator++) {
                                 String column = reportTable_rst.getString(columnIterator);
                                 table.addCell(new PdfPCell(new Paragraph(column, textFont)));
                            }
                            sno++;
            }
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                try {
                    closeConnection(connection, null, null);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return table;
    }

private static void addTitlePage(Document document, String reportName) throws DocumentException {
        Paragraph preface = new Paragraph();
        addEmptyLine(preface, 1);
        /**
         * Lets write a big header
         */
        Paragraph paragraph = new Paragraph(reportName, titleFont);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);

        /**
         * Add one empty line
         */
        addEmptyLine(preface, 1);
        document.add(preface);
    }
    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

when i use the following i get the following exception ‘getoutputstream() has already called for this response’

i wanted to use this for inserting image.

            Image image = Image.getInstance(path+"images/abi.png");
            image.setAbsolutePosition(40f, 770f);
            image.scaleAbsolute(70f, 50f);
            document.add(image);

so how to go about doing this?

UPDATE :

i want to create a pdf file like this i just want to add image on the top and rows with alternate color like this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:31:22+00:00Added an answer on June 11, 2026 at 3:31 pm

    http://what-when-how.com/itext-5/decorating-tables-using-table-and-cell-events-itext-5/


    (source: what-when-how.com)


    (source: what-when-how.com)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using com.lowagie.text.FontFactory in generating a PDF file and am trying to use
I am generating one PDF from the Code Behind File using StringWriter and HtmlTextWriter.
i am writing some code in vb.net that will be generating a pdf file.
I'm generating pdf file with IoTcpdfBundle using Symfony2, but there's a strange behaviour that
I'm dynamically generating a PDF using ABCpdf which contains a table of contents that
I am using com.lowagie.text.xml.SAXiTextHandler to convert XML to PDF using iText(v - 2.1.7). I
I am generating a pdf file in asp.net c# using itextsharp. i am not
I am generating a PDF file dynamically from html/css using the cfdocument tag. There
I am generating PDF/HTML report from a BIRT template. I am using visibility property
I'm generating pdf using ReportLab. But when i'm trying to attach image in it,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.