Free Support Forum - aspose.com

Delete WaterMark aspose.words

Hey all,


I am using aspose.words for to add the watermark into document, but I need to delete this watermark, I search in all side but I don’t find nothing.

if you know about this, Can you help me please?

thanks in advance.

this is my code java:

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Document doc = new Document(“C:\Users\German\Documents\memorando-general.docx”);
insertWatermarkText(doc, “CONFIDENTIAL”);
doc.save(“C:\Users\German\Documents\memorando-general-OUT.docx”);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 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.TEXT_PLAIN_TEXT);
// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(“Arial Black”);
watermark.setWidth(500);
watermark.setHeight(100);
// 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.red); // 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));
}
private static void RemoveWatermark(Document doc)
{
//What do I add here?
}

Hi German,


Thanks for your inquiry. You can use the following workflow to be able to remove watermark Shapes from Document:
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“D:<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>temp<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>in.docx”<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
insertWatermarkText(doc, “CONFIDENTIAL”);
removeWatermarkText(doc);

doc.save(“D:\temp\awjava-17.1.0.docx”);

<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>private static void <span style=“font-family: “Courier New”; font-size: 9pt;”>removeWatermarkText(Document doc) <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>throws <span style=“font-family: “Courier New”; font-size: 9pt;”>Exception {
<pre style=“font-family: “Courier New”; font-size: 9pt;”> for(HeaderFooter hf :(Iterable) doc.getChildNodes(NodeType.HEADER_FOOTER, true)){
for(Shape shape :(Iterable) hf.getChildNodes(NodeType.SHAPE, true)){
if(shape.getName().contains(“WaterMark”))
shape.remove();
}
}
}


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.TEXT_PLAIN_TEXT);

// Set name to be able to remove it afterwards
watermark.setName(“WaterMark”);

// Set up the text of the watermark.
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily(
“Arial”);
watermark.setWidth(
500);
watermark.setHeight(
100);
// 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));
}

Best regards,
Hello, I currently need to manage some editable areas that allow me to restrict the writing of some texts in java. I have been looking for information on this subject but I find nothing. Can you help me with this please?

I am currently using version 17.1.0 of aspose.words

Thanks in advance!
Hi,

Thanks for your inquiry. I think, you may be able to achieve this by protecting whole document and then allow some areas in document to be editable. Please see following example:

Document doc = new Document();
DocumentBuilder builder =
new DocumentBuilder(doc);

builder.writeln(
"Some text is added.");
builder.writeln(
"Some text is added.");
builder.writeln(
"Some text is added.");

builder.startEditableRange();
builder.write(
"Some text is added.");

Comment comment =
new Comment(doc, "Awais Hafeez", "AH", new Date());
builder.getCurrentParagraph().appendChild(comment);
comment.getParagraphs().add(
new Paragraph(doc));
comment.getFirstParagraph().getRuns().add(
new Run(doc, "Comment text."));
builder.endEditableRange();

doc.protect(ProtectionType.
READ_ONLY);

doc.save(
getMyDir() + "awjava-16.3.0.docx");

Hope, this helps.

Best regards,
Hello everyone,

I'm sorry to disturb you but I need to solve this problem, I need to insert the editable areas in a specific place of the document, for example where it finds all the "{}" tags. The previous example is perfect but I need to add this detail, and tried a lot but I could not.

Thanks in advance!
Hi,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone Java application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we'll start further investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.

Best regards,
Thanks for your help and understanding.

Hi,


Thanks for your inquiry. Please try using the following code and see attached output:
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document doc = <span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>new <span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>Document(<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>“D:<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>temp<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>Informe_Aspose_Test_EditAreas<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 0, 128); font-weight: bold;”>\<span style=“font-family: “Courier New”; font-size: 9pt; color: rgb(0, 128, 0); font-weight: bold;”>memorando-general.docx”<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>);
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
FindReplaceOptions opts = new FindReplaceOptions();
opts.setDirection(FindReplaceDirection.BACKWARD);
opts.ReplacingCallback = new ReplaceEvaluator();

Pattern regex = Pattern.compile("\{Editable Text\}", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, “{Editable Text}”, opts);

doc.protect(ProtectionType.READ_ONLY);

doc.save(“D:\temp\Informe_Aspose_Test_EditAreas\awjava-17.2.0.docx”);


<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>
<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”; font-size: 9pt;”>static class ReplaceEvaluator implements IReplacingCallback
{
/
* This method is called by the Aspose.Words find and replace engine for each match.
* This method highlights the match string, even if it spans multiple runs.
*/
public int replacing(ReplacingArgs e) throws Exception
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.getMatchNode();

// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run)currentNode, e.getMatchOffset());

// This array is used to store all nodes of the match for further highlighting.
ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group().length();
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.getText().length() <= remainingLength))
{
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();

// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.getNextSibling();
}
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}

// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
splitRun((Run)currentNode, remainingLength);
runs.add(currentNode);
}

DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
builder.moveTo((Run) runs.get(runs.size() - 1));

builder.startEditableRange();
builder.write(e.getReplacement());
builder.endEditableRange();

for (Run run : (Iterable) runs)
run.remove();

// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.SKIP;
}

/
* Splits text of the specified run into two runs.
* Inserts the new run just after the specified run.
*/
private Run splitRun(Run run, int position) throws Exception
{
Run afterRun = (Run)run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
}

Hope, this helps.

Best regards,