Skip to content

Commit 9dfb035

Browse files
Update _index.md
1 parent ee8d372 commit 9dfb035

File tree

1 file changed

+71
-15
lines changed
  • content/english/java/document-loading-and-saving/advance-html-documents-saving-options

1 file changed

+71
-15
lines changed

content/english/java/document-loading-and-saving/advance-html-documents-saving-options/_index.md

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void exportRoundtripInformation() throws Exception {
2929
With the `exportFontsAsBase64` method, you can export fonts used in the document as Base64-encoded data in the HTML. This ensures that the HTML representation retains the same font styles as the original Word document.
3030

3131
```java
32-
@Test
32+
3333
public void exportFontsAsBase64() throws Exception {
3434
Document doc = new Document("Your Directory Path" + "Rendering.docx");
3535
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
@@ -42,7 +42,7 @@ public void exportFontsAsBase64() throws Exception {
4242
The `exportResources` method allows you to specify the type of CSS stylesheet and export font resources. You can also set a resource folder and an alias for resources in the HTML.
4343

4444
```java
45-
@Test
45+
4646
public void exportResources() throws Exception {
4747
Document doc = new Document("Your Directory Path" + "Rendering.docx");
4848
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
@@ -58,27 +58,48 @@ public void exportResources() throws Exception {
5858
The `convertMetafilesToEmfOrWmf` method allows you to convert metafiles in the document to either EMF or WMF format, ensuring compatibility and smooth rendering in HTML.
5959

6060
```java
61-
@Test
61+
6262
public void convertMetafilesToEmfOrWmf() throws Exception {
63-
// Code snippet not shown for brevity.
63+
64+
string dataDir = "Your Document Directory";
65+
Document doc = new Document();
66+
DocumentBuilder builder = new DocumentBuilder(doc);
67+
68+
builder.write("Here is an image as is: ");
69+
builder.insertHtml(
70+
"<img src=\"data:image/png;base64,\r\n iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP\r\n C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA\r\n AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J\r\n REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq\r\n ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0\r\n vr4MkhoXe0rZigAAAABJRU5ErkJggg==\" alt=\"Red dot\" />");
71+
72+
HtmlSaveOptions saveOptions = new HtmlSaveOptions(); { saveOptions.setMetafileFormat(HtmlMetafileFormat.EMF_OR_WMF); }
73+
74+
doc.save(dataDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToEmfOrWmf.html", saveOptions);
6475
}
6576
```
6677

6778
## 6. Convert Metafiles to SVG
6879
Use the `convertMetafilesToSvg` method to convert metafiles to SVG format. This format is ideal for displaying vector graphics in HTML documents.
6980

7081
```java
71-
@Test
82+
7283
public void convertMetafilesToSvg() throws Exception {
73-
// Code snippet not shown for brevity.
84+
string dataDir = "Your Document Directory";
85+
Document doc = new Document();
86+
DocumentBuilder builder = new DocumentBuilder(doc);
87+
88+
builder.write("Here is an SVG image: ");
89+
builder.insertHtml(
90+
"<svg height='210' width='500'>\r\n <polygon points='100,10 40,198 190,78 10,78 160,198' \r\n style='fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;' />\r\n </svg> ");
91+
92+
HtmlSaveOptions saveOptions = new HtmlSaveOptions(); { saveOptions.setMetafileFormat(HtmlMetafileFormat.SVG); }
93+
94+
doc.save(dataDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToSvg.html", saveOptions);
7495
}
7596
```
7697

7798
## 7. Add CSS Class Name Prefix
7899
With the `addCssClassNamePrefix` method, you can add a prefix to CSS class names in the exported HTML. This helps prevent conflicts with existing styles.
79100

80101
```java
81-
@Test
102+
82103
public void addCssClassNamePrefix() throws Exception {
83104
Document doc = new Document("Your Directory Path" + "Rendering.docx");
84105
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
@@ -92,36 +113,71 @@ public void addCssClassNamePrefix() throws Exception {
92113
The `exportCidUrlsForMhtmlResources` method is used when saving documents in MHTML format. It allows exporting Content-ID URLs for resources.
93114

94115
```java
95-
@Test
116+
96117
public void exportCidUrlsForMhtmlResources() throws Exception {
97-
// Code snippet not shown for brevity.
118+
string dataDir = "Your Document Directory";
119+
Document doc = new Document(dataDir + "Content-ID.docx");
120+
121+
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.MHTML);
122+
{
123+
saveOptions.setPrettyFormat(true); saveOptions.setExportCidUrlsForMhtmlResources(true);
124+
}
125+
126+
doc.save(dataDir + "WorkingWithHtmlSaveOptions.ExportCidUrlsForMhtmlResources.mhtml", saveOptions);
98127
}
99128
```
100129

101130
## 9. Resolve Font Names
102131
The `resolveFontNames` method helps resolve font names when saving documents in HTML format, ensuring consistent rendering across different platforms.
103132

104133
```java
105-
@Test
134+
106135
public void resolveFontNames() throws Exception {
107-
// Code snippet not shown for brevity.
136+
137+
string dataDir = "Your Document Directory";
138+
Document doc = new Document(dataDir + "Missing font.docx");
139+
140+
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.HTML);
141+
{
142+
saveOptions.setPrettyFormat(true); saveOptions.setResolveFontNames(true);
143+
}
144+
145+
doc.save(dataDir + "WorkingWithHtmlSaveOptions.ResolveFontNames.html", saveOptions);
108146
}
109147
```
110148

111149
## 10. Export Text Input Form Field as Text
112150
The `exportTextInputFormFieldAsText` method exports form fields as plain text in the HTML, making them easily readable and editable.
113151

114152
```java
115-
@Test
153+
116154
public void exportTextInputFormFieldAsText() throws Exception {
117-
// Code snippet not shown for brevity.
155+
156+
string dataDir = "Your Document Directory";
157+
Document doc = new Document(dataDir + "Rendering.docx");
158+
159+
String imagesDir = Path.combine(dataDir, "Images");
160+
161+
// The folder specified needs to exist and should be empty.
162+
if (Directory.exists(imagesDir))
163+
Directory.delete(imagesDir, true);
164+
165+
Directory.createDirectory(imagesDir);
166+
167+
// Set an option to export form fields as plain text, not as HTML input elements.
168+
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.HTML);
169+
{
170+
saveOptions.setExportTextInputFormFieldAsText(true); saveOptions.setImagesFolder(imagesDir);
171+
}
172+
173+
doc.save(dataDir + "WorkingWithHtmlSaveOptions.ExportTextInputFormFieldAsText.html", saveOptions);
118174
}
119175
```
120176

121-
## 11. Conclusion
177+
## Conclusion
122178
In this tutorial, we explored the advanced HTML document saving options provided by Aspose.Words for Java. These options give you fine-grained control over the conversion process, allowing you to create HTML documents that closely resemble the original Word documents.
123179

124-
## 12. FAQs
180+
## FAQ's
125181
Here are some frequently asked questions about working with Aspose.Words for Java and HTML document saving options:
126182

127183
### Q1: How can I convert HTML back to Word format using Aspose.Words for Java?

0 commit comments

Comments
 (0)