package com.ob.expressions.document; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import com.appiancorp.suiteapi.knowledge.Document; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import org.apache.log4j.Logger; import com.appiancorp.services.ServiceContext; import com.appiancorp.suiteapi.common.LocalObject; import com.appiancorp.suiteapi.common.ServiceLocator; import com.appiancorp.suiteapi.common.exceptions.InvalidVersionException; import com.appiancorp.suiteapi.common.exceptions.PrivilegeException; import com.appiancorp.suiteapi.common.exceptions.StorageLimitException; import com.appiancorp.suiteapi.content.ContentConstants; import com.appiancorp.suiteapi.content.ContentService; import com.appiancorp.suiteapi.content.ContentOutputStream; import com.appiancorp.suiteapi.content.exceptions.DuplicateUuidException; import com.appiancorp.suiteapi.content.exceptions.InsufficientNameUniquenessException; import com.appiancorp.suiteapi.content.exceptions.InvalidContentException; import com.appiancorp.suiteapi.expression.annotations.Function; import com.appiancorp.suiteapi.expression.annotations.Parameter; import com.appiancorp.suiteapi.knowledge.DocumentDataType; import com.appiancorp.suiteapi.knowledge.DocumentOrFolderDataType; import com.appiancorp.suiteapi.knowledge.FolderDataType; import com.appiancorp.suiteapi.type.AppianType; @AppianDocumentFunctionsCategory public class GetImageFile { private static final Logger LOG = Logger.getLogger(GetImageFile.class); @Function @DocumentDataType public Long ImageWithText(ServiceContext sc, @Parameter String InputText, @Parameter @DocumentDataType Long InputFile, @Parameter @FolderDataType Long FileLoc) { ContentService cs = ServiceLocator.getContentService(sc); BufferedImage image = null; Long image1 = null; try { String s = InputText; String InputFile1; try { //Read an Input Image InputFile1 = cs.getInternalFilename(cs.getVersionId(InputFile, ContentConstants.VERSION_CURRENT)); URL url = new URL(InputFile1); image = ImageIO.read(url); // Write text on Image Graphics g = image.getGraphics(); g.setFont(new Font("Calibri", Font.BOLD, 14)); g.setColor(Color.BLACK); g.drawString(s, 8, 20); g.dispose(); // Create the new document Document doc = new Document(); doc.setName(s); doc.setExtension("jpg"); doc.setParent(FileLoc); cs.create(doc, ContentConstants.UNIQUE_NONE); // ContentOutputStream cos = cs.upload(doc, ContentConstants.UNIQUE_NONE); //Write the image to the new document ImageIO.write(image, "jpg", (ImageOutputStream) doc); //cos.close(); image1 = doc.getId(); //image1 = cos.getContentId(); } catch (PrivilegeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidContentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidVersionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (StorageLimitException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InsufficientNameUniquenessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DuplicateUuidException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } return image1; } }