We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document content is not editable after adding watermark

Hi Team,


I need to add an image file as back ground to the word document and that image should not be editable as suggested by your technical team member i used below code for this.

/*
* Copyright 2001-2014 Aspose Pty Ltd. All Rights Reserved.
*
* This file is part of Aspose.Words. The source code in this file
* is only intended as a supplement to the documentation, and is provided
* “as is”, without warranty of any kind, either expressed or implied.
*/
package com.test;

import java.awt.Color;

import com.aspose.words.Document;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.HorizontalAlignment;
import com.aspose.words.Paragraph;
import com.aspose.words.ProtectionType;
import com.aspose.words.RelativeHorizontalPosition;
import com.aspose.words.RelativeVerticalPosition;
import com.aspose.words.Section;
import com.aspose.words.Shape;
import com.aspose.words.ShapeType;
import com.aspose.words.VerticalAlignment;
import com.aspose.words.WrapType;


public class AddWatermark
{
static String dataDir=“d:\images\”;
public static void main(String[] args) throws Exception
{
// The path to the documents directory.
Document doc = new Document(dataDir + “test.docx”);
insertWatermarkText(doc, “d:\images\Demo.png”);
doc.protect(ProtectionType.READ_ONLY);
doc.save(dataDir + “TestFileOut1.docx”);

System.out.println(“Watermark added to the document successfully.”);
}

/**
* Inserts a watermark into a document.
*
* @param doc The input document.
* @param watermarkText Text of the watermark.
*/
private static void insertWatermarkText(Document doc, String watermarkText) throws Exception
{
// Create a watermark shape. This will be a WordArt shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.IMAGE);
watermark.getImageData().setImage(watermarkText);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(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.
for (Section sect : doc.getSections())
{
// 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.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception
{
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}

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

Even though the image which is added in all pages it not editable I am not able to edit the text as well which should not be the case. I should be able to edit the text and should not be able to edit the image which i added. Can you please help me in this regard. I am enclosing the word document for your reference in which I am not able to edit the text. Can you please help me in this regard.

Regards
Balu

Hi Balu,


Thanks for your inquiry. EditableRangeStart class represents a start of an editable range in a Word document.EditableRangeEnd class represents an end of an editable range in a Word document.

A complete editable range in a Word document consists of a EditableRangeStart and a matching EditableRangeEnd with the same Id. EditableRangeStart and EditableRangeEnd are just markers inside a document that specify where the editable range starts and ends.

Please note that currently editable ranges are supported only at the inline-level, that is inside Paragraph, but editable range start and editable range end can be in different paragraphs. Badly formed editable range will be ignored when the document is saved.

Please use following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + “in.docx”);<o:p></o:p>

insertWatermarkText(doc, MyDir + "Demo.png");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToDocumentStart();

builder.startEditableRange();

builder.moveToDocumentEnd();

builder.endEditableRange();

doc.protect(ProtectionType.READ_ONLY);

doc.save(MyDir + "TestFileOut1.docx");

A post was split to a new topic: Protect the document