2

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();
    }

}
2
  • Just check this answer. Hope it helps you. iText
    – Dhruv
    Feb 4, 2016 at 6:31
  • i am looking ,can it be done using PdfContentByte ,after all it behaves like a canvas. i dont want to use separator @DhruvVaishnav
    – abh22ishek
    Feb 4, 2016 at 6:35

1 Answer 1

2

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.

7
  • Strictly speaking the first example (and even more so the OP's code) is invalid: As soon as you start to create a path, only other path creating instructions until an optional clip path setting and a mandatory path drawing instruction. Thus, cb.setColorStroke(new BaseColor(Color.BLUE)) is invalid. PDF viewer usually accept it nonetheless...
    – mkl
    Feb 4, 2016 at 9:09
  • @mkl I'll update my answer to inform other readers that they shouldn't use that snippet. Feb 4, 2016 at 9:12
  • @mkl what do you mean cb.setColorStroke(new BaseColor(Color.BLUE)) is invalid. do you mean if i print in A4 sheet it wont take?
    – abh22ishek
    Feb 4, 2016 at 9:13
  • 2
    @abh22ishek See the update to my answer. You are not respecting the rules of ISO-32000-1. There's a logical sequence: (1) change the graphics state (color, line width,...), (2) construct the path (line to, move to, rectangle,...), (3) draw the path (fill, stroke,...). You are mixing things up by changing the graphics state (1) after constructing the path (2). Feb 4, 2016 at 9:19
  • got it @BrunoLowagie
    – abh22ishek
    Feb 4, 2016 at 9:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.