Free Support Forum - aspose.com

File corrupted after editing and adding a Watermakt

Hi,


I try to add a Watermarkt in a WORD document but having an issue. It works perfectly if i don’t edit the document. But if i edit it using WORD(i.e. adding some text) and then add the Watermarkt, the file is corrupted. Can you help me please ?

Here the code i use :

private void addWatermark()
{
using (FileStream fileStream = new FileStream(@“C:\Users\Kevin\Documents\input.docm”, FileMode.Open, FileAccess.ReadWrite))
{
insertWatermarkText(fileStream, “WATERMARKT”);
}

}
private void insertWatermarkText(Stream fileStream, string watermarkText)
{
Aspose.Words.Document doc = new Aspose.Words.Document(fileStream);

// Create a watermark shape. This will be a WordArt shape.
// You are free to try other shape types as watermarks.
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);

// Set up the text of the watermark.
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = “Arial”;
watermark.Width = 500;
watermark.Height = 100;
// Text will be directed from the bottom-left to the top-right corner.
watermark.Rotation = -40;
// Remove the following two lines if you need a solid black text.
watermark.Fill.Color = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark
watermark.StrokeColor = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark

// Place the watermark in the page center.
watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
watermark.WrapType = Aspose.Words.Drawing.WrapType.None;
watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;

// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark);

// Insert the watermark into all headers of each document section.
foreach (Section sect in doc.Sections)
{
// There could be up to three different headers in each section, since we want
// the watermark to appear on all pages, insert into all headers.
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}

doc.Save(fileStream, Aspose.Words.SaveFormat.Docm);
fileStream.Seek(0, SeekOrigin.Begin);
}
private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];

if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}

// Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(true));
}

Thanks!

Kevin

Hi Kevin,


Thanks for your inquiry. I have tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem as WORDSNET-7793 in our issue tracking system. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-7793) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi there,


Just to clarify the fix for this issue, it was closed as not a bug. You are saving the modified document back to the original stream which results in a corrupt document.

You need to use code like the following, however it is recommended to save to a completely separate stream anyway.

using (FileStream fileStream = new FileStream(@“C:\Temp\any.docx”, FileMode.Open, FileAccess.ReadWrite))
{
Document doc = new Document(fileStream);
<span style="line-height: 1.3em;">fileStream.Position = 0;</span><span class="code-comment" style="color: rgb(128, 128, 128);">
doc.Save(fileStream, SaveFormat.Docx); }


Thanks,