Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 27, 2022 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/69b0b171913a8aa497c2d0e29be6eb21 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/69b0b171913a8aa497c2d0e29be6eb21 to your computer and use it in GitHub Desktop.
Find and Replace Text in Visio Diagram VSD VSDX in Java
// Load the input diagram
Diagram diagram = new Diagram("FindReplaceText.vsdx");
DateFormat dateFormat = new SimpleDateFormat("dd/MMMM/yyyy");
Date myDate = new Date(System.currentTimeMillis());
Calendar cal = Calendar.getInstance();
// Prepare a collection old and new text
Hashtable<String, String> replacements = new Hashtable<String, String>();
replacements.put("[[CompanyName]]", "Research Society of XYZ");
replacements.put("[[CompanyName]]", "Research Society of XYZ");
replacements.put("[[EmplyeeName]]", "James Bond");
replacements.put("[[SubjectTitle]]", "The affect of the internet on social behavior in the industrialize world");
cal.setTime(myDate);
cal.add(Calendar.YEAR, -1);
System.out.println(dateFormat.format(cal.getTime()));
replacements.put("[[TimePeriod]]", dateFormat.format(cal.getTime()) + " -- " + dateFormat.format(myDate));
cal.setTime(myDate);
cal.add(Calendar.DAY_OF_MONTH, -7);
System.out.println(dateFormat.format(cal.getTime()));
replacements.put("[[SubmissionDate]]", dateFormat.format(cal.getTime()));
replacements.put("[[AmountReq]]", "$100,000");
cal.setTime(myDate);
cal.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(dateFormat.format(cal.getTime()));
replacements.put("[[DateApproved]]", dateFormat.format(cal.getTime()));
// Iterate through the shapes of a page
for (Shape shape : (Iterable<Shape>) diagram.getPages().getPage("Page-1").getShapes())
{
Set<String> keys = replacements.keySet();
for(String key: keys)
{
for (FormatTxt txt : (Iterable<FormatTxt>) shape.getText().getValue())
{
Txt tx = (Txt)((txt instanceof Txt) ? txt : null);
if (tx != null && tx.getText().contains(key))
{
// Find and replace text of a shape
tx.setText(tx.getText().replace(key, replacements.get(key)));
}
}
}
}
// Save the diagram
diagram.save("FindReplaceText_Out.vsdx", SaveFileFormat.VSDX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment