Java Code Examples for com.lowagie.text.Paragraph#setLeading()

The following examples show how to use com.lowagie.text.Paragraph#setLeading() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FactoryProperties.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void setParagraphLeading(Paragraph p, String leading) {
	if (leading == null) {
		p.setLeading(0, 1.5f);
		return;
	}
	try {
		StringTokenizer tk = new StringTokenizer(leading, " ,");
		String v = tk.nextToken();
		float v1 = Float.parseFloat(v);
		if (!tk.hasMoreTokens()) {
			p.setLeading(v1, 0);
			return;
		}
		v = tk.nextToken();
		float v2 = Float.parseFloat(v);
		p.setLeading(v1, v2);
	} catch (Exception e) {
		p.setLeading(0, 1.5f);
	}
}
 
Example 2
Source File: TableBordersTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static PdfPCell makeCell(String text, int vAlignment, int hAlignment, Font font, float leading,
		float padding, Rectangle borders, boolean ascender, boolean descender) {
	Paragraph p = new Paragraph(text, font);
	p.setLeading(leading);

	PdfPCell cell = new PdfPCell(p);
	cell.setLeading(leading, 0);
	cell.setVerticalAlignment(vAlignment);
	cell.setHorizontalAlignment(hAlignment);
	cell.cloneNonPositionParameters(borders);
	cell.setUseAscender(ascender);
	cell.setUseDescender(descender);
	cell.setUseBorderPadding(true);
	cell.setPadding(padding);
	return cell;
}
 
Example 3
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeCurrencyEntry(Document document,String label , double value) throws Exception{
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.setIndentationLeft(18);
	questionParagraph.add(new Chunk(label.trim() + ": ",boldedFont)); 
    questionParagraph.add(new Chunk(CurrencyValidator.getInstance().format(value , LocaleContextHolder.getLocale()) , normalFont));  
	document.add(questionParagraph);
}
 
Example 4
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeEntry(Document document,String label) throws Exception{
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.setIndentationLeft(18);
	questionParagraph.add(new Chunk(label.trim()  ,boldedFont)); 
    document.add(questionParagraph);
}
 
Example 5
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeEntry(Document document,String label , String value) throws Exception{
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.setIndentationLeft(18);
	questionParagraph.add(new Chunk(label.trim() + ": ",boldedFont)); 
    questionParagraph.add(new Chunk(value == null ? "": value.trim() ,normalFont));  
	document.add(questionParagraph);
}
 
Example 6
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeSubTitle(Document document,String title) throws Exception{
	Paragraph titleParagraph = new Paragraph(title,subTitleFont);
	titleParagraph.setAlignment(Element.ALIGN_LEFT);
	titleParagraph.setLeading(20);
	titleParagraph.setSpacingAfter(2);
	document.add(titleParagraph);
}
 
Example 7
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeTitle(Document document,String title) throws Exception{
	Paragraph titleParagraph = new Paragraph(title,titleFont);
	titleParagraph.setAlignment(Element.ALIGN_LEFT);
	titleParagraph.setLeading(30);
	titleParagraph.setSpacingAfter(2);
	document.add(titleParagraph);
}
 
Example 8
Source File: SurveyPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeAnswer(Document document,String questionText , String answerValue) throws Exception{
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.add(new Chunk(questionText.trim() + ": ",boldedFont)); 
    questionParagraph.add(new Chunk(answerValue == null ? "": answerValue.trim() ,normalFont));  
	document.add(questionParagraph);
}
 
Example 9
Source File: StatisticsPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeEntry(Document document,String label , Date value, String dateFormat) throws Exception{
	Paragraph questionParagraph = new Paragraph();
	questionParagraph.setLeading(14, 0);
	questionParagraph.setIndentationLeft(18);
	questionParagraph.add(new Chunk(label.trim() + ": ",boldedFont)); 
    questionParagraph.add(new Chunk(DateValidator.getInstance().format(value ,dateFormat) , normalFont));  
	document.add(questionParagraph);
}
 
Example 10
Source File: SurveyPdf.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
private void writeTitle(Document document,String title) throws Exception{
	Paragraph titleParagraph = new Paragraph(title,titleFont);
	titleParagraph.setAlignment(Element.ALIGN_LEFT);
	titleParagraph.setLeading(30);
	titleParagraph.setSpacingAfter(2);
	document.add(titleParagraph);
}
 
Example 11
Source File: PDFPrinter.java    From unitime with Apache License 2.0 5 votes vote down vote up
@Override
public void printHeader(String... fields) {
	iTable = new PdfPTable(fields.length - iHiddenColumns.size());
	iMaxWidth = new float[fields.length];
	iTable.setHeaderRows(1);
	iTable.setWidthPercentage(100);

	for (int idx = 0; idx < fields.length; idx++) {
		if (iHiddenColumns.contains(idx)) continue;
		String f = fields[idx];
		
		PdfPCell cell = new PdfPCell();
		cell.setBorder(Rectangle.BOTTOM);
		cell.setVerticalAlignment(Element.ALIGN_TOP);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		
		Font font = PdfFont.getFont(true);
		Paragraph ch = new Paragraph(f, font);
		ch.setLeading(0f, 1f);
		cell.addElement(ch);		
		iTable.addCell(cell);
		
		float width = 0; 
		if (f.indexOf('\n')>=0) {
			for (StringTokenizer s = new StringTokenizer(f,"\n"); s.hasMoreTokens();)
				width = Math.max(width,font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
		} else 
			width = Math.max(width,font.getBaseFont().getWidthPoint(f, font.getSize()));
		iMaxWidth[idx] = width;
	}
}
 
Example 12
Source File: PdfWorksheet.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected void printFooter() throws DocumentException {
    out("");
    out(renderEnd(renderMiddle("","Page "+(iPageNo+1)),"<"+iCurrentSubjectArea.getSubjectAreaAbbreviation()+(iCourseNumber!=null?" "+iCourseNumber:"")+">  "));
	//FIXME: For some reason when a line starts with space, the line is shifted by one space in the resulting PDF (when using iText 5.0.2)
    Paragraph p = new Paragraph(iBuffer.toString().replace("\n ", "\n  "), PdfFont.getFixedFont());
    p.setLeading(9.5f); //was 13.5f
    iDoc.add(p);
    iBuffer = new StringBuffer();
    iPageNo++;
}
 
Example 13
Source File: MultiColumnR2LTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Element newPara(String text, int alignment, int type) {
	Font font = FontFactory.getFont("Helvetica", 10, type, Color.BLACK);
	Paragraph p = new Paragraph(text, font);
	p.setAlignment(alignment);
	p.setLeading(font.getSize() * 1.2f);
	return p;
}
 
Example 14
Source File: MultiColumnSimpleTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Element newPara(String text, int alignment, int type) {
	Font font = FontFactory.getFont("Helvetica", 10, type, Color.BLACK);
	Paragraph p = new Paragraph(text, font);
	p.setAlignment(alignment);
	p.setLeading(font.getSize() * 1.2f);
	return p;
}
 
Example 15
Source File: PdfDocument.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Adds a <CODE>PdfPTable</CODE> to the document.
 * @param ptable the <CODE>PdfPTable</CODE> to be added to the document.
 * @throws DocumentException on error
 */
void addPTable(PdfPTable ptable) throws DocumentException {
    ColumnText ct = new ColumnText(writer.getDirectContent());
    // if the table prefers to be on a single page, and it wouldn't
    //fit on the current page, start a new page.
    if (ptable.getKeepTogether() && !fitsPage(ptable, 0f) && currentHeight > 0)  {
    	newPage();
    }
    // add dummy paragraph if we aren't at the top of a page, so that
    // spacingBefore will be taken into account by ColumnText
    if (currentHeight > 0) {
     Paragraph p = new Paragraph();
     p.setLeading(0);
     ct.addElement(p);
    }
    ct.addElement(ptable);
    boolean he = ptable.isHeadersInEvent();
    ptable.setHeadersInEvent(true);
    int loop = 0;
    while (true) {
        ct.setSimpleColumn(indentLeft(), indentBottom(), indentRight(), indentTop() - currentHeight);
        int status = ct.go();
        if ((status & ColumnText.NO_MORE_TEXT) != 0) {
            text.moveText(0, ct.getYLine() - indentTop() + currentHeight);
            currentHeight = indentTop() - ct.getYLine();
            break;
        }
        if (indentTop() - currentHeight == ct.getYLine())
            ++loop;
        else
            loop = 0;
        if (loop == 3) {
            add(new Paragraph("ERROR: Infinite table loop"));
            break;
        }
        newPage();
    }
    ptable.setHeadersInEvent(he);
}
 
Example 16
Source File: PdfDocument.java    From MesquiteCore with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** Adds a <CODE>PdfPTable</CODE> to the document.
 * @param ptable the <CODE>PdfPTable</CODE> to be added to the document.
 * @throws DocumentException on error
 */
void addPTable(PdfPTable ptable) throws DocumentException {
    ColumnText ct = new ColumnText(writer.getDirectContent());
    if (currentHeight > 0) {
        Paragraph p = new Paragraph();
        p.setLeading(0);
        ct.addElement(p);
    }
    ct.addElement(ptable);
    boolean he = ptable.isHeadersInEvent();
    ptable.setHeadersInEvent(true);
    int loop = 0;
    while (true) {
        ct.setSimpleColumn(indentLeft(), indentBottom(), indentRight(), indentTop() - currentHeight);
        int status = ct.go();
        if ((status & ColumnText.NO_MORE_TEXT) != 0) {
            text.moveText(0, ct.getYLine() - indentTop() + currentHeight);
            currentHeight = indentTop() - ct.getYLine();
            break;
        }
        if (indentTop() - currentHeight == ct.getYLine())
            ++loop;
        else
            loop = 0;
        if (loop == 3) {
            add(new Paragraph("ERROR: Infinite table loop"));
            break;
        }
        newPage();
    }
    ptable.setHeadersInEvent(he);
}
 
Example 17
Source File: PdfDocument.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Adds a <CODE>PdfPTable</CODE> to the document.
 *
 * @param ptable the <CODE>PdfPTable</CODE> to be added to the document.
 * @throws DocumentException on error
 */
void addPTable(PdfPTable ptable) throws DocumentException {
	ColumnText ct = new ColumnText(writer.getDirectContent());
	// if the table prefers to be on a single page, and it wouldn't
	// fit on the current page, start a new page.
	if (ptable.getKeepTogether() && !fitsPage(ptable, 0f) && currentHeight > 0) {
		newPage();
	}
	// add dummy paragraph if we aren't at the top of a page, so that
	// spacingBefore will be taken into account by ColumnText
	if (currentHeight > 0) {
		Paragraph p = new Paragraph();
		p.setLeading(0);
		ct.addElement(p);
	}
	ct.addElement(ptable);
	boolean he = ptable.isHeadersInEvent();
	ptable.setHeadersInEvent(true);
	int loop = 0;
	while (true) {
		ct.setSimpleColumn(indentLeft(), indentBottom(), indentRight(), indentTop() - currentHeight);
		int status = ct.go();
		if ((status & ColumnText.NO_MORE_TEXT) != 0) {
			text.moveText(0, ct.getYLine() - indentTop() + currentHeight);
			currentHeight = indentTop() - ct.getYLine();
			break;
		}
		if (indentTop() - currentHeight == ct.getYLine()) {
			++loop;
		} else {
			loop = 0;
		}
		if (loop == 3) {
			add(new Paragraph("ERROR: Infinite table loop"));
			break;
		}
		newPage();
	}
	ptable.setHeadersInEvent(he);
}
 
Example 18
Source File: TemplateImagesTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * PdfTemplates can be wrapped in an Image.
    */
@Test
   public  void main() throws Exception {
       
           
       // step 1: creation of a document-object
       Rectangle rect = new Rectangle(PageSize.A4);
       rect.setBackgroundColor(new Color(238, 221, 88));
       Document document = new Document(rect, 50, 50, 50, 50);
	// step 2: we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("templateImages.pdf"));
	// step 3: we open the document
	document.open();
	// step 4:
	PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
	BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
			BaseFont.NOT_EMBEDDED);
	String text = "Vertical";
	float size = 16;
	float width = bf.getWidthPoint(text, size);
	template.beginText();
	template.setRGBColorFillF(1, 1, 1);
	template.setFontAndSize(bf, size);
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(size + 2);
	template.sanityCheck();
	Image img = Image.getInstance(template);
	img.setRotationDegrees(90);
	Chunk ck = new Chunk(img, 0, 0);
	PdfPTable table = new PdfPTable(3);
	table.setWidthPercentage(100);
	table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
	PdfPCell cell = new PdfPCell(img);
	cell.setPadding(4);
	cell.setBackgroundColor(new Color(0, 0, 255));
	cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");
	table.addCell(cell);
	table.addCell("I see a template everywhere");
	table.addCell(cell);
	table.addCell("I see a template on my right");
	table.addCell(cell);
	table.addCell("I see a template on my left");

	Paragraph p1 = new Paragraph("This is a template ");
	p1.add(ck);
	p1.add(" just here.");
	p1.setLeading(img.getScaledHeight() * 1.1f);
	document.add(p1);
	document.add(table);
	Paragraph p2 = new Paragraph("More templates ");
	p2.setLeading(img.getScaledHeight() * 1.1f);
	p2.setAlignment(Element.ALIGN_JUSTIFIED);
	img.scalePercent(70);
	for (int k = 0; k < 20; ++k)
		p2.add(ck);
	document.add(p2);
	// step 5: we close the document
	document.close();

   }
 
Example 19
Source File: DocStyleUtils.java    From DWSurvey with GNU Affero General Public License v3.0 3 votes vote down vote up
/** 
 * 功能说明:设置段落的样式,设置前半截内容填充了浅灰色的背景颜色,后半截内容没有背景颜色的段落样式</BR> 
 * 修改日期:2011-04-27 
 * @author myclover 
 * @param content  前半截有背景颜色的内容 
 * @param font     字体的样式 
 * @param firstLineIndent 首行缩进的字符,16f约等于一个字符 
 * @param leading  行间距12f表示单倍行距 
 * @param appendStr 后半截内容 
 * @return 
 */  
public static Paragraph setParagraphStyle(String content , Font font , float firstLineIndent , float leading , String appendStr){  
    Phrase phrase = setPhraseStyle(content , appendStr);  
    Paragraph par = new Paragraph(phrase);  
    par.setFont(font);  
    par.setFirstLineIndent(firstLineIndent);  
    par.setLeading(leading);  
    //设置对齐方式为两端对齐  
    par.setAlignment(Paragraph.ALIGN_JUSTIFIED_ALL);  
    return par;  
}
 
Example 20
Source File: DocStyleUtils.java    From DWSurvey with GNU Affero General Public License v3.0 3 votes vote down vote up
/** 
 * 功能说明:设置段落的样式</BR> 
 * 修改日期:2011-04-27 
 * @author myclover 
 * @param content  段落的内容 
 * @param font     字体的样式 
 * @param firstLineIndent  首行缩进多少字符,16f约等于一个字符 
 * @param leading  行间距 
 * @param indentationRight 缩进样式中的文本之后多少毫米,0.6f相当于0.2毫米 
 * @param alignment 对齐方式 
 * @return 
 */  
public static Paragraph setParagraphStyle(String content , Font font , float firstLineIndent , float leading , float indentationRight , int alignment){  
    Paragraph par = new Paragraph(content, font);  
    par.setFirstLineIndent(firstLineIndent);  
    par.setLeading(leading);  
    par.setIndentationRight(indentationRight);  
    par.setAlignment(alignment);  
    return par;  
}