Documentation
¶
Overview ¶
Aspose.PDF for Go via C++
The package asposepdf is a powerful toolkit that allows developers to manipulate PDF files directly and helps do various tasks for PDF.
Features
PDF Processing Main core operation: New, Open, Save, SaveAs, Close, SetLicense, Append, AppendPages, MergeDocuments, SplitDocument, Split, SplitAtPage, SplitAt Other core operation: WordCount, CharacterCount, Bytes Page main core operation: Add, Insert, Delete, Count Page other core operation: WordCount, CharacterCount, IsBlank Organize: Optimize, OptimizeResource, Grayscale, Rotate, SetBackground, Repair, Flatten Page organize: Rotate, SetSize, Grayscale, AddText Remove operation: RemoveAnnotations, RemoveAttachments, RemoveBlankPages, RemoveBookmarks, RemoveHiddenText, RemoveImages, RemoveTables, RemoveJavaScripts Page remove operation: PageRemoveAnnotations, PageRemoveHiddenText, PageRemoveImages, PageRemoveTables Others: Get contents as plain text PDF converting and saving Microsoft Office: DOC, DOCX, XLSX, PPTX, DOCX with Enhanced Recognition Mode (fully editable tables and paragraphs) Images: JPEG, PNG, BMP, TIFF Others: EPUB, DICOM, SVG, SVG(ZIP), XPS, TEX, TXT, MD, N-UP PDF, BOOKLET PDF Export with AcroForm: FDF, XFDF, XML Metadata Product Info: JSON with product name, version, release date, and license status
Platforms
Linux x64, macOS x86_64, macOS arm64 and Windows x64, with using cgo. The platform-specific version of the dynamic library from the 'lib'-folder in the package's root directory is required for distributing the resulting application: libAsposePDFforGo_linux_amd64.so for Linux x64 platform libAsposePDFforGo_darwin_arm64.dylib for macOS arm64 platform libAsposePDFforGo_darwin_amd64.dylib for macOS x86_64 platform AsposePDFforGo_windows_amd64.dll for Windows x64 platform Windows x64 platform requires MinGW-W64 installed.
Installation
This package includes a large file which is stored as a bzip2 archive. Add the asposepdf package to Your Project: go get github.com/aspose-pdf/aspose-pdf-go-cpp@latest Generate the large file on macOS and linux List the folders of the github.com/aspose-pdf within the Go module cache: ls $(go env GOMODCACHE)/github.com/aspose-pdf/ Change curent folder to the specific version folder of the package obtained in the previous step. Replace `@vx.x.x` with the actual package version: cd $(go env GOMODCACHE)/github.com/aspose-pdf/[email protected] Run go generate with superuser privileges: sudo go generate Generate the large file on Windows: List the folders of the github.com/aspose-pdf within the Go module cache: for /f "delims=" %G in ('go env GOMODCACHE') do for /d %a in ("%G\github.com\aspose-pdf\*") do echo %~fa Change curent folder to the specific version folder of the package obtained in the previous step: cd <specific version folder of the package> Run go generate: go generate Add specific version folder of the package to the %PATH% environment variable: setx PATH "%PATH%;<specific version folder of the package>\lib\"
Quick Start
All code snippets are contained in the snippet folder on https://github.com/aspose-pdf/aspose-pdf-go-cpp.
Hello World! example:
package main
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
import "log"
func main() {
// Create new PDF-document
pdf, err := asposepdf.New()
if err != nil {
log.Fatal(err)
}
// Add new page
err = pdf.PageAdd()
if err != nil {
log.Fatal(err)
}
// Set page size A4
err = pdf.PageSetSize(1, asposepdf.PageSizeA4)
if err != nil {
log.Fatal(err)
}
// Add text on first page
err = pdf.PageAddText(1, "Hello World!")
if err != nil {
log.Fatal(err)
}
// Save PDF-document with "hello.pdf" name
err = pdf.SaveAs("hello.pdf")
if err != nil {
log.Fatal(err)
}
// Release allocated resources
defer pdf.Close()
}
Testing
The test run from the root package folder: go test -v
License
The Go source code is licensed under the MIT License.
The shared native libraries (DLL, SO, DYLIB) are proprietary and require a commercial license.
Evaluation version limitations:
Documents created with an evaluation watermark.
Limit on number of pages processed (first 4 pages).
For production use, a commercial license is required.
Resources
Aspose home: https://www.aspose.com Product Page: https://products.aspose.com/pdf/go-cpp/ Docs: https://docs.aspose.com/pdf/go-cpp/ Demos: https://products.aspose.app/pdf/family API Reference: https://reference.aspose.com/pdf/go-cpp/ Examples: https://github.com/aspose-pdf/aspose-pdf-go-cpp/ Blog: https://blog.aspose.com/category/pdf/ Free Support: https://forum.aspose.com/c/pdf Temporary License: https://purchase.aspose.com/temporary-license GitHub.com: https://github.com/aspose-pdf/aspose-pdf-go-cpp pkg.go.dev: https://pkg.go.dev/github.com/aspose-pdf/aspose-pdf-go-cpp
Index ¶
- Constants
- func SplitAtPage(document *Document, page int) (*Document, *Document, error)
- type Document
- func (document *Document) About() (*ProductInfo, error)
- func (document *Document) AddPageNum() error
- func (document *Document) AddTextFooter(footer string) error
- func (document *Document) AddTextHeader(header string) error
- func (document *Document) Append(anotherdocument *Document) error
- func (document *Document) AppendPages(anotherdocument *Document, pagerange string) error
- func (document *Document) Bytes() ([]byte, error)
- func (document *Document) CharacterCount() (int32, error)
- func (document *Document) Close() error
- func (document *Document) ExportFdf(filename string) error
- func (document *Document) ExportXfdf(filename string) error
- func (document *Document) ExportXml(filename string) error
- func (document *Document) ExtractText() (string, error)
- func (document *Document) Flatten() error
- func (document *Document) Grayscale() error
- func (document *Document) Optimize() error
- func (document *Document) OptimizeResource() error
- func (document *Document) PageAdd() error
- func (document *Document) PageAddPageNum(num int32) error
- func (document *Document) PageAddText(num int32, addText string) error
- func (document *Document) PageAddTextFooter(num int32, footer string) error
- func (document *Document) PageAddTextHeader(num int32, header string) error
- func (document *Document) PageCharacterCount(num int32) (int32, error)
- func (document *Document) PageCount() (int32, error)
- func (document *Document) PageDelete(num int32) error
- func (document *Document) PageGrayscale(num int32) error
- func (document *Document) PageInsert(num int32) error
- func (document *Document) PageIsBlank(num int32) (bool, error)
- func (document *Document) PageRemoveAnnotations(num int32) error
- func (document *Document) PageRemoveHiddenText(num int32) error
- func (document *Document) PageRemoveImages(num int32) error
- func (document *Document) PageRemoveTables(num int32) error
- func (document *Document) PageReplaceText(num int32, findText, replaceText string) error
- func (document *Document) PageRotate(num int32, rotation int32) error
- func (document *Document) PageSetSize(num int32, pageSize int32) error
- func (document *Document) PageToBmp(num int32, resolution_dpi int32, filename string) error
- func (document *Document) PageToDICOM(num int32, resolution_dpi int32, filename string) error
- func (document *Document) PageToJpg(num int32, resolution_dpi int32, filename string) error
- func (document *Document) PageToPdf(num int32, filename string) error
- func (document *Document) PageToPng(num int32, resolution_dpi int32, filename string) error
- func (document *Document) PageToSvg(num int32, filename string) error
- func (document *Document) PageToTiff(num int32, resolution_dpi int32, filename string) error
- func (document *Document) PageWordCount(num int32) (int32, error)
- func (document *Document) RemoveAnnotations() error
- func (document *Document) RemoveAttachments() error
- func (document *Document) RemoveBlankPages() error
- func (document *Document) RemoveBookmarks() error
- func (document *Document) RemoveHiddenText() error
- func (document *Document) RemoveImages() error
- func (document *Document) RemoveJavaScripts() error
- func (document *Document) RemoveTables() error
- func (document *Document) Repair() error
- func (document *Document) ReplaceText(findText, replaceText string) error
- func (document *Document) Rotate(rotation int32) error
- func (document *Document) Save() error
- func (document *Document) SaveAs(filename string) error
- func (document *Document) SaveBooklet(filename string) error
- func (document *Document) SaveDoc(filename string) error
- func (document *Document) SaveDocX(filename string) error
- func (document *Document) SaveDocXEnhanced(filename string) error
- func (document *Document) SaveEpub(filename string) error
- func (document *Document) SaveMarkdown(filename string) error
- func (document *Document) SaveNUp(filename string, columns int32, rows int32) error
- func (document *Document) SavePptX(filename string) error
- func (document *Document) SaveSvgZip(filename string) error
- func (document *Document) SaveTeX(filename string) error
- func (document *Document) SaveTiff(filename string, resolution_dpi ...int32) error
- func (document *Document) SaveTxt(filename string) error
- func (document *Document) SaveXlsX(filename string) error
- func (document *Document) SaveXps(filename string) error
- func (document *Document) SetBackground(r, g, b int32) error
- func (document *Document) SetLicense(filename string) error
- func (document *Document) Split(pagerange string) ([]*Document, error)
- func (document *Document) SplitAt(page int) (*Document, *Document, error)
- func (document *Document) WordCount() (int32, error)
- type ProductInfo
Constants ¶
const ( RotationNone int32 = 0 // Non-rotated. RotationOn90 int32 = 1 // Rotated on 90 degrees clockwise. RotationOn180 int32 = 2 // Rotated on 180 degrees. RotationOn270 int32 = 3 // Rotated on 270 degrees clockwise. RotationOn360 int32 = 4 // Rotated on 360 degrees clockwise. )
Enumeration of possible rotation values.
const ( PageSizeA0 int32 = 0 // A0 size. PageSizeA1 int32 = 1 // A1 size. PageSizeA2 int32 = 2 // A2 size. PageSizeA3 int32 = 3 // A3 size. PageSizeA4 int32 = 4 // A4 size. PageSizeA5 int32 = 5 // A5 size. PageSizeA6 int32 = 6 // A6 size. PageSizeB5 int32 = 7 // B5 size. PageSizePageLetter int32 = 8 // PageLetter size. PageSizePageLegal int32 = 9 // PageLegal size. PageSizePageLedger int32 = 10 // PageLedger size. PageSizeP11x17 int32 = 11 // P11x17 size. )
Enumeration of possible page size values.
const (
ERR_OK = ""
)
Meaning no error.
Variables ¶
This section is empty.
Functions ¶
func SplitAtPage ¶ added in v1.25.5
SplitAtPage splits the PDF-document into two new PDF-documents. The first document includes pages 1 to 'page' (inclusive). The second document includes pages from 'page+1' to the end.
Example:
left, right, err := SplitAtPage(source, 3) // 'left' contains pages 1-3, 'right' contains pages 4 to end
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents a PDF-document.
func MergeDocuments ¶ added in v1.25.5
MergeDocuments creates a new PDF-document by merging the provided documents.
Example:
pdf_merged, err := MergeDocuments([]*Document{pdf1, pdf2})
if err != nil {
fmt.Errorf("MergeDocuments(): %v", err)
} else {
// working with new merged PDF-document
}
func New ¶
New creates a new PDF-document.
Example:
pdf_new, err := New()
if err != nil {
fmt.Errorf("New(): %v", err)
} else {
// working with new PDF-document
}
func Open ¶
Open opens a PDF-document with filename.
Example:
pdf, err := Open("example.pdf")
if err != nil {
fmt.Errorf("Open(): %v", err)
} else {
// working with open PDF-document
}
func SplitDocument ¶ added in v1.25.5
SplitDocument creates multiple new PDF-documents by extracting pages from the source PDF-document.
Each part of the pagerange string (separated by `;`) defines the page range for a new PDF-document.
Example:
pdfs, err := asposepdf.SplitDocument(pdf_source, "1-2;3;4-") // pdfs[0] will contain pages 1-2, pdfs[1] page 3, pdfs[2] pages 4 to end.
func (*Document) About ¶ added in v1.25.9
func (document *Document) About() (*ProductInfo, error)
About returns metadata information about the Aspose.PDF for Go via C++.
The metadata is returned as a ProductInfo struct, deserialized from a JSON string. It includes product name, version, release date, licensing status, and related details.
See also: product_info.go
Example:
info, err := pdf.About()
func (*Document) AddPageNum ¶
AddPageNum adds page number to a PDF-document.
Example:
err := pdf.AddPageNum()
func (*Document) AddTextFooter ¶
AddTextFooter adds text in Footer of a PDF-document.
Example:
err := pdf.AddTextFooter("Footer")
func (*Document) AddTextHeader ¶
AddTextHeader adds text in Header of a PDF-document.
Example:
err := pdf.AddTextHeader("Aspose")
func (*Document) Append ¶ added in v1.25.4
Append appends pages from another PDF-document.
Example:
err := pdf.Append(anotherdoc)
func (*Document) AppendPages ¶ added in v1.25.5
AppendPages appends selected pages from another PDF-document.
Example:
err := pdf.AppendPages(anotherdoc, "-2,4,6-8,10-")
func (*Document) Bytes ¶ added in v1.25.6
Bytes returns the contents of the PDF-document as a byte slice.
Example:
bytes, err := pdf.Bytes()
func (*Document) CharacterCount ¶
CharacterCount returns character count in PDF-document.
Example:
character_count, err := pdf.CharacterCount()
func (*Document) Close ¶
Close releases allocated resources for PDF-document.
Example:
defer pdf.Close()
func (*Document) ExportFdf ¶
ExportFdf exports from previously opened PDF-document with AcroForm to FDF-document with filename.
Example:
err := pdf.ExportFdf("filename.fdf")
func (*Document) ExportXfdf ¶
ExportXfdf exports from previously opened PDF-document with AcroForm to XFDF-document with filename.
Example:
err := pdf.ExportXfdf("filename.xfdf")
func (*Document) ExportXml ¶
ExportXml exports from previously opened PDF-document with AcroForm to XML-document with filename.
Example:
err := pdf.ExportXml("filename.xml")
func (*Document) ExtractText ¶
ExtractText returns PDF-document contents as plain text.
Example:
txt, err := pdf.ExtractText()
func (*Document) Grayscale ¶
Grayscale converts PDF-document to black and white.
Example:
err := pdf.Grayscale()
func (*Document) OptimizeResource ¶
OptimizeResource optimizes resources of PDF-document.
Example:
err := pdf.OptimizeResource()
func (*Document) PageAddPageNum ¶
PageAddPageNum adds page number on page.
Example:
err := pdf.PageAddPageNum(1)
func (*Document) PageAddText ¶
PageAddText adds text on page.
Example:
err := pdf.PageAddText(1, "text on the first page")
func (*Document) PageAddTextFooter ¶
PageAddTextFooter adds text in page footer
Example:
err := pdf.PageAddTextFooter("Footer")
func (*Document) PageAddTextHeader ¶
PageAddTextHeader adds text in page header
Example:
err := pdf.PageAddTextHeader(1, "Aspose")
func (*Document) PageCharacterCount ¶
PageCharacterCount returns character count on specified page in PDF-document.
Example:
character_count, err := pdf.PageCharacterCount(1)
func (*Document) PageCount ¶
PageCount returns page count in PDF-document.
Example:
page_count, err := pdf.PageCount()
func (*Document) PageDelete ¶
PageDelete deletes specified page in PDF-document.
Example:
err := pdf.PageDelete(1)
func (*Document) PageGrayscale ¶
PageGrayscale converts page to black and white.
Example:
err := pdf.PageGrayscale(1)
func (*Document) PageInsert ¶
PageInsert inserts new page at the specified position in PDF-document.
Example:
err := pdf.PageInsert(1)
func (*Document) PageIsBlank ¶
PageIsBlank returns page is blank in PDF-document.
Example:
is_blank, err := pdf.PageIsBlank(1)
func (*Document) PageRemoveAnnotations ¶ added in v1.25.4
PageRemoveAnnotations removes annotations in page.
Example:
err := pdf.PageRemoveAnnotations(1)
func (*Document) PageRemoveHiddenText ¶ added in v1.25.4
PageRemoveHiddenText removes hidden text in page.
Example:
err := pdf.PageRemoveHiddenText(1)
func (*Document) PageRemoveImages ¶ added in v1.25.4
PageRemoveImages removes images in page.
Example:
err := pdf.PageRemoveImages(1)
func (*Document) PageRemoveTables ¶ added in v1.25.10
PageRemoveTables removes tables in page.
Example:
err := pdf.PageRemoveTables(1)
func (*Document) PageReplaceText ¶
PageReplaceText replaces text on page.
Example:
err := pdf.PageReplaceText(1, "old text", "new text")
func (*Document) PageRotate ¶
PageRotate rotates page.
Example:
err := pdf.PageRotate(1, asposepdf.RotationOn180)
func (*Document) PageSetSize ¶
PageSetSize sets size of page.
Example:
err := pdf.PageSetSize(1, asposepdf.PageSizeA4)
func (*Document) PageToBmp ¶
PageToBmp saves the specified page as Bmp-image file.
Example:
err := pdf.PageToBmp(1, 100, "page_num_1_with_100_dpi.bmp")
func (*Document) PageToDICOM ¶
PageToDICOM saves the specified page as DICOM-image file.
Example:
err := pdf.PageToDICOM(1, 100, "page_num_1_with_100_dpi.dcm")
func (*Document) PageToJpg ¶
PageToJpg saves the specified page as Jpg-image file.
Example:
err := pdf.PageToJpg(1, 300, "page_num_1_with_300_dpi.jpg")
func (*Document) PageToPdf ¶
PageToPdf saves the specified page as Pdf-file.
Example:
err := pdf.PageToPdf(1, "page_num_1.pdf")
func (*Document) PageToPng ¶
PageToPng saves the specified page as Png-image file.
Example:
err := pdf.PageToPng(1, 100, "page_num_1_with_100_dpi.png")
func (*Document) PageToSvg ¶
PageToSvg saves the specified page as Svg-image file.
Example:
err := pdf.PageToSvg(1, "page_num_1.svg")
func (*Document) PageToTiff ¶
PageToTiff saves the specified page as Tiff-image file.
Example:
err := pdf.PageToTiff(1, 100, "page_num_1_with_100_dpi.tiff")
func (*Document) PageWordCount ¶
PageWordCount returns word count on specified page in PDF-document.
Example:
word_count, err := pdf.PageWordCount(1)
func (*Document) RemoveAnnotations ¶ added in v1.25.4
RemoveAnnotations removes annotations from PDF-document.
Example:
err := pdf.RemoveAnnotations()
func (*Document) RemoveAttachments ¶ added in v1.25.4
RemoveAttachments removes attachments from PDF-document.
Example:
err := pdf.RemoveAttachments()
func (*Document) RemoveBlankPages ¶ added in v1.25.4
RemoveBlankPages removes blank pages from PDF-document.
Example:
err := pdf.RemoveBlankPages()
func (*Document) RemoveBookmarks ¶ added in v1.25.4
RemoveBookmarks removes bookmarks from PDF-document.
Example:
err := pdf.RemoveBookmarks()
func (*Document) RemoveHiddenText ¶ added in v1.25.4
RemoveHiddenText removes hidden text from PDF-document.
Example:
err := pdf.RemoveHiddenText()
func (*Document) RemoveImages ¶ added in v1.25.4
RemoveImages removes images from PDF-document.
Example:
err := pdf.RemoveImages()
func (*Document) RemoveJavaScripts ¶ added in v1.25.4
RemoveJavaScripts removes java scripts from PDF-document.
Example:
err := pdf.RemoveJavaScripts()
func (*Document) RemoveTables ¶ added in v1.25.10
RemoveTables removes tables from PDF-document.
Example:
err := pdf.RemoveTables()
func (*Document) ReplaceText ¶
ReplaceText replaces text in PDF-document.
Example:
err := pdf.ReplaceText("old text", "new text")
func (*Document) Rotate ¶
Rotate rotates PDF-document.
Example:
err := pdf.Rotate(asposepdf.RotationOn180)
func (*Document) SaveAs ¶
SaveAs saves previously opened PDF-document with new filename.
Example:
err := pdf.SaveAs("new_filename.pdf")
func (*Document) SaveBooklet ¶
SaveBooklet saves previously opened PDF-document as booklet PDF-document with filename.
Example:
err := pdf.SaveBooklet("filename.pdf")
func (*Document) SaveDoc ¶
SaveDoc saves previously opened PDF-document as Doc-document with filename.
Example:
err := pdf.SaveDoc("filename.doc")
func (*Document) SaveDocX ¶
SaveDocX saves previously opened PDF-document as DocX-document with filename.
Example:
err := pdf.SaveDocX("filename.docx")
func (*Document) SaveDocXEnhanced ¶ added in v1.25.8
SaveDocXEnhanced saves previously opened PDF-document as Enhanced Recognition Mode DocX-document with filename.
Example:
err := pdf.SaveDocXEnhanced("filename.docx")
func (*Document) SaveEpub ¶
SaveEpub saves previously opened PDF-document as Epub-document with filename.
Example:
err := pdf.SaveEpub("filename.epub")
func (*Document) SaveMarkdown ¶
SaveMarkdown saves previously opened PDF-document as Markdown-document with filename.
Example:
err := pdf.SaveMarkdown("filename.md")
func (*Document) SaveNUp ¶
SaveNUp saves previously opened PDF-document as N-Up PDF-document with filename.
Example:
err := pdf.SaveNUp("filename.pdf", 2, 2)
func (*Document) SavePptX ¶
SavePptX saves previously opened PDF-document as PptX-document with filename.
Example:
err := pdf.SavePptX("filename.pptx")
func (*Document) SaveSvgZip ¶ added in v1.25.9
SaveSvgZip saves previously opened PDF-document as SVG-archive with filename.
Example:
err := pdf.SaveSvgZip("filename.zip")
func (*Document) SaveTeX ¶
SaveTeX saves previously opened PDF-document as TeX-document with filename.
Example:
err := pdf.SaveTeX("filename.tex")
func (*Document) SaveTiff ¶ added in v1.25.7
SaveTiff saves previously opened PDF-document as Tiff-document with filename.
Example:
err := pdf.SaveTiff("filename.tiff")
func (*Document) SaveTxt ¶
SaveTxt saves previously opened PDF-document as Txt-document with filename.
Example:
err := pdf.SaveTxt("filename.txt")
func (*Document) SaveXlsX ¶
SaveXlsX saves previously opened PDF-document as XlsX-document with filename.
Example:
err := pdf.SaveXlsX("filename.xlsx")
func (*Document) SaveXps ¶
SaveXps saves previously opened PDF-document as Xps-document with filename.
Example:
err := pdf.SaveXps("filename.xps")
func (*Document) SetBackground ¶
SetBackground sets PDF-document background color.
Example:
err := pdf.SetBackground(200, 100, 101)
func (*Document) SetLicense ¶
SetLicense licenses with filename.
Example:
err := pdf.SetLicense("Aspose.PDF.Go.lic")
func (*Document) Split ¶ added in v1.25.10
Split creates multiple new PDF-documents by extracting pages from the current PDF-document.
Each part of the pagerange string (separated by `;`) defines the page range for a new PDF-document.
Example:
pdfs, err := pdf.Split("1-2;3;4-")
// pdfs[0] will contain pages 1-2, pdfs[1] page 3, pdfs[2] pages 4 to end.
func (*Document) SplitAt ¶ added in v1.25.10
SplitAt splits the current PDF-document into two new PDF-documents. The first document includes pages 1 to 'page' (inclusive). The second document includes pages from 'page+1' to the end.
Example:
left, right, err := SplitAtPage(source, 3) // 'left' contains pages 1-3, 'right' contains pages 4 to end
type ProductInfo ¶ added in v1.25.9
type ProductInfo struct {
Product string `json:"product"` // Name
Family string `json:"family"` // Family (e.g., "Aspose.PDF")
Version string `json:"version"` // Version
ReleaseDate string `json:"releasedate"` // Release date in ISO format (YYYY-MM-DD)
Producer string `json:"producer"` // Producer
IsLicensed bool `json:"islicensed"` // License status (true if licensed)
}
ProductInfo contains metadata about the Aspose.PDF for Go via C++.