ts-visio - v1.16.27
    Preparing search index...

    Class VisioDocument

    The root object for reading and writing Visio (.vsdx) files.

    Create a blank document with VisioDocument.create or load an existing file with VisioDocument.load. Call VisioDocument.save when done.

    import { VisioDocument } from 'ts-visio';

    const doc = await VisioDocument.create();
    const page = doc.pages[0];
    await page.addShape({ text: 'Hello, Visio!', x: 1, y: 1, width: 3, height: 1 });
    await doc.save('output.vsdx');
    Index

    Accessors

    Methods

    • Add a background page to the document

      Parameters

      • name: string

      Returns Promise<Page>

    • Add a color to the document-level color palette and return its integer index (IX).

      If the color is already registered the existing index is returned without creating a duplicate. The two built-in colors are always present:

      • index 0 → #000000 (black)
      • index 1 → #FFFFFF (white)

      User colors receive indices starting at 2.

      Parameters

      • hex: string

        CSS hex string — '#4472C4', '#ABC', or '4472c4' are all accepted.

      Returns number

      Integer IX that uniquely identifies this color in the palette.

      const blueIx = doc.addColor('#4472C4');  // → 2
      const redIx = doc.addColor('#FF0000'); // → 3
      doc.addColor('#4472C4'); // → 2 (de-duplicated)
    • Define a new master shape in the document and return its record. Creates all necessary OPC infrastructure (masters.xml, content-type overrides, and document-level relationships) on the first call.

      Parameters

      • name: string

        Display name shown in the stencil panel.

      • geometry: ShapeGeometry = 'rectangle'

        Visual outline; defaults to 'rectangle'.

      Returns MasterRecord

      const boxMaster = doc.createMaster('Box');
      const ellMaster = doc.createMaster('Process', 'ellipse');
      await page.addShape({ text: 'Start', x: 1, y: 1, width: 2, height: 1, masterId: ellMaster.id });
    • Create a named document-level stylesheet and return its record. The returned id can be passed to addShape({ styleId }) or shape.applyStyle().

      Parameters

      Returns StyleRecord

      const s = doc.createStyle('Header', { fillColor: '#4472C4', fontColor: '#ffffff', bold: true });
      const shape = await page.addShape({ text: 'Title', x: 1, y: 1, width: 3, height: 1, styleId: s.id });
    • Delete a page from the document. Removes the page XML, its relationships, the Content Types entry, and any BackPage references from other pages.

      Parameters

      Returns Promise<void>

    • Duplicate a page and return the new Page object. The duplicate is inserted directly after the source page in the tab order. If newName is omitted, "<original name> (Copy)" is used.

      Parameters

      • page: Page
      • OptionalnewName: string

      Returns Promise<Page>

      const copy = await doc.duplicatePage(page, 'Page 2');
      
    • Look up the palette index of a color by its hex value. Returns undefined if the color has not been added to the palette.

      Parameters

      • hex: string

      Returns number | undefined

      doc.addColor('#4472C4');
      doc.getColorIndex('#4472C4'); // → 2
      doc.getColorIndex('#FF0000'); // → undefined
    • Return all color entries in the document palette, ordered by index.

      Returns ColorEntry[]

      doc.addColor('#4472C4');
      doc.getColors();
      // → [{ index: 0, rgb: '#000000' }, { index: 1, rgb: '#FFFFFF' }, { index: 2, rgb: '#4472C4' }]
    • Return all master shapes currently defined in the document. Each record's id can be passed as masterId to page.addShape().

      Returns MasterRecord[]

      const masters = doc.getMasters();
      const box = masters.find(m => m.name === 'Box')!;
      await page.addShape({ text: '', x: 2, y: 3, width: 1, height: 1, masterId: box.id });
    • Find a page by name. Returns undefined if no page with that name exists.

      Parameters

      • name: string

      Returns Page | undefined

    • Import all masters from a .vssx stencil file into this document. Each master is assigned a fresh ID that does not conflict with any master already present. Returns the array of imported master records.

      Parameters

      • pathOrBuffer: string | ArrayBuffer | Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike>

        Filesystem path or raw buffer of the .vssx file.

      Returns Promise<MasterRecord[]>

      const masters = await doc.importMastersFromStencil('./Basic_Shapes.vssx');
      const arrowMaster = masters.find(m => m.name === 'Arrow');
      await page.addShape({ text: '', x: 3, y: 4, width: 1, height: 0.5, masterId: arrowMaster!.id });
    • Move a page to a new 0-based position in the tab order. Clamps toIndex to the valid range automatically.

      Parameters

      • page: Page
      • toIndex: number

      Returns void

      doc.movePage(page, 0);  // move to first position
      
    • Rename a page. Updates page.name in-memory as well as the pages.xml record.

      Parameters

      • page: Page
      • newName: string

      Returns void

      doc.renamePage(page, 'Architecture Overview');
      
    • Parameters

      • Optionalfilename: string

      Returns Promise<Buffer<ArrayBufferLike>>

    • Set a background page for a foreground page

      Parameters

      • foregroundPage: Page
      • backgroundPage: Page

      Returns Promise<void>

    • Write document metadata. Only the supplied fields are changed; all other fields keep their existing values.

      Parameters

      Returns void

      doc.setMetadata({ title: 'My Diagram', author: 'Alice', company: 'ACME' });
      
    • Parameters

      • pathOrBuffer: string | ArrayBuffer | Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike>

      Returns Promise<VisioDocument>