Working with Rich Text Images Using PersistableAdapter
This topic explains how to create, store, and retrieve images embedded in rich text attributes by using the PersistableAdapter and supported helper APIs.
Terminology
|
Term
|
Description
|
|
Rich text attribute
|
An attribute with the HTMLText data type.
|
|
Rich text holder
|
An object (such as a change notice or document) that contains one or more rich text attributes.
|
|
filename attribute in <img>
|
A platform‑defined attribute used to link an <img> tag in the HTML text to an actual image file stored in Windchill.
|
|
ApplicationData
|
The content object that represents a stored image file in Windchill.
|
Create Operation
Creating rich text content with images involves three main steps.
1. Generate unique image file names.
Before creating the rich text content, generate unique file names for the images that will be embedded. These names are UUID‑based and ensure correct mapping between the HTML and stored image content.
API
RichTextImageHelper.generateUniqueImageNames(Map<String, Integer> imageTypeCountMap)
Example:
Map<String, Integer> imageTypeCountMap = new HashMap<>();
imageTypeCountMap.put("png", 2);
imageTypeCountMap.put("jpg", 1);
Map<String, List<String>> uniqueNames =
RichTextImageHelper.generateUniqueImageNames(imageTypeCountMap);
Example output:
◦ 550e8400-e29b-41d4-a716-446655440000.png
◦ 6ba7b810-9dad-11d1-80b4-00c04fd430c8.png
◦ 6ba7b812-9dad-11d1-80b4-00c04fd430c8.jpg
|
|
• Rename your actual image files to exactly match these generated names.
• File names and extensions must match the values used later in the HTML.
|
2. Build the rich text HTML and create the object.
Create the rich text content and include one <img> tag for each image.
◦ Each <img> tag must include a filename attribute.
◦ The filename value must match one of the generated names.
◦ The order of <img> tags controls how images appear when the rich text is rendered.
◦ Optional image styling can be applied using the style attribute.
Example HTML:
String htmlText = "<p>Some Rich Text"
+ "<img filename=\"550e8400-e29b-41d4-a716-446655440000.png\" style=\"width: 300px; display: block; vertical-align: top; margin: 5px auto; text-align: center;\" />"
+ "<img filename=\"6ba7b812-9dad-11d1-80b4-00c04fd430c8.jpg\" style=\"width: 200px;\" />"
+ "<img filename=\"6ba7b810-9dad-11d1-80b4-00c04fd430c8.jpeg\" style=\"width: 150px;\" />"
+ "</p>";
Set this HTML string on the rich text attribute and persist the object using PersistableAdapter.
Example:
TypeDefinitionReadView type = TypeDefinitionManager.getTypeDefinitionManagerInstance() .getTypeDefView("wt.change2.WTChangeOrder2");
Locale locale = SessionHelper.getLocale();
PersistableAdapter pa = new PersistableAdapter( type.getName(), locale, new CreateOperationIdentifier());
pa.load("name", "theChangeNoticeComplexity", ATTRIBUTE_NAME);
pa.set("name", "MyChangeOrder_" + System.currentTimeMillis());
pa.set("theChangeNoticeComplexity", ChangeNoticeComplexity.BASIC);
pa.set(ATTRIBUTE_NAME, htmlText);
TypeInstanceIdentifier tii = pa.persist();
TypeInstance ti = TypeInstanceUtility.newTypeInstance(tii);
ContentHolder createdObject = (ContentHolder)
TypeInstanceUtility.getPersistable(ti);
3. Persist the image files.
After the object is created and the rich text attribute value is saved, persist the actual image files using the supported helper API.
API:
RichTextImageHelper.persistRichTextImageContents (ContentHolder richtextHolder, String richtextAttribute, Map<String, File> imageFiles)
This API associates the image files with the rich text attribute based on the filename values specified in the <img> tags.
|
|
Each file name must match the corresponding <img filename="..."> value in the HTML.
|
Example:
File img1 = new File("/path/to/550e8400-e29b-41d4-a716-446655440000.png");
File img2 = new File("/path/to/6ba7b812-9dad-11d1-80b4-00c04fd430c8.jpg");
File img3 = new File("/path/to/6ba7b810-9dad-11d1-80b4-00c04fd430c9.jpeg");
Map<String, File> images = new HashMap<>();
images.put("550e8400-e29b-41d4-a716-446655440000.png", img1);
images.put("6ba7b812-9dad-11d1-80b4-00c04fd430c8.jpg", img2);
images.put("6ba7b810-9dad-11d1-80b4-00c04fd430c9.jpeg", img3);
RichTextImageHelper.persistRichTextImageContents(createdObject, richTextAttribute, images);
When you complete this step successfully, the images are stored as content and are logically linked to the rich text attribute.
Rendering Rich Text Images with Downloadable URLs
Rich text stored in attributes contains <img> tags that reference images by filename. To render the rich text with <img> tags that include a valid content download URL in the src (source) attribute, use the following API.
RichTextImageHelper.insertSrcForImgTags(ContentHolder contentHolder,HTMLText htmlText)
Usage:
• contentHolder: The object that contains the rich text attribute.
• HTMLText: The object representing the rich text value that contains <img> tags with filename attributes.
This API updates the HTML so that each <img> tag includes a src attribute pointing to the appropriate content download URL.
Read Operation — Retrieving Associated Images
To retrieve images that are stored for rich text attributes, use one of the following APIs:
|
API
|
Description
|
|
getAllImageContents(ContentHolder)
|
Returns all images associated with all rich text attributes on the object.
|
|
getImageContents(ContentHolder, String)
|
Returns images associated with a specific rich text attribute.
|
Example
WTSet allImages = RichTextImageHelper.getAllImageContents(obj);
WTSet attributeImages = RichTextImageHelper.getImageContents(obj, ATTRIBUTE_NAME);
for (Object o : attributeImages) {
ApplicationData ad = (ApplicationData) o;
String fileName = ad.getFileName();}
The returned WTSet contains ApplicationData objects. All supported ApplicationData APIs can be used to work with these images.