Steps for duplicating and resizing a drawing canvas in Microsoft Word

We are faced with a challenge in replicating a specific Word functionality using Office-JS and are seeking guidance on its feasibility.

Our scenario involves two drawing canvases; one larger canvas filled with various images and shapes, and another smaller empty canvas.

In Word, we can easily select all content within the large canvas, copy it, and paste it into the smaller canvas where it automatically resizes to fit. However, our attempts to automate this process using the Office-JS API have not yielded the desired outcome.

After extracting the OOXML from the original canvas and inserting it into the new canvas using context.document.body.insertOoxml, the contents are pasted but do not resize accordingly to fit the smaller canvas.

Is there an alternative method we could explore to achieve this resizing functionality?

One approach we tried involved looping through each element in the canvas to manually adjust their sizes, but the changes were not reflected as expected:

// Retrieve all paragraphs 
let xmlContent;
xmlContent = doc.body.getOoxml();
await context.sync();

const parser = new DOMParser();
let xml: Document = parser.parseFromString(xmlContent.value, 'application/xhtml+xml');
const paragraphs = xml.getElementsByTagName('w:p');

for (let i = 0; i <= paragraphs.length - 1; i++) {
    if (paragraphs[i].getElementsByTagName('w:sdtPr').length > 0
        && paragraphs[i].getElementsByTagName('w:sdtPr')[0].getElementsByTagName('w:id')[0].getAttributeNode("w:val").value === '1234306') {
            targetCanvasParagraph = paragraphs[i + 1];
        }
    let drawings = targetCanvasParagraph.getElementsByTagName('w:drawing');
    let factor = 1.724156661532341;
    for (let i = 0; i < drawings.length; i++) {
        let positionH = drawings.item(i).getElementsByTagName('wp:positionH')[0];
        if (positionH) {
            let posOffsetH = positionH.getElementsByTagName('wp:posOffset')[0];
            posOffsetH.textContent = (Math.trunc(Number(posOffsetH.textContent) / factor)).toString();
        }

        let positionV = drawings.item(i).getElementsByTagName('wp:positionV')[0];
        if(positionV){
            let posOffsetV = positionV.getElementsByTagName('wp:posOffset')[0];
            posOffsetV.textContent = (Math.trunc(Number(posOffsetV.textContent) / factor)).toString();
        }
    }
}

Answer №1

You won't find a straightforward method for accomplishing this task. Perhaps you could experiment with retrieving and adjusting the canvas size instead of focusing on individual elements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to TypeScript, RxJS, web development, and NodeJS: A comprehensive manual

Looking for recommendations on advanced web development books that focus on modern techniques. Digital resources are great, but there's something special about reading from a physical book. I don't need basic intros or overviews - consider me an ...

Troubleshooting a 400 error when trying to authenticate with Firebase using AngularFire2

Authentication through Google has been enabled in the console: https://i.sstatic.net/Kkdh3.png Here is the login code: import { Component, OnInit } from '@angular/core'; import { AngularFire, AuthProviders } from 'angularfire2'; @Com ...

Routing in Angular 2 is functioning properly when running the ng serve command, however, there appears to

Currently in the process of developing an Angular 2 application, but running into some difficulties with routing. It seems to work fine when using 'ng serve', but not when using 'nodemon'. I've tried various methods based on my res ...

What is the method for retrieving a page's title in TypeScript?

Can someone help me with how to access the page title and save it to a variable using TypeScript in an Angular project? import { Component, OnInit } from '@angular/core'; @Component({ selector: 'home', templateUrl: './hom ...

The Array instance does not reflect changes made by the HTTP service request

Currently, I am tackling a situation where I must retrieve a series of objects from Spring Boot by making REST calls and then display them in the user interface using Angular 5. This is what my component looks like: export class BusinessComponent impleme ...

The issue with hiding the Angular PrimeNG megamenu despite setting the visibility to false

Need help with Angular PrimeNG megamenu visibility issue. <p-megaMenu [model]="menuItems"></p-megaMenu> this.menuItems = [ { label: 'Home', items: null, routerLink: [''] }, ...

Creating a new array for an Angular 5 class instance

My goal is to create and initialize an array of a class in Angular 5. export class B{ public Colors: string; public Temp: number; public Numbers: number; } export class A{ public MyTable: B[]; public othervar: number; ... ... } Next, I ...

Has React With TypeScript Retained Its Value With the Introduction of React With Hooks?

Our team is diving into a new React project, coming from a long history of working with .Net at Microsoft. We have been pleasantly surprised by React and after thorough research, we have decided to go down the path of using it. Being C# developers, our pl ...

Converting an array of type T to an array of its return type T through typing

I'm having trouble typing a callback function in TypeScript to map a parameter result. The following function is causing me some difficulty: type CustomRouteType<TParam = any> = <T>(value?: T) => TParam; const createRoute = <TTypes ...

Tips for adjusting the time format within Ionic 3 using TypeScript

I currently have a time displayed as 15:12:00 (HH:MM:SS) format. I am looking to convert this into the (3.12 PM) format. <p class="headings" display-format="HH:mm" > <b>Time :</b> {{this.starttime}} </p> In my TypeScript code t ...

Dealing with Cross-Origin Resource Sharing problem in serving static images on Dotnet Core 3.1

I have my dotnet core 3.1 API hosted with Angular on a different domain than my frontend HTML site. I am using a jQuery watermark plugin to add watermarks to my images, but sometimes the image displays normally without the watermark being added. Upon check ...

Angular's toolbar has the ability to alter the appearance of my login interface

I am facing an issue with my final paper assignment regarding my Angular project and the Login component. I wanted to hide the toolbar component when there is no currentUser present in the system. Although I was able to achieve this using <ng-content> ...

Converting PascalCase to camelCase, searching and substituting in an Angular project

Just curious, is there a software or program available that can detect PascalCase and convert it to camelCase? Or even just identify the instances of PascalCase so I can change them manually. Currently, I am going through each file laboriously updating cas ...

Maximizing Reusability: Implementing Redux Toolkit Reducers with TypeScript

Below is an example of a slice that I have: const authSlice = createSlice({ name: "auth", initialState: { ...initialUserInfo, ...initialBasicAsyncState, }, reducers: { setUser: (state, { payload }: PayloadAction<{ userObj: ...

You cannot assign a constructor type that is abstract to one that is not abstract

I am attempting to create an abstract component in Angular, but I am encountering the following error: Error: src/app/app.module.ts:191:5 - error TS2322: Type 'typeof AbstractFormComponent' is not assignable to type 'any[] | Type<any>& ...

Unable to locate the "lint" target for the project selected OR The data path "" must contain the necessary property 'lintFilePatterns'

Upon transitioning my Angular project structure to a monorepo setup as demonstrated in this tutorial, I encountered several configuration issues that required extensive troubleshooting to resolve. The first error message stated: Cannot find "lint" target ...

Ability to utilize an alternative attribute type within a TypeScript object

Within my class, I have: class Target { ... readonly overlay: { type: 'none' } | { type: 'centering', style?: { color?: string, offset?: number, size?: number, } } | { type: 'enterin ...

Discovering the following solution in JavaScript

I am a beginner in the field of web development and seeking help in generating a specific output for a given problem: var totalRows = 5; var result = ''; for (var i = 1; i <= totalRows; i++) { for (var j = 1; j <= i; j++) { res ...

Having trouble with Rollup resolving my typings file during the build process

Embarking on the journey of creating my inaugural library, I diligently followed a comprehensive guide. Utilizing rollup to consolidate all components and dependencies. The conundrum that currently besets me is the perplexing error thrown by rollup regard ...

Updating an Angular mat-table when the drawer is located in a separate component: A complete guide

The primary element contains tabs, component A, B, and so on. My component_A comes with preloaded information, but it also includes a filter button. Additionally, there is a button that triggers a drawer from another component (the main component). toggl ...