Hey all,
Hi German,
insertWatermarkText(doc, “CONFIDENTIAL”);
removeWatermarkText(doc);
doc.save(“D:\temp\awjava-17.1.0.docx”);
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));
}
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");
- 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.
Hi,
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;”>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;
}
}