The DRAFT watermark is now added to the master slides with a lighter color

This commit is contained in:
2025-03-10 20:15:04 +01:00
parent deeed2a735
commit e8a41e9206
+54 -62
View File
@@ -40,41 +40,33 @@ export const DraftButtons: React.FC = () => {
setIsProcessing(true); setIsProcessing(true);
try { try {
await PowerPoint.run(async (context) => { await PowerPoint.run(async (context) => {
// Get all slides in the presentation // Get the slide masters collection
const slides = context.presentation.slides; const masters = context.presentation.slideMasters;
slides.load("items"); masters.load("items");
await context.sync(); await context.sync();
// Get a reference slide to determine dimensions if (masters.items.length === 0) {
// Since we can't access presentation width/height directly setStatusMessage("Could not access slide masters.");
if (slides.items.length === 0) { setStatusType("error");
setStatusMessage("No slides found in the presentation.");
setStatusType("warning");
return; return;
} }
// Process counter // Process counter
let processedSlides = 0; let processedMasters = 0;
let errorSlides = 0; let errorMasters = 0;
// Process each slide // Process each slide master (usually there's just one)
for (let i = 0; i < slides.items.length; i++) { for (let i = 0; i < masters.items.length; i++) {
try { try {
const slide = slides.items[i]; const master = masters.items[i];
//const positionFromTop = slideHeight - 23; // Create the textbox on the master slide
const textBox = master.shapes.addTextBox("");
// Create the textbox - make sure it spans the full width of the slide
// This is important for proper centering
const textBox = slide.shapes.addTextBox("");
// Make it span most of the width of the slide (90% centered)
// This ensures we have room for the text to be centered
//const textBoxWidth = slideWidth * 1; // 0.9
textBox.left = 0; // Center it horizontally textBox.left = 0; // Center it horizontally
textBox.top = 0; textBox.top = 0;
textBox.width = 960; textBox.width = 960;
textBox.height = 540; // Smaller height for footer text textBox.height = 540;
await context.sync(); await context.sync();
@@ -103,12 +95,12 @@ export const DraftButtons: React.FC = () => {
textBox.textFrame.textRange.font.bold = true; textBox.textFrame.textRange.font.bold = true;
textBox.textFrame.verticalAlignment = "MiddleCentered" textBox.textFrame.verticalAlignment = "MiddleCentered"
// Set the color to RGB(218, 19, 53) // Set the color to RGB(255, 233, 232)
// Different APIs may need different color formats // Different APIs may need different color formats
textBox.textFrame.textRange.font.color = "#DA1335"; textBox.textFrame.textRange.font.color = "#FFE9E8";
// Set the text // Set the text
textBox.textFrame.textRange.text = "DRAFT"; textBox.textFrame.textRange.text = "DRAFT";
} catch (fontError) { } catch (fontError) {
console.error("Error setting font properties:", fontError); console.error("Error setting font properties:", fontError);
// Even if we can't set the font properties exactly, continue with default font // Even if we can't set the font properties exactly, continue with default font
@@ -118,25 +110,25 @@ export const DraftButtons: React.FC = () => {
textBox.name = "DraftWatermark"; textBox.name = "DraftWatermark";
await context.sync(); await context.sync();
processedSlides++; processedMasters++;
} }
} catch (slideError) { } catch (masterError) {
console.error(`Error processing slide ${i+1}:`, slideError); console.error(`Error processing master ${i+1}:`, masterError);
errorSlides++; errorMasters++;
// Continue to the next slide // Continue to the next master
continue; continue;
} }
} }
// Report results // Report results
if (processedSlides > 0) { if (processedMasters > 0) {
setStatusMessage(`Added draft watermark to ${processedSlides} slides.`); setStatusMessage(`Added draft watermark to ${processedMasters} master slide${processedMasters > 1 ? 's' : ''}.`);
setStatusType("success"); setStatusType("success");
} else if (errorSlides > 0) { } else if (errorMasters > 0) {
setStatusMessage(`Failed to add markings. Errors on ${errorSlides} slides.`); setStatusMessage(`Failed to add markings. Errors on ${errorMasters} master slide${errorMasters > 1 ? 's' : ''}.`);
setStatusType("error"); setStatusType("error");
} else { } else {
setStatusMessage("No slides found to add draft watermark."); setStatusMessage("No master slides found to add draft watermark.");
setStatusType("warning"); setStatusType("warning");
} }
@@ -155,63 +147,63 @@ export const DraftButtons: React.FC = () => {
try { try {
await PowerPoint.run(async (context) => { await PowerPoint.run(async (context) => {
try { try {
// Get all slides in the presentation // Get the slide masters collection
const slides = context.presentation.slides; const masters = context.presentation.slideMasters;
slides.load("items"); masters.load("items");
await context.sync(); await context.sync();
if (slides.items.length === 0) { if (masters.items.length === 0) {
setStatusMessage("No slides found in the presentation."); setStatusMessage("Could not access slide masters.");
setStatusType("warning"); setStatusType("error");
return; return;
} }
// Process counter // Process counter
let processedSlides = 0; let processedMasters = 0;
let errorSlides = 0; let errorMasters = 0;
let removedCount = 0; let removedCount = 0;
// Process each slide // Process each master slide
for (let i = 0; i < slides.items.length; i++) { for (let i = 0; i < masters.items.length; i++) {
try { try {
const slide = slides.items[i]; const master = masters.items[i];
// Load all shapes on the slide // Load all shapes on the master slide
slide.shapes.load("items"); master.shapes.load("items");
await context.sync(); await context.sync();
// Find shapes with name "ConfidentialMarking" // Find shapes with name "DraftWatermark"
for (let j = 0; j < slide.shapes.items.length; j++) { for (let j = 0; j < master.shapes.items.length; j++) {
const shape = slide.shapes.items[j]; const shape = master.shapes.items[j];
shape.load("name"); shape.load("name");
await context.sync(); await context.sync();
if (shape.name === "DraftWatermark") { if (shape.name === "DraftWatermark") {
// Delete the confidential marking shape // Delete the draft watermark shape
shape.delete(); shape.delete();
removedCount++; removedCount++;
} }
} }
await context.sync(); await context.sync();
processedSlides++; processedMasters++;
} catch (slideError) { } catch (masterError) {
console.error(`Error processing slide ${i+1}:`, slideError); console.error(`Error processing master slide ${i+1}:`, masterError);
errorSlides++; errorMasters++;
// Continue to the next slide // Continue to the next master slide
continue; continue;
} }
} }
// Report results // Report results
if (removedCount > 0) { if (removedCount > 0) {
setStatusMessage(`Removed ${removedCount} draft watermark from ${processedSlides} slides.`); setStatusMessage(`Removed ${removedCount} draft watermark${removedCount > 1 ? 's' : ''} from ${processedMasters} master slide${processedMasters > 1 ? 's' : ''}.`);
setStatusType("success"); setStatusType("success");
} else if (errorSlides > 0) { } else if (errorMasters > 0) {
setStatusMessage(`Failed to remove draft watermarks. Errors on ${errorSlides} slides.`); setStatusMessage(`Failed to remove draft watermarks. Errors on ${errorMasters} master slide${errorMasters > 1 ? 's' : ''}.`);
setStatusType("error"); setStatusType("error");
} else { } else {
setStatusMessage("No daft watermark found to remove."); setStatusMessage("No draft watermark found to remove.");
setStatusType("info"); setStatusType("info");
} }
} catch (innerError) { } catch (innerError) {