abh22ishek
abh22ishek

Reputation: 2671

How do i change the color of a line in itext?

I have drawn a rectangle in a pdf . Inside it i have drawn multiple lines . i want lines to appear in different colors as . for that i have written following lines of code, but its not working.

All lines appear in black including recatngle drawn on pdf

 PdfContentByte cb = writer.getDirectContent();
    try {

        cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, true), 24);
        cb.rectangle(0f,0f,842f,595f);
        cb.saveState();

    //  Rectangle rect=new Rectangle(56.69f,56.69f,737f,510.23f);
        final float bx=0f;
        float bX=842f;
        float by=0f;
        float bY=595f;
       float  x=14.17f;
        int count=0;

      float bXX=14.17f+bx;


         for(float k=0;k<842;k=k+x)
         {
             cb.moveTo(k+bx,by);
                cb.lineTo(k+bx,bY+bx); 

                cb.setLineWidth(7);
                cb.setColorStroke(new BaseColor(Color.BLUE));

         }



        for(float i=0;i<595;i=i+x)
        {
            cb.moveTo(bx,by+i);
            cb.lineTo(bx+bX,i+by); 
            cb.setColorStroke(new BaseColor(Color.RED));
        }



    //  ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("LEAD I"), 0,(12*graphHeight), 0);
        cb.restoreState();
        cb.stroke();


    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Upvotes: 3

Views: 3141

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

All lines are drawn at the moment you invoke the stroke() method, using the color that is "active" at that particular moment.

So if you do:

cb.setColorStroke(new BaseColor(Color.RED));
cb.moveTo(x1, y1);
cb.lineTo(x2, y2);
cb.setColorStroke(new BaseColor(Color.BLUE));
cb.moveTo(x3, y3);
cb.lineTo(x4, y4);
cb.stroke();

This is illegal syntax, because once you've started constructing a path, only other path construction operations, a clipping operation, and (finally) a drawing operation is allowed before you change the state. Nevertheless, most PDF viewer will tolerate syntax errors like this: the lines from x1, y1 to x2, y2and from x3, y3 to x4, y4 will be drawn in blue because that's the current graphics state at the moment the stroke operation is performed.

If you want the line from x1, y1 to x2, y2 to be drawn in red, you need to insert another stroke() operation:

cb.setColorStroke(new BaseColor(Color.RED));
cb.moveTo(x1, y1);
cb.lineTo(x2, y2);
cb.stroke();
cb.setColorStroke(new BaseColor(Color.BLUE));
cb.moveTo(x3, y3);
cb.lineTo(x4, y4);
cb.stroke();

In your code, your setColorStroke() operations, don't have any effect, because you're using saveState() and restoreState() incorrectly:

// you save the current graphics state: e.g. stroke color is black.
cb.saveState();
// you change the stroke color: it is now blue
cb.setColorStroke(new BaseColor(Color.BLUE));
// you return to the graphics state from before saveState:
cb.restoreState();
// the stroke color is now black again
// all changes to the graphics state after saveState() are rolled back

You should reorganize the way you save and restore the graphics state and you should draw the lines and shapes in the color you've just defined before changing those colors to something else again.

Upvotes: 3

Related Questions