Skip to content

Commit 2d79426

Browse files
SLIDESDOC-616 Update the article "Exporting Presentations to HTML with Externally Linked Images" (#785)
Updated the title and added keywords and description for SEO purposes. Updated the text and improved comments in the code example (C++, Python via .NET).
1 parent 8a319a7 commit 2d79426

File tree

3 files changed

+110
-68
lines changed
  • en
    • cpp/developer-guide/technical-articles/exporting-presentations-to-html-with-externally-linked-images
    • net/developer-guide/technical-articles/exporting-presentations-to-html-with-externally-linked-images
    • python-net/developer-guide/technical-articles/exporting-presentations-to-html-with-externally-linked-images

3 files changed

+110
-68
lines changed
Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,121 @@
11
---
2-
title: Exporting Presentations to HTML with Externally Linked Images
2+
title: Export Presentations to HTML with Externally Linked Images
33
type: docs
44
weight: 50
55
url: /cpp/exporting-presentations-to-html-with-externally-linked-images/
6+
keywords:
7+
- export PowerPoint
8+
- export OpenDocument
9+
- export presentation
10+
- export slide
11+
- export PPT
12+
- export PPTX
13+
- export ODP
14+
- PowerPoint to HTML
15+
- OpenDocument to HTML
16+
- presentation to HTML
17+
- slide to HTML
18+
- PPT to HTML
19+
- PPTX to HTML
20+
- ODP to HTML
21+
- linked image
22+
- externally linked image
23+
- C++
24+
- Aspose.Slides
25+
description: "Export PowerPoint and OpenDocument presentations to HTML in C++ using Aspose.Slides with externally linked images—faster pages, code examples, and setup tips."
626
---
727

8-
{{% alert color="primary" %}}
28+
{{% alert color="primary" %}}
929

10-
This article describes an advanced technique that allows controlling which resources are embedded into the resulting HTML file and which are saved externally and referenced from the HTML file.
30+
The presentation-to-HTML export process lets you specify:
31+
32+
1. which resources are embedded in the resulting HTML file, and
33+
1. which resources are saved externally and referenced from the HTML file.
34+
35+
{{% /alert %}}
1136

12-
{{% /alert %}}
1337
## **Background**
14-
The default HTML export behavior is to embed any resource into the HTML file. Such approach results in a single HTML file that is easy to view and distribute. All necessary resources are base64-encoded inside. But such approach has two drawbacks:
1538

16-
- The size of output is significantly larger because of the base64 encoding. It is difficult to replace the images contained in the file.
39+
An alternative approach using [ILinkEmbedController](https://reference.aspose.com/slides/cpp/aspose.slides.export/ilinkembedcontroller/) avoids these limitations.
40+
41+
The `LinkController` class below implements [ILinkEmbedController](https://reference.aspose.com/slides/cpp/aspose.slides.export/ilinkembedcontroller/) and is passed to the [HtmlOptions](https://reference.aspose.com/slides/cpp/aspose.slides.export/htmloptions/htmloptions/#htmloptionshtmloptionssystemsharedptrilinkembedcontroller-constructor) constructor. The interface exposes three methods that control how resources are embedded or linked during HTML export:
1742

18-
In this article we will see how we can change the default behavior using the **Aspose.Slides for C++** to link the images externally rather than embedding in the HTML file. We will use the [ILinkEmbedController](https://reference.aspose.com/slides/cpp/class/aspose.slides.export.i_link_embed_controller) interface which contains three methods to control the resource embedding and saving process. We can pass this interface to the[HtmlOptions](https://reference.aspose.com/slides/cpp/class/aspose.slides.export.html_options) class constructor when preparing the export.
43+
[GetObjectStoringLocation(id, entityData, semanticName, contentType, recommendedExtension)](https://reference.aspose.com/slides/cpp/aspose.slides.export/ilinkembedcontroller/getobjectstoringlocation): Called when the exporter encounters a resource and must decide where to store it. The most important parameters are `id` (the resource’s unique identifier for this export run) and `contentType` (the resource MIME type). Return [LinkEmbedDecision.Link](https://reference.aspose.com/slides/cpp/aspose.slides.export/linkembeddecision/) to link the resource, or [LinkEmbedDecision.Embed](https://reference.aspose.com/slides/cpp/aspose.slides.export/linkembeddecision/) to embed it.
1944

20-
Following is the complete code of the **LinkController** class which implements the [ILinkEmbedController](https://reference.aspose.com/slides/cpp/class/aspose.slides.export.i_link_embed_controller) interface. As mentioned before, the **LinkController** must implement the [ILinkEmbedController](https://reference.aspose.com/slides/cpp/class/aspose.slides.export.i_link_embed_controller) interface. This interface specifies three methods:
45+
[GetUrl(id, referrer)](https://reference.aspose.com/slides/cpp/aspose.slides.export/ilinkembedcontroller/geturl/): Returns the URL that will appear in the resulting HTML for the resource identified by `id` (optionally considering the referrer object).
2146

22-
- **LinkEmbedDecision GetObjectStoringLocation(int32_t id, ArrayPtr<uint8_t> entityData, String semanticName, String contentType, String recomendedExtension)** It is called when the exporter encounters a resource and needs to decide how to store it. The most important parameters are ‘id’ – the resource unique identifier for the entire export operation and ‘contentType’ – contains the resource MIME type. If we decide to link the resource we should return LinkEmbedDecision::Link from this method. Otherwise, LinkEmbedDecision::Embed should be returned to embed the resource.
23-
- **String GetUrl(int32_t id, int32_t referrer)**
24-
It is called to get the resource URL in the form how it is used in the resulting file, say for a ```<img src=%method_result_here%>``` tag. The resource is identified by ‘id’.
25-
- **SaveExternal(int32_t id, ArrayPtr<uint8_t> entityData)**
26-
The final method of the sequence, it is called when it comes to storing the resource externally. We have the resource identifier and the resource contents as a byte array. It’s up to us what to do with the provided resource data.
47+
[SaveExternal(id, entityData)](https://reference.aspose.com/slides/cpp/aspose.slides.export/ilinkembedcontroller/saveexternal/): Called when a resource selected for linking needs to be written externally. Because the identifier and contents are provided (as a byte array), you can persist the resource however you like.
2748

28-
``` cpp
29-
/// <summary>
30-
/// This class is responsible for making decisions about the resources saved externally.
31-
/// It must implement the Aspose::Slides::Export::ILinkEmbedController interface.
32-
/// </summary>
49+
The C++ `LinkController` implementation of [ILinkEmbedController](https://reference.aspose.com/slides/cpp/aspose.slides.export/ilinkembedcontroller/) follows below.
50+
51+
```cpp
3352
class LinkController : public ILinkEmbedController
3453
{
3554
public:
55+
// Initializes a new instance of the LinkController class.
3656
LinkController()
3757
{
3858
m_externalImages = System::MakeObject<Dictionary<int32_t, String>>();
3959
}
60+
61+
// Initializes a new instance of the LinkController class and sets the path where generated resource files will be saved.
62+
// savePath - Path to the location where generated resource files will be stored.
4063
LinkController::LinkController(String savePath) : LinkController()
4164
{
4265
m_savePath = savePath;
4366
}
4467

68+
// Determines whether to embed the resource or store it externally.
69+
// id - A unique identifier for each object during the export operation.
4570
LinkEmbedDecision GetObjectStoringLocation(int32_t id, ArrayPtr<uint8_t> entityData,
4671
String semanticName, String contentType, String recomendedExtension) override
4772
{
48-
// Here we make the decision about storing images externally.
49-
// The id is unique identifier of each object during the whole export operation.
50-
5173
String template_;
5274

53-
// The s_templates dictionary contains content types we are going to store externally and the corresponding file name template.
75+
// The s_templates dictionary maps content types to file name templates for resources stored externally.
5476
if (s_templates->TryGetValue(contentType, template_))
5577
{
56-
// Storing this resource to the export list
78+
// Store this resource for external linking.
5779
m_externalImages->Add(id, template_);
5880
return LinkEmbedDecision::Link;
5981
}
6082

61-
// All other resources, if any, will be embedded
83+
// All other resources are embedded.
6284
return LinkEmbedDecision::Embed;
6385
}
6486

87+
// Builds the URL for a previously externalized resource.
88+
// Constructs the resource reference to use in tags such as <img src="%result%">.
89+
// Checks the dictionary to exclude resources that were not externalized.
90+
// Also retrieves the corresponding file name template.
6591
String GetUrl(int32_t id, int32_t referrer) override
6692
{
67-
// Here we construct the resource reference string to form the tag: <img src="%result%">
68-
// We need to check the dictionary to filter out unnecessary resources.
69-
// Along with checking we extract the corresponding file name template.
7093
String template_;
7194
if (m_externalImages->TryGetValue(id, template_))
7295
{
73-
// Assuming we are going to store resource files just near the HTML file.
74-
// The image tag will look like <img src="image-1.png"> with the appropriate resource Id and extension.
96+
// Assumes resource files are stored alongside the HTML file.
97+
// The image tag will look like <img src="image-1.png"> with the appropriate resource ID and extension.
7598
String fileUrl = String::Format(template_, id);
7699
return fileUrl;
77100
}
78101

79-
// null must be returned for the resources remaining embedded
102+
// Return null for resources that remain embedded.
80103
return nullptr;
81104
}
82105

106+
// Saves an externalized resource to disk.
107+
// Checks the dictionary again. If the ID is not found, it indicates an error in GetObjectStoringLocation or GetUrl.
83108
void SaveExternal(int32_t id, ArrayPtr<uint8_t> entityData) override
84109
{
85110
// Here we actually save the resource files to disk.
86111
// Once again, checking the dictionary. If the id is not found here it is a sign of an error in GetObjectStoringLocation or GetUrl methods.
87112
if (m_externalImages->ContainsKey(id))
88113
{
89-
// Now we use the file name stored in the dictionary and combine it with a path as required.
90-
91-
// Constructing the file name using the stored template and the Id.
114+
// Uses the stored file name template and combines it with the target path.
115+
// Constructs the file name using the stored template and the ID.
92116
String fileName = String::Format(m_externalImages->idx_get(id), id);
93117
94-
// Combining with the location directory
118+
// Combines it with the destination directory.
95119
const String savePath = m_savePath != nullptr ? m_savePath : String::Empty;
96120
String filePath = Path::Combine(savePath, fileName);
97121

@@ -100,13 +124,18 @@ public:
100124
}
101125
else
102126
{
103-
throw Exception(u"Something is wrong");
127+
throw Exception(u"Something is wrong.");
104128
}
105129
}
106130

107131
private:
132+
// The path where generated resource files are saved.
108133
String m_savePath;
134+
135+
// A dictionary mapping resource IDs to file name templates.
109136
SharedPtr<Dictionary<int32_t, String>> m_externalImages;
137+
138+
// A dictionary mapping content types of externally stored resources to file name templates.
110139
static SharedPtr<Dictionary<String, String>> s_templates;
111140

112141
static struct __StaticConstructor__
@@ -120,21 +149,21 @@ private:
120149
};
121150
```
122151
123-
After writing the **LinkController** class, now we will use it with [HtmlOptions](https://reference.aspose.com/slides/cpp/class/aspose.slides.export.html_options) class to export the presentation to HTML having externally linked images using the following code.
152+
After implementing the `LinkController` class, you can use it with the [HtmlOptions](https://reference.aspose.com/slides/cpp/aspose.slides.export/htmloptions/htmloptions/) class to export the presentation to HTML with externally linked images, as shown below:
124153
125-
``` cpp
126-
const String templatePath = u"../templates/image.pptx";
127-
auto pres = System::MakeObject<Presentation>(templatePath);
154+
```cpp
155+
auto presentation = MakeObject<Presentation>(u"C:\\data\\input.pptx");
128156
129-
auto htmlOptions = System::MakeObject<HtmlOptions>(System::MakeObject<LinkController>(GetOutPath()));
130-
htmlOptions->set_SlideImageFormat(SlideImageFormat::Svg(System::MakeObject<SVGOptions>()));
131-
// This line is needed to remove the slide title display in HTML.
132-
// Comment it out if your prefer slide title displayed.
157+
auto htmlOptions = MakeObject<HtmlOptions>(MakeObject<LinkController>(u"C:\\data\\out\\"));
158+
htmlOptions->set_SlideImageFormat(SlideImageFormat::Svg(MakeObject<SVGOptions>()));
159+
// This line hides slide titles in the generated HTML.
160+
// Comment it out if you prefer slide titles to be displayed.
133161
htmlOptions->set_HtmlFormatter(HtmlFormatter::CreateDocumentFormatter(String::Empty, false));
134162
135-
pres->Save(GetOutPath() + u"/output.html", SaveFormat::Html, htmlOptions);
163+
presentation->Save(u"C:\\data\\out\\output.html", SaveFormat::Html, htmlOptions);
164+
presentation->Dispose();
136165
```
137166

138-
We pass **SlideImageFormat::Svg** to the **set_SlideImageFormat** method which means the resulting HTML file will contain SVG data inside to draw the presentation contents.
167+
We assigned `SlideImageFormat::Svg` to the `SlideImageFormat` property so that the resulting HTML file will contain SVG data to render the presentation’s contents.
139168

140-
As for the content types, it depends on the actual image data contained in the presentation. If there are raster bitmaps in the presentation then the class code must be ready to process both image/jpeg and image/png content types. The actual content type of the exported raster bitmaps may not match the content type of the images stored in the presentation. The Aspose.Slides for C++ internal algorithms perform size optimization and use either JPG or PNG codec whichever generates smaller data size. Images containing alpha-channel (transparency) are always encoded to PNG.
169+
Content types: If the presentation contains raster bitmaps, then the class code must be prepared to process both `image/jpeg` and `image/png` content types. The content of the exported bitmap images may not match what was stored in the presentation. Aspose.Slidesinternal algorithms perform size optimization and use either the JPEG or PNG codec (depending on which produces a smaller file size). Images containing an alpha channel (transparency) are always encoded as PNG.

en/net/developer-guide/technical-articles/exporting-presentations-to-html-with-externally-linked-images/_index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ keywords:
2020
- ODP to HTML
2121
- linked image
2222
- externally linked image
23-
- Python
23+
- .NET
24+
- C#
2425
- Aspose.Slides
2526
description: "Export PowerPoint and OpenDocument presentations to HTML in .NET using Aspose.Slides with externally linked images—faster pages, code examples, and setup tips."
2627
---
@@ -162,7 +163,7 @@ class LinkController : ILinkEmbedController
162163
}
163164
```
164165

165-
After implementing the `LinkController` class, you can use it with the [HtmlOptions](https://reference.aspose.com/slides/net/aspose.slides.export/htmloptions/htmloptions/#constructor_1) class to export the presentation to HTML with externally linked images, as shown below:
166+
After implementing the `LinkController` class, you can use it with the [HtmlOptions](https://reference.aspose.com/slides/net/aspose.slides.export/htmloptions/htmloptions/) class to export the presentation to HTML with externally linked images, as shown below:
166167

167168
```cs
168169
using (var presentation = new Presentation(@"C:\data\input.pptx"))

0 commit comments

Comments
 (0)