Introduction
Aspose.PDF FOSS for Go now exports PDF documents to HTML, exposes optional content layers on the page tree, and provides a facade for authoring Tagged PDF (accessible) documents. These additions extend the Document, Page, and Form types with document-management capabilities that go beyond basic open/save workflows: turning a PDF into a self-contained HTML page, toggling the visibility of layered content, marking up the logical structure for PDF/UA conformance, and exchanging AcroForm field data as JSON.
The library is a Go module, github.com/aspose-pdf-foss/aspose-pdf-foss-for-go, released under the MIT license with no native C or C++ dependencies. Install it with go get github.com/aspose-pdf-foss/aspose-pdf-foss-for-go.
This post walks through the new HTML export modes, optional content (layer) handling, tagged-content authoring, and form JSON export/import — each grounded in the current API surface.
Key Features
Exporting PDF to HTML
Document.SaveHTML and Document.WriteHTML convert a loaded PDF to HTML, controlled by HTMLSaveOptions.Mode (HTMLModeText for a visible, selectable text layer over a glyph-less raster background; HTMLModeNative for an inline-SVG vector layer; HTMLModeFlow for reflowable HTML). Interactive AcroForm fields can be converted into real HTML form controls with InteractiveForms: true.
doc, _ := pdf.Open("input.pdf")
// Faithful mode (default): full page raster + transparent selectable text layer.
doc.SaveHTML("out.html")
// Visible-text mode: real styled HTML text over a glyph-less background raster.
doc.SaveHTML("out.html", pdf.HTMLSaveOptions{Mode: pdf.HTMLModeText})
// Native mode: page graphics as one inline SVG layer.
doc.SaveHTML("out.html", pdf.HTMLSaveOptions{Mode: pdf.HTMLModeNative})
// Flow mode: reflowable HTML, paragraphs and headings in reading order.
doc.SaveHTML("article.html", pdf.HTMLSaveOptions{Mode: pdf.HTMLModeFlow})
// Interactive forms become fillable HTML controls.
doc.SaveHTML("form.html", pdf.HTMLSaveOptions{Mode: pdf.HTMLModeText, InteractiveForms: true})
Optional Content Layers
Document.AddLayer creates a new Optional Content Group (OCG) and returns a *Layer; Document.Layers lists every layer already in the document. A page marks content as belonging to a layer with Page.BeginLayer / Page.EndLayer. Layer.SetVisible(false) hides the layer’s content by default.
doc := pdf.NewDocumentFromFormat(pdf.PageFormatA4)
layer := doc.AddLayer("Watermark")
page, _ := doc.Page(1)
page.BeginLayer(layer)
page.AddText("DRAFT", pdf.TextStyle{Font: pdf.FontHelveticaBold, Size: 48},
pdf.Rectangle{LLX: 150, LLY: 400, URX: 450, URY: 460})
page.EndLayer()
layer.SetVisible(false) // hide the watermark by default
doc.Save("layered.pdf")
Tagged PDF and Accessibility
Document.TaggedContent returns the *TaggedContent facade that owns the document’s logical structure tree. TaggedContent.SetTitle / SetLanguage set the catalog-level metadata PDF/UA requires, and Page.TagContent brackets a drawing call in marked content while adding a *StructElement node.
doc := pdf.NewDocumentFromFormat(pdf.PageFormatA4)
tc := doc.TaggedContent()
tc.SetTitle("Quarterly Report")
tc.SetLanguage("en-US")
page, _ := doc.Page(1)
page.TagContent(tc.Root(), pdf.StructH1, func() error {
return page.AddText("Quarterly Report", pdf.TextStyle{Font: pdf.FontHelveticaBold, Size: 24},
pdf.Rectangle{LLX: 50, LLY: 760, URX: 545, URY: 800})
})
fig, _ := page.TagContent(tc.Root(), pdf.StructFigure, func() error {
return page.AddImage("chart.png", pdf.Rectangle{LLX: 50, LLY: 540, URX: 300, URY: 680})
})
fig.SetAlt("Bar chart of Q3 sales by region") // required for figures
doc.Save("accessible.pdf") // doc.ValidatePDFUA() is now Conformant
Exporting Form Data as JSON
Form.ExportJSON serializes every AcroForm field’s type and value to JSON; Form.ImportJSON applies a JSON payload back onto a form and returns the number of fields it updated. JSONExportOptions.Indent pretty-prints the output.
doc, _ := pdf.Open("filled.pdf")
data, _ := doc.Form().ExportJSON(pdf.JSONExportOptions{Indent: true})
// Fill a template from a JSON payload; discard the applied-field count.
template, _ := pdf.Open("template.pdf")
_, _ = template.Form().ImportJSON(data)
Quick Start
go get github.com/aspose-pdf-foss/aspose-pdf-foss-for-go
package main
import (
"fmt"
pdf "github.com/aspose-pdf-foss/aspose-pdf-foss-for-go"
)
func main() {
doc, err := pdf.Open("input.pdf")
if err != nil {
panic(err)
}
err = doc.SaveHTML("out.html", pdf.HTMLSaveOptions{Mode: pdf.HTMLModeText})
if err != nil {
panic(err)
}
fmt.Println("HTML export complete")
}
Supported Formats
| Format | Extension | Read | Write |
|---|---|---|---|
| Yes | - | ||
| HTML | html | - | Yes |
| JSON | json | - | Yes |
Open Source & Licensing
Aspose.PDF FOSS for Go is released under the MIT license: no usage restrictions, no runtime fees, and no registration requirements for commercial or personal use. Source code is hosted at github.com/aspose-pdf-foss/aspose-pdf-foss-for-go.