Skip to content

Commit

Permalink
ability to add image, fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Oct 22, 2015
1 parent 3abfdef commit bd5858e
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
id 'org.asciidoctor.gradle.asciidoctor' version '1.5.1'
}

String currentVersion = '0.1.10'
String currentVersion = '0.1.11'

version = currentVersion

Expand Down
24 changes: 24 additions & 0 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,30 @@ _Result_

image:spans.png[]

=== Images
You can insert an image calling one of `png`, `jpeg`, `emf`, `wmf`, `pict`, `dib` method inside the cell definition.

[source,groovy]
----
cell ('C') {
png image from 'https://goo.gl/UcL1wy'
}
----

_Result_

image:image.png[]

The source of the image can be String which either translates to URL if it starts with `https://` or `http://` or
a file path otherwise. For advanced usage it can be also byte array or any `InputStream`.

[WARNING]
====
Resizing images with API is not reliable so you need to resize your image properly before inserting into the spreadsheet.
====



=== Comments
You can set comment of any cell using the `comment` method. Use the variant accepting closure If you want to specify
the author of the comment as well. The author only appears in the status bar of the application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public ToKeyword getTo() {
return ToKeyword.TO;
}

public ImageKeyword getImage() {
return ImageKeyword.IMAGE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ public interface Cell extends HasStyle {
*/
void text(String text, @DelegatesTo(Font.class) @ClosureParams(value=FromString.class, options = "org.modelcatalogue.builder.spreadsheet.api.Font") Closure fontConfiguration);

ImageCreator png(ImageKeyword image);
ImageCreator jpeg(ImageKeyword image);
ImageCreator pict(ImageKeyword image);
ImageCreator emf(ImageKeyword image);
ImageCreator wmf(ImageKeyword image);
ImageCreator dib(ImageKeyword image);


ImageKeyword getImage();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.modelcatalogue.builder.spreadsheet.api;

import java.io.InputStream;

public interface ImageCreator {

void from(String fileOrUrl);
void from(InputStream stream);
void from(byte[] imageData);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.modelcatalogue.builder.spreadsheet.api;

public enum ImageKeyword {
IMAGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.modelcatalogue.builder.spreadsheet.poi
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.FromString
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFCell
import org.apache.poi.xssf.usermodel.XSSFName
import org.apache.poi.xssf.usermodel.XSSFRichTextString
Expand All @@ -12,6 +13,8 @@ import org.modelcatalogue.builder.spreadsheet.api.AutoKeyword
import org.modelcatalogue.builder.spreadsheet.api.CellStyle
import org.modelcatalogue.builder.spreadsheet.api.Comment
import org.modelcatalogue.builder.spreadsheet.api.Font
import org.modelcatalogue.builder.spreadsheet.api.ImageCreator
import org.modelcatalogue.builder.spreadsheet.api.ImageKeyword
import org.modelcatalogue.builder.spreadsheet.api.LinkDefinition
import org.modelcatalogue.builder.spreadsheet.api.ToKeyword

Expand Down Expand Up @@ -185,6 +188,40 @@ class PoiCell extends AbstractCell implements Resolvable {
richTextParts << new RichTextPart(run, font, start, end)
}

@Override
ImageCreator png(ImageKeyword image) {
return createImageConfigurer(Workbook.PICTURE_TYPE_PNG)
}

@Override
ImageCreator jpeg(ImageKeyword image) {
return createImageConfigurer(Workbook.PICTURE_TYPE_JPEG)
}

@Override
ImageCreator pict(ImageKeyword image) {
return createImageConfigurer(Workbook.PICTURE_TYPE_JPEG)
}

@Override
ImageCreator emf(ImageKeyword image) {
return createImageConfigurer(Workbook.PICTURE_TYPE_EMF)
}

@Override
ImageCreator wmf(ImageKeyword image) {
return createImageConfigurer(Workbook.PICTURE_TYPE_WMF)
}

@Override
ImageCreator dib(ImageKeyword image) {
return createImageConfigurer(Workbook.PICTURE_TYPE_DIB)
}

protected ImageCreator createImageConfigurer(int fileType) {
return new PoiImageCreator(this, fileType)
}

protected int getColspan() {
return colspan
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.modelcatalogue.builder.spreadsheet.poi

import org.apache.poi.ss.usermodel.ClientAnchor
import org.apache.poi.ss.usermodel.CreationHelper
import org.apache.poi.ss.usermodel.Drawing
import org.apache.poi.ss.usermodel.Picture
import org.modelcatalogue.builder.spreadsheet.api.ImageCreator

class PoiImageCreator implements ImageCreator {

private final PoiCell cell
private final int type

PoiImageCreator(PoiCell poiCell, int type) {
this.cell = poiCell
this.type = type
}

@Override
void from(String fileOrUrl) {
if (fileOrUrl.startsWith('https://') || fileOrUrl.startsWith('http://')) {
from new URL(fileOrUrl).newInputStream()
return
}
from new FileInputStream(new File(fileOrUrl))
}

@Override
void from(InputStream stream) {
addPicture(cell.row.sheet.sheet.workbook.addPicture(stream, type))
}

@Override
void from(byte[] imageData) {
addPicture(cell.row.sheet.sheet.workbook.addPicture(imageData, type))
}

void addPicture(int pictureIdx) {
Drawing drawing = cell.row.sheet.sheet.createDrawingPatriarch();

CreationHelper helper = cell.row.sheet.sheet.workbook.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(cell.cell.columnIndex)
anchor.setRow1(cell.cell.rowIndex)

Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class PoiExcelBuilderSpec extends Specification {
}
}

sheet('Image') {
row (3) {
cell ('C') {
png image from 'https://goo.gl/UcL1wy'
}
}
}

sheet('Rich Text') {
row {
cell {
Expand Down

0 comments on commit bd5858e

Please sign in to comment.