How to remove a watermark from a Word document.

348 views
Skip to first unread message

Fatbob

unread,
Sep 8, 2011, 10:08:23 AM9/8/11
to ComfyJ Forum
Hi everyone,

I'm using ComfyJ/JNIWrapper to maniulate Word documents. So far I'm
able to open up a doc file and save it as pdf. But what I need to do
is remove the watermark on the doc file before saving it as pdf.

Here is the code I'm using to open the doc and save as pdf...

public static void main(String[] args) {
OleFunctions.oleInitialize();

_Application app = Application.create(ClsCtx.LOCAL_SERVER);
app.setVisible(new VariantBool(false));

try
{
_Document doc = app.getDocuments().add(new Variant("c:\\Doc1.doc"),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter());

doc.saveAs(new Variant("c:\\output4544.pdf"),
new Variant(WdSaveFormat.wdFormatPDF),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter());
}
catch (Exception e)
{
}
finally
{
app.quit(Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter(),
Variant.createUnspecifiedParameter());
}
}

I have C# code that removes the watermark, but I can't seem to convert
that code to Java. Can anyone help me in converting this C# code?

public void DeleteWatermark()
{
Word.Application wa = new Word.Application();

try
{
Word.Selection Selection = wa.Selection;
//Select the section
document.Sections[1].Range.Select();
wa.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekCurrentPageHeader;
object shapeName = SHAPENAME;
Selection.HeaderFooter.Shapes.get_Item(ref shapeName).Delete();
//set focus back to document
wa.ActiveWindow.ActivePane.View.SeekView =
Word.WdSeekView.wdSeekMainDocument;
}
catch (Exception ex)
{
Debug.Print(ex.Source + " " + ex.Message);
}
}

Thanks!

Yuriy Privezentsev

unread,
Sep 9, 2011, 11:03:47 AM9/9/11
to Fatbob, ComfyJ Forum
Hi Fatbob.

In fact there is no special trick in translating C# code to Java code since interfaces and method names are the same in C# stubs and Java stubs generated by ComfyJ code generator. Please find the translated method below:

    private void removeWatermark(_Document document) {
        _Application application = document.getApplication();
        Selection selection = application.getSelection();

        Sections sections = document.getSections();
        Section first = sections.item(new Int32(1));
        Range range = first.getRange();
        range.select();

        Window activeWindow = application.getActiveWindow();
        Pane activePane = activeWindow.getActivePane();
        View view = activePane.getView();
        view.setSeekView(new WdSeekView(WdSeekView.wdSeekCurrentPageHeader));

        HeaderFooter headerFooter = selection.getHeaderFooter();
        Shapes shapes = headerFooter.getShapes();

        //Considering that you do not know the exact name of the watermark shape you will have to iterate over
        //the shape array looking for something that looks like watermark
        Int32 shapesCount = shapes.getCount();
        long shapesCountValue = shapesCount.getValue();
        for (long i = 1; i <= shapesCountValue; i++) {
            Variant shapeNumber = new Variant(i);
            Shape item = shapes.item(shapeNumber);
            String itemName = item.getName().toString();
            if(itemName.contains("WaterMark")){
                item.delete();
            }
        }

        view.setSeekView(new WdSeekView(WdSeekView.wdSeekMainDocument));
    }

Please try it and let me know the results.

Sincerely,
Yuriy.

Fatbob

unread,
Sep 9, 2011, 1:58:04 PM9/9/11
to ComfyJ Forum
Thanks Yuriy! I'll try it out.

On Sep 9, 11:03 am, Yuriy Privezentsev

anil.k...@sumasoft.net

unread,
Aug 23, 2018, 6:07:11 AM8/23/18
to ComfyJ Forum, sbucci...@gmail.com
Hi,

This code is in complete.
please share complete code with demo
Thanks & Reagrds
Anil Kushaba

Nataliya Rybynok

unread,
Aug 23, 2018, 9:25:06 AM8/23/18
to anil.k...@sumasoft.net, ComfyJ Support
Hello Anil,
Thank you for reaching out!

The provided answer contains an implementation of the function that removes watermarks from the document.

You have to call this function and pass the Document instance to it. Have you tried to call this method? If it is not working correctly, please send us information about the actual result of its execution.

Sincerely,
Nataliya Rybynok

Customer Service Manager | TeamDev, Ltd.
Phone +380 57 766-4300
teamdev.com






Confidentiality Note:
This e-mail message and any attachments to it are intended only for the named recipients and may contain legally privileged and/or confidential information. If you are not one of the intended recipients, please forward this mail to ab...@sumasoft.net and do not duplicate or forward this e-mail message and immediately delete it from your computer.

--
You received this message because you are subscribed to the Google Groups "ComfyJ Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to comfyj-forum...@teamdev.com.
To view this discussion on the web visit https://groups.google.com/a/teamdev.com/d/msgid/comfyj-forum/0dc4b6c1-a01c-458e-8572-1819e9c877a1%40teamdev.com.
Reply all
Reply to author
Forward
0 new messages