Java Code Examples for com.lowagie.text.pdf.PdfTemplate#setHeight()

The following examples show how to use com.lowagie.text.pdf.PdfTemplate#setHeight() . 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: ImgWMF.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/** Reads the WMF into a template.
 * @param template the template to read to
 * @throws IOException on error
 * @throws DocumentException on error
 */    
public void readWMF(PdfTemplate template) throws IOException, DocumentException {
    setTemplateData(template);
    template.setWidth(getWidth());
    template.setHeight(getHeight());
    InputStream is = null;
    try {
        if (rawData == null){
            is = url.openStream();
        }
        else{
            is = new java.io.ByteArrayInputStream(rawData);
        }
        MetaDo meta = new MetaDo(is, template);
        meta.readAll();
    }
    finally {
        if (is != null) {
            is.close();
        }
    }
}
 
Example 2
Source File: ImgWMF.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Reads the WMF into a template.
 * @param template the template to read to
 * @throws IOException on error
 * @throws DocumentException on error
 */    
public void readWMF(PdfTemplate template) throws IOException, DocumentException {
    setTemplateData(template);
    template.setWidth(getWidth());
    template.setHeight(getHeight());
    InputStream is = null;
    try {
        if (rawData == null){
            is = url.openStream();
        }
        else{
            is = new java.io.ByteArrayInputStream(rawData);
        }
        MetaDo meta = new MetaDo(is, template);
        meta.readAll();
    }
    finally {
        if (is != null) {
            is.close();
        }
    }
}
 
Example 3
Source File: PdfExportHelper.java    From mdw with Apache License 2.0 5 votes vote down vote up
private void printDiagram(DocWriter writer, Chapter chapter, Process process,
        Rectangle pageSize) throws Exception {
    ProcessCanvas canvas = new ProcessCanvas(project, process);
    canvas.prepare();

    Dimension size = new PngProcessExporter(project).getDiagramSize(process);
    // create a template and a Graphics2D object that corresponds with it
    int w;
    int h;
    float scale;
    if ((float) size.width < pageSize.getWidth() * 0.8
            && (float) size.height < pageSize.getHeight() * 0.8) {
        w = size.width + 36;
        h = size.height + 36;
        scale = -1f;
    }
    else {
        scale = pageSize.getWidth() * 0.8f / (float) size.width;
        if (scale > pageSize.getHeight() * 0.8f / (float) size.height)
            scale = pageSize.getHeight() * 0.8f / (float) size.height;
        w = (int) (size.width * scale) + 36;
        h = (int) (size.height * scale) + 36;
    }

    PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
    PdfTemplate tp = cb.createTemplate(w, h);
    Graphics2D g2 = tp.createGraphics(w, h);
    if (scale > 0)
        g2.scale(scale, scale);
    tp.setWidth(w);
    tp.setHeight(h);
    canvas.paintComponent(g2);
    g2.dispose();
    Image img = new ImgTemplate(tp);
    chapter.add(img);
    canvas.dispose();
}
 
Example 4
Source File: PdfTimetableGridTable.java    From unitime with Apache License 2.0 5 votes vote down vote up
public void addTextVertical(PdfPCell cell, String text, boolean bold) throws Exception  {
	if (text==null) return;
       if (text.indexOf("<span")>=0)
           text = text.replaceAll("</span>","").replaceAll("<span .*>", "");
       Font font = PdfFont.getFont(bold);
	BaseFont bf = font.getBaseFont();
	float width = bf.getWidthPoint(text, font.getSize());
	PdfTemplate template = iWriter.getDirectContent().createTemplate(2 * font.getSize() + 4, width);
	template.beginText();
	template.setColorFill(Color.BLACK);
	template.setFontAndSize(bf, font.getSize());
	template.setTextMatrix(0, 2);
	template.showText(text);
	template.endText();
	template.setWidth(width);
	template.setHeight(font.getSize() + 2);
	//make an Image object from the template
	Image img = Image.getInstance(template);
	img.setRotationDegrees(270);
	//embed the image in a Chunk
	Chunk ck = new Chunk(img, 0, 0);
	
	if (cell.getPhrase()==null) {
		cell.setPhrase(new Paragraph(ck));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
	} else {
		cell.getPhrase().add(ck);
	}
}
 
Example 5
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 6
Source File: VerticalTextInCellsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Example with vertical text in Cells.
 */
@Test
public void main() throws Exception {

	// step1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step2
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("VerticalText.pdf"));
	// step3
	document.open();
	// step4

	// make a PdfTemplate with the vertical text
	PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
	BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false);
	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);
	// make an Image object from the template
	Image img = Image.getInstance(template);
	img.setRotationDegrees(90);
	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");
	document.add(table);

	// step5
	document.close();
}