Skip to content

Staging #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ builder.writeln();
shape = builder.insertShape(ShapeType.TEXT_BOX, 50.0, 50.0);
shape.setRotation(30.0);

OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.DOCX);
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);

doc.save("Your Directory Path" + "WorkingWithShapes.InsertShape.docx", saveOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Now, it's time to convert the loaded document to the chosen output format. Aspos

```java
// Convert the document to PDF
doc.save("output.pdf", SaveFormat.PDF);
doc.save("output.pdf");
```

## Conclusion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Once the document is loaded, the next step is to set up the options for saving t
`ImageSaveOptions` is a class that allows you to specify how the document should be saved as an image.

```java
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.PNG);
ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
```

Explanation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Next, you'll configure the save options for the document. This is where you can

```java
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setSaveFormat(SaveFormat.EPUB);
saveOptions.setSaveFormat();
saveOptions.setEncoding(StandardCharsets.UTF_8);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Next, convert the loaded Word document to PDF:

```java
// Save the document as PDF
doc.save("output.pdf", SaveFormat.PDF);
doc.save("output.pdf");
```

## Step 4: Converting to Other Formats
Expand All @@ -67,21 +67,21 @@ Besides PDF, Aspose.Words for Java allows you to convert documents to various ot

```java
// Save the document as RTF
doc.save("output.rtf", SaveFormat.RTF);
doc.save("output.rtf");
```

### Converting to HTML

```java
// Save the document as HTML
doc.save("output.html", SaveFormat.HTML);
doc.save("output.html");
```

### Converting to EPUB

```java
// Save the document as EPUB
doc.save("output.epub", SaveFormat.EPUB);
doc.save("output.epub");
```

## Tips for Effective Document Converting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void exportRoundtripInformation() throws Exception {
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.

```java
@Test

public void exportFontsAsBase64() throws Exception {
Document doc = new Document("Your Directory Path" + "Rendering.docx");
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
Expand All @@ -42,7 +42,7 @@ public void exportFontsAsBase64() throws Exception {
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.

```java
@Test

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

```java
@Test

public void convertMetafilesToEmfOrWmf() throws Exception {
// Code snippet not shown for brevity.

string dataDir = "Your Document Directory";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("Here is an image as is: ");
builder.insertHtml(
"<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\" />");

HtmlSaveOptions saveOptions = new HtmlSaveOptions(); { saveOptions.setMetafileFormat(HtmlMetafileFormat.EMF_OR_WMF); }

doc.save(dataDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToEmfOrWmf.html", saveOptions);
}
```

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

```java
@Test

public void convertMetafilesToSvg() throws Exception {
// Code snippet not shown for brevity.
string dataDir = "Your Document Directory";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.write("Here is an SVG image: ");
builder.insertHtml(
"<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> ");

HtmlSaveOptions saveOptions = new HtmlSaveOptions(); { saveOptions.setMetafileFormat(HtmlMetafileFormat.SVG); }

doc.save(dataDir + "WorkingWithHtmlSaveOptions.ConvertMetafilesToSvg.html", saveOptions);
}
```

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

```java
@Test

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

```java
@Test

public void exportCidUrlsForMhtmlResources() throws Exception {
// Code snippet not shown for brevity.
string dataDir = "Your Document Directory";
Document doc = new Document(dataDir + "Content-ID.docx");

HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.MHTML);
{
saveOptions.setPrettyFormat(true); saveOptions.setExportCidUrlsForMhtmlResources(true);
}

doc.save(dataDir + "WorkingWithHtmlSaveOptions.ExportCidUrlsForMhtmlResources.mhtml", saveOptions);
}
```

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

```java
@Test

public void resolveFontNames() throws Exception {
// Code snippet not shown for brevity.

string dataDir = "Your Document Directory";
Document doc = new Document(dataDir + "Missing font.docx");

HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.HTML);
{
saveOptions.setPrettyFormat(true); saveOptions.setResolveFontNames(true);
}

doc.save(dataDir + "WorkingWithHtmlSaveOptions.ResolveFontNames.html", saveOptions);
}
```

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

```java
@Test

public void exportTextInputFormFieldAsText() throws Exception {
// Code snippet not shown for brevity.

string dataDir = "Your Document Directory";
Document doc = new Document(dataDir + "Rendering.docx");

String imagesDir = Path.combine(dataDir, "Images");

// The folder specified needs to exist and should be empty.
if (Directory.exists(imagesDir))
Directory.delete(imagesDir, true);

Directory.createDirectory(imagesDir);

// Set an option to export form fields as plain text, not as HTML input elements.
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.HTML);
{
saveOptions.setExportTextInputFormFieldAsText(true); saveOptions.setImagesFolder(imagesDir);
}

doc.save(dataDir + "WorkingWithHtmlSaveOptions.ExportTextInputFormFieldAsText.html", saveOptions);
}
```

## 11. Conclusion
## Conclusion
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.

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

### Q1: How can I convert HTML back to Word format using Aspose.Words for Java?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ In this code, we create an HTML string and use `HtmlLoadOptions` to specify that
Now that we have loaded the HTML into a `Document`, we can save it as a Word document. Let's save it in DOCX format:

```java
doc.save("Your Directory Path" + "WorkingWithHtmlLoadOptions.PreferredControlType.docx", SaveFormat.DOCX);
doc.save("Your Directory Path" + "WorkingWithHtmlLoadOptions.PreferredControlType.docx");
```

This code saves the `Document` as a DOCX file, which is a common format for Word documents.
Expand All @@ -68,7 +68,7 @@ HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setPreferredControlType(HtmlControlType.STRUCTURED_DOCUMENT_TAG);
}
Document doc = new Document(new ByteArrayInputStream(HTML.getBytes(StandardCharsets.UTF_8)), loadOptions);
doc.save("Your Directory Path" + "WorkingWithHtmlLoadOptions.PreferredControlType.docx", SaveFormat.DOCX);
doc.save("Your Directory Path" + "WorkingWithHtmlLoadOptions.PreferredControlType.docx");
```

## Conclusion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import com.aspose.words.SaveFormat;
Document doc = new Document("LegacyControlChars.doc");

// Create OoxmlSaveOptions with the FLAT_OPC format and enable keeping legacy control characters
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.FLAT_OPC);
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setKeepLegacyControlChars(true);

// Save the document with legacy control characters
Expand Down Expand Up @@ -148,7 +148,7 @@ public void updateLastSavedTimeProperty() throws Exception
public void keepLegacyControlChars() throws Exception
{
Document doc = new Document("Your Directory Path" + "Legacy control character.doc");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.FLAT_OPC); { saveOptions.setKeepLegacyControlChars(true); }
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); { saveOptions.setKeepLegacyControlChars(true); }
doc.save("Your Directory Path" + "WorkingWithOoxmlSaveOptions.KeepLegacyControlChars.docx", saveOptions);
}
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Next, you'll need to configure the PCL save options. These options specify the f
```java
PclSaveOptions saveOptions = new PclSaveOptions();
{
saveOptions.setSaveFormat(SaveFormat.PCL);
saveOptions.setSaveFormat();
saveOptions.setRasterizeTransformedElements(false);
}
```
Expand All @@ -56,7 +56,7 @@ Replace `"YourPCLDocument.pcl"` with the desired name for your PCL file.
Document doc = new Document("Your Directory Path" + "Rendering.docx");
PclSaveOptions saveOptions = new PclSaveOptions();
{
saveOptions.setSaveFormat(SaveFormat.PCL); saveOptions.setRasterizeTransformedElements(false);
saveOptions.setSaveFormat(); saveOptions.setRasterizeTransformedElements(false);
}
doc.save("Your Directory Path" + "WorkingWithPclSaveOptions.RasterizeTransformedElements.pcl", saveOptions);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To save images as TIFF format with threshold control, follow these steps:

```java
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.TIFF);
ImageSaveOptions saveOptions = new ImageSaveOptions();
saveOptions.setTiffCompression(TiffCompression.CCITT_3);
saveOptions.setImageColorMode(ImageColorMode.GRAYSCALE);
saveOptions.setTiffBinarizationMethod(ImageBinarizationMethod.FLOYD_STEINBERG_DITHERING);
Expand All @@ -36,7 +36,7 @@ To save a specific page as a multipage TIFF, use the following code:

```java
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.TIFF);
ImageSaveOptions saveOptions = new ImageSaveOptions();
saveOptions.setPageSet(new PageSet(new PageRange(0, 1)));
saveOptions.setTiffCompression(TiffCompression.CCITT_4);
saveOptions.setResolution(160f);
Expand All @@ -49,7 +49,7 @@ To save images as 1 BPP indexed PNG, follow these steps:

```java
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.PNG);
ImageSaveOptions saveOptions = new ImageSaveOptions();
saveOptions.setPageSet(new PageSet(1));
saveOptions.setImageColorMode(ImageColorMode.BLACK_AND_WHITE);
saveOptions.setPixelFormat(ImagePixelFormat.FORMAT_1_BPP_INDEXED);
Expand All @@ -62,7 +62,7 @@ To save a specific page as JPEG with customization options, use this code:

```java
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
ImageSaveOptions options = new ImageSaveOptions();
options.setPageSet(new PageSet(0));
options.setImageBrightness(0.3f);
options.setImageContrast(0.7f);
Expand All @@ -76,7 +76,7 @@ You can use a callback to customize page saving. Here's an example:

```java
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.PNG);
ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
imageSaveOptions.setPageSet(new PageSet(new PageRange(0, doc.getPageCount() - 1)));
imageSaveOptions.setPageSavingCallback(new HandlePageSavingCallback());
doc.save("Your Directory Path" + "PageSavingCallback.png", imageSaveOptions);
Expand All @@ -96,7 +96,7 @@ private static class HandlePageSavingCallback implements IPageSavingCallback {
public void exposeThresholdControlForTiffBinarization() throws Exception
{
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.TIFF);
ImageSaveOptions saveOptions = new ImageSaveOptions();
{
saveOptions.setTiffCompression(TiffCompression.CCITT_3);
saveOptions.setImageColorMode(ImageColorMode.GRAYSCALE);
Expand All @@ -110,7 +110,7 @@ public void getTiffPageRange() throws Exception
{
Document doc = new Document("Your Directory Path" + "Rendering.docx");
doc.save("Your Directory Path" + "WorkingWithImageSaveOptions.MultipageTiff.tiff");
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.TIFF);
ImageSaveOptions saveOptions = new ImageSaveOptions();
{
saveOptions.setPageSet(new PageSet(new PageRange(0, 1))); saveOptions.setTiffCompression(TiffCompression.CCITT_4); saveOptions.setResolution(160f);
}
Expand All @@ -120,7 +120,7 @@ public void getTiffPageRange() throws Exception
public void format1BppIndexed() throws Exception
{
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.PNG);
ImageSaveOptions saveOptions = new ImageSaveOptions();
{
saveOptions.setPageSet(new PageSet(1));
saveOptions.setImageColorMode(ImageColorMode.BLACK_AND_WHITE);
Expand All @@ -132,7 +132,7 @@ public void format1BppIndexed() throws Exception
public void getJpegPageRange() throws Exception
{
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
ImageSaveOptions options = new ImageSaveOptions();
// Set the "PageSet" to "0" to convert only the first page of a document.
options.setPageSet(new PageSet(0));
// Change the image's brightness and contrast.
Expand All @@ -148,7 +148,7 @@ public void getJpegPageRange() throws Exception
public static void pageSavingCallback() throws Exception
{
Document doc = new Document("Your Directory Path" + "Rendering.docx");
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.PNG);
ImageSaveOptions imageSaveOptions = new ImageSaveOptions();
{
imageSaveOptions.setPageSet(new PageSet(new PageRange(0, doc.getPageCount() - 1)));
imageSaveOptions.setPageSavingCallback(new HandlePageSavingCallback());
Expand All @@ -174,7 +174,7 @@ You have learned how to save images from documents using Aspose.Words for Java.
You can change the image format by specifying the desired format in the `ImageSaveOptions`. For example, to save as PNG, use `SaveFormat.PNG` as shown in the code:

```java
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.PNG);
ImageSaveOptions saveOptions = new ImageSaveOptions();
```

### Can I customize the compression settings for TIFF images?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ LoadOptions loadOptions = new LoadOptions();
loadOptions.setConvertShapeToOfficeMath(true);

Document doc = new Document("Your Directory Path" + "Office math.docx", loadOptions);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx");
```

This code demonstrates how to convert shapes to Office Math objects during document loading. The `setConvertShapeToOfficeMath(true)` method enables this conversion.
Expand Down Expand Up @@ -136,7 +136,7 @@ public void convertShapeToOfficeMath() throws Exception {
loadOptions.setConvertShapeToOfficeMath(true);
}
Document doc = new Document("Your Directory Path" + "Office math.docx", loadOptions);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx");
}
@Test
public void setMsWordVersion() throws Exception {
Expand Down
Loading