1

I am trying to draw lines on image that needs to be loaded on pdf document, just like we draw graphics on paint event of any control, but it fails to do so.

Any Suggestions ?

Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
pdfDoc.AddHeader("Batting Report - ", txtSearchBox.Text);

iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(Properties.Resources.bgWW
                        , System.Drawing.Imaging.ImageFormat.Jpeg);


PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();

pdfDoc.Add(pic);

So, how do I modify the pic object of ItextSharpImage so that it can draw lines on the image?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Harsh Kumar Singhi
  • 201
  • 1
  • 3
  • 10
  • This is almost a duplicate of [How to add text to an image?](http://stackoverflow.com/questions/26814958/how-to-add-text-to-an-image) with as main difference that this new question is about adding lines instead of text. The principle however, remains the same: instead of adding text to the `PdfContentByte`, you add lines using `MoveTo()`, `LineTo()` and `Stroke()` commands. For more similar answers, please download the free ebook [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) – Bruno Lowagie Apr 10 '15 at 12:29
  • No, here I want to draw lines on the image base on x and y cordinates – Harsh Kumar Singhi Apr 10 '15 at 12:30
  • Is there any other way other than PdfContentbyte – Harsh Kumar Singhi Apr 10 '15 at 12:32
  • Can you show me a sample to draw lines on an image ? – Harsh Kumar Singhi Apr 10 '15 at 12:32
  • 1
    Hey, please give me the time to update the comment that is automatically added when voting to close the question. The free ebook, has examples such as http://stackoverflow.com/questions/26586093/how-to-add-a-shading-pattern-to-a-custom-shape http://stackoverflow.com/questions/28709603/draw-a-line-every-n-words-using-itextsharp and many other code samples that show how to use `MoveTo()` and `LineTo()`. – Bruno Lowagie Apr 10 '15 at 12:33
  • How do i add image to PdfContentByte and then draw lines on that image? – Harsh Kumar Singhi Apr 10 '15 at 12:35
  • 1
    By patiently reading the code samples that are available. – Bruno Lowagie Apr 10 '15 at 12:38
  • I finally found some time to write an example. I have two comments: (1) my example proves that your question is a duplicate because the same principle applies, and (2) the multiple comments revealed impatience. Being impatient does not create goodwill among the people who are willing to help. Your name is Harsh, but so was your behavior. – Bruno Lowagie Apr 10 '15 at 13:32

1 Answers1

3

Please take a look at the WatermarkedImages4 example. It is based on the WatermarkedImages1 example I referred to in the comments. The only different between the two examples is that we add text in the example written in answer to How to add text to an image? whereas we add lines in the example written in answer to your question.

We add images like this:

document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1)));

The getWatermarkedImage() method looks like this:

public Image getWatermarkedImage(PdfContentByte cb, Image img) throws DocumentException {
    float width = img.getScaledWidth();
    float height = img.getScaledHeight();
    PdfTemplate template = cb.createTemplate(width, height);
    template.addImage(img, width, 0, 0, height, 0, 0);
    template.saveState();
    template.setColorStroke(BaseColor.GREEN);
    template.setLineWidth(3);
    template.moveTo(width * .25f, height * .25f);
    template.lineTo(width * .75f, height * .75f);
    template.moveTo(width * .25f, height * .75f);
    template.lineTo(width * .25f, height * .25f);
    template.stroke();
    template.setColorStroke(BaseColor.WHITE);
    template.ellipse(0, 0, width, height);
    template.stroke();
    template.restoreState();
    return Image.getInstance(template);
}

As you can see, I add two green lines using moveTo(), lineTo() and stroke(). I also add a white ellipse using the ellipse() and stroke() method.

This results in a PDF that looks like this:

enter image description here

As you can see, the shape of the ellipse and the position of the lines are different for the different images because I defined my coordinates based on the width and the height of the image.

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