Aspose.Wordsを使用してWord文書にページをフォーマットし、ブランディングを追加する方法
La personnalisation des layouts de page et l’application d’éléments de marquage tels que les heads, les pieds et les marques d’eau peuvent améliorer l’apparence professionnelle des documents Word.Utilisant Aspose.Words pour .NET, les développeurs peuvent programmatiquement mettre en œuvre ces fonctionnalités avec précision.
Principaux critères : Mettre en place votre environnement pour la personnalisation des documents Word
- Installez le Le .NET SDK .
- Ajoutez le paquet Aspose.Words à votre projet :
dotnet add package Aspose.Words
- Préparer un document (
template.docx
Pour tester le formatage et le branding.
Guide étape par étape pour le formatage des pages et l’ajout de la marque dans les fichiers Word
Étape 1: Téléchargez le document Word pour la personnalisation
using System;
using Aspose.Words;
class Program
{
static void Main()
{
// Step 1: Load the Word document
string filePath = "template.docx";
Document doc = new Document(filePath);
// Steps 2, 3, and 4 will be added below
}
}
Explication: Ce code charge le document Word spécifié dans la mémoire pour une personnalisation ultérieure.
Étape 2: Ajoutez un header avec Branding
using System;
using Aspose.Words;
class Program
{
static void Main()
{
string filePath = "template.docx";
Document doc = new Document(filePath);
// Step 2: Add a Header with Branding
foreach (Section section in doc.Sections)
{
HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
section.HeadersFooters.Add(header);
Paragraph headerParagraph = new Paragraph(doc);
headerParagraph.AppendChild(new Run(doc, "Company Name - Confidential"));
header.Paragraphs.Add(headerParagraph);
}
// Steps 3 and 4 will be added below
}
}
Explication: Ce code ajoute un titre avec un texte de marque à chaque section du document Word.
Étape 3 : Appliquer un watermark sur le document
using System;
using Aspose.Words;
using System.Drawing;
class Program
{
static void Main()
{
string filePath = "template.docx";
Document doc = new Document(filePath);
foreach (Section section in doc.Sections)
{
HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
section.HeadersFooters.Add(header);
Paragraph headerParagraph = new Paragraph(doc);
headerParagraph.AppendChild(new Run(doc, "Company Name - Confidential"));
header.Paragraphs.Add(headerParagraph);
}
// Step 3: Apply a Watermark
AddWatermark(doc, "CONFIDENTIAL");
// Step 4 will be added below
}
static void AddWatermark(Document doc, string watermarkText)
{
foreach (Section section in doc.Sections)
{
Shape watermark = new Shape(doc, ShapeType.TextPlainText)
{
TextPath = { Text = watermarkText, FontFamily = "Arial" },
Width = 300,
Height = 70,
Rotation = -40,
FillColor = Color.LightGray,
StrokeColor = Color.LightGray,
WrapType = WrapType.None,
BehindText = true,
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
Left = 100,
Top = 200
};
section.HeadersFooters[HeaderFooterType.HeaderPrimary]?.AppendChild(watermark);
}
}
}
Explication: Ce code ajoute un watermark “CONFIDENTIAL” à chaque page du document.
Étape 4 : sauvegarder le document mis à jour
using System;
using Aspose.Words;
using System.Drawing;
class Program
{
static void Main()
{
string filePath = "template.docx";
Document doc = new Document(filePath);
foreach (Section section in doc.Sections)
{
HeaderFooter header = section.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
section.HeadersFooters.Add(header);
Paragraph headerParagraph = new Paragraph(doc);
headerParagraph.AppendChild(new Run(doc, "Company Name - Confidential"));
header.Paragraphs.Add(headerParagraph);
}
AddWatermark(doc, "CONFIDENTIAL");
// Step 4: Save the Updated Document
string outputPath = "FormattedDocument.docx";
doc.Save(outputPath);
Console.WriteLine("Document formatting and branding applied successfully.");
}
static void AddWatermark(Document doc, string watermarkText)
{
foreach (Section section in doc.Sections)
{
Shape watermark = new Shape(doc, ShapeType.TextPlainText)
{
TextPath = { Text = watermarkText, FontFamily = "Arial" },
Width = 300,
Height = 70,
Rotation = -40,
FillColor = Color.LightGray,
StrokeColor = Color.LightGray,
WrapType = WrapType.None,
BehindText = true,
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
Left = 100,
Top = 200
};
section.HeadersFooters[HeaderFooterType.HeaderPrimary]?.AppendChild(watermark);
}
}
}
Explication: Ce code enregistre le document modifié avec le titre appliqué et le marquage d’eau.
Étape 5: Testez votre solution de formatage de document Word
- Exécutez le programme et vérifiez ce qui suit :- Le titre est ajouté à toutes les pages avec le texte de marque.
- Un diagonale “CONFIDENTIAL” apparaît sur chaque page.
Options d’hébergement: Déployer des solutions de formatage de documents Word sur différentes plateformes
Déploiement sur Windows
- Installez le temps de fonctionnement .NET et hébergez l’application sur IIS pour une accessibilité plus large.
- Testez l’application localement ou déployez-la pour une utilisation à distance.
Déploiement sur Linux
- Installez le temps de fonctionnement ASP.NET Core.
- Utilisez Nginx pour servir l’application et permettre le traitement de documents sans fil.
Déploiement sur macOS
- Utilisez le serveur Kestrel pour tester l’application localement.
- Déployer la solution dans un environnement cloud pour l’escalabilité.
Problèmes communs lors du formatage des pages dans les documents Word
Les marques d’eau ne sont pas affichées:- Assurez-vous que le marquage d’eau est ajouté au titre principal de chaque section.
Les titres manquants sur des pages spécifiques:- Vérifiez les rubriques de la section et assurez-vous qu’elles sont activées pour toutes les sections.
Les questions d’alignement des documents:- Ajuster la position des éléments de marquage tels que les marques d’eau et les chevaux en utilisant des coordonnées précises.
En suivant ce guide, vous pouvez créer des documents Word formatifs avec des marques personnalisées et des layouts cohérents en utilisant Aspose.Words pour .NET.