Home Java Itext Measure Of Width



Measure Of Width
Posted on: March 13, 2008 at 12:00 AM
In this section, you will learn how we can measure the width of the page. iText api provides a getWidthPoint() to know the width of the page. The width will be in centimeters or inches.

Measure Of Width    

     

In this section, you will learn how we can measure the width of the page.  iText api provides a getWidthPoint() to know the width of the page. The width will be in centimeters or inches.

Code Description:
getWidthPoint():
If we have a Chunk and we want to know it's width on a page then we can use the method getWidthPoint(). It gives us the width in points and we will have to do maths to know the width in inches or centimeters.
setHorizontalScaling(float scale):
With the method setHorizontalScaling(float scale), we can shrink (scale < 1.0f) or expand (scale > 1.0f) the Chunk. In the example we first print the Chunk in its actual size, then we print it twice at half its size (scale = 0.5f).

The code of the program is given below:

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MeasureOfWidth {  
  public static void main(String[] args)throws Exception {
  System.out.println("Example of measure Width of the chunk");
  Document document = new Document();
  PdfWriter.getInstance(document,new 
FileOutputStream
("Width.pdf"));
  document.open();
  Chunk c = new Chunk("Rose India");
  float w = c.getWidthPoint();
  Paragraph p = new Paragraph("The width of the chunk: '");
  p.add(c);
  p.add("' is ");
  p.add(String.valueOf(w));
  p.add(" points or ");
  p.add(String.valueOf(w / 72f));
  p.add(" inches.");
  document.add(p);
  document.add(c);
  document.add(Chunk.NEWLINE);
  c.setHorizontalScaling(0.5f);
  document.add(c);
  document.add(c);  
    document.close();
  }
}

The output of the program is given below:

Download this example.

 

Related Tags for Measure Of Width:
capiidetexttimeiogetpageviwidthintthisiditexttexpoitopointlearnearexteilitdessectioncanimceinnoasmntcaesagepimehphowprossuetwkincisimellmetermeasureeaarxtwidtwssthapprono


More Tutorials from this section

Ask Questions?    Discuss: Measure Of Width   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.