Sign UpSign Up   Sign InSign In Welcome Guest,
Live Chat Live Chat

How to add Watermark in Aspose.Words for Java

Last post 10-30-2011, 2:40 PM by aspose.notifier. 16 replies.
Page 1 of 2 (17 items)   1 2 Next >
Sort Posts: Previous Next
  •  07-11-2006, 4:25 PM 52366

    How to add Watermark in Aspose.Words for Java

    How are watermarks added to .doc files under Aspose.Words for Java? 

     
  •  07-12-2006, 4:36 AM 52406 in reply to 52366

    Re: How to add Watermark in Aspose.Words for Java

    Hi, jebert,

    Below is a small example:

    /**
    * Opens an existing document and inserts a watermark header with some information
    * to indicate the document was processed by the system.
    */
    @Test
    public class WatermarkTest
    {
    @Test
    public void testWatermark() throws Exception
    {
    String fileIn = "X:\\tmp\\Dinner Invitation.doc";
    String fileOut = "X:\\tmp\\Dinner Invitation Out.doc";

    //Open the document
    Document doc = new Document(fileIn);

    //Create a document builder that will allow us to modify the document.
    DocumentBuilder builder = new DocumentBuilder(doc);

    //The cursor is in the first section already, navigate to the header as this is where
    //we want to insert the watermark.
    builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

    Border topBorder = builder.getCellFormat().getBorders().getByBorderType(BorderType.TOP);
    topBorder.setLineStyle(LineStyle.SINGLE);
    topBorder.setLineWidth(1);

    builder.insertCell();
    builder.getCellFormat().setWidth(3 * 72);
    builder.write(MessageFormat.format("Processed on: {0,date}", new Date()));

    builder.insertCell();
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
    builder.write("Version 1.00");

    builder.endRow();
    builder.endTable();

    //Save "watermarked" document
    doc.save(fileOut);
    }

    Best regards,

    Konstantin Sidorenko
    Java Subteam Leader, Aspose Auckland Team
     
  •  07-12-2006, 9:04 PM 52473 in reply to 52406

    Re: How to add Watermark in Aspose.Words for Java

    Is the only option to have this on the Header/Footer?  Is it possible to put this in the middle of the page?
     
  •  07-12-2006, 9:15 PM 52474 in reply to 52473

    Re: How to add Watermark in Aspose.Words for Java

    At the moment this is the only way in Aspose.Words for Java. Later, when drawing objects are supported, you will be able to insert a floating textbox, for example into the header and position it anywhere on the page. Estimated time 2-3 months.
    Roman Korchagin
    Aspose.Words Team Leader
    Aspose Auckland Team
     
  •  07-12-2006, 10:39 PM 52477 in reply to 52473

    Re: How to add Watermark in Aspose.Words for Java

    I must add that when you add watermark in MS Word via Format | Background | Printed Watermark menu the created watermark autoshape is always put inside the document header, although it can appear anywhere in the page. So technically watermark is a shape inside the document header.


    Vladimir Averkin
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-03-2010, 2:23 AM 256921 in reply to 52474

    Re: How to add Watermark in Aspose.Words for Java

    Adding printed watermark in the middle of the page is now supported by aspose.words in java?
    If so, please let me know how to add that.
    Please reply ASAP.

    thanks in advance.
     
  •  09-03-2010, 3:22 AM 256930 in reply to 256921

    Re: How to add Watermark in Aspose.Words for Java

    Hello

    Thanks for your inquiry. Yes you can do it. Please follow the link to learn how to insert Watermark in the document using Aspose.Words for Java:

    http://www.aspose.com/documentation/java-components/aspose.words-for-java/howto-add-a-watermark-to-a-document.html


    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-03-2010, 9:24 AM 256989 in reply to 256930

    Re: How to add Watermark in Aspose.Words for Java

    Hello,

    I have tried this, but the watermark is missing in the pdf document.

    Kind regards
      Andreas

     
  •  09-03-2010, 9:58 AM 256996 in reply to 256989

    Re: How to add Watermark in Aspose.Words for Java

    Hello

     

    Thanks for your request. Please try using the following code:

     

    // Open the document.

    Document doc = new Document("C:\\Temp\\Test.doc");

     

    // Work with document.

     

    // Insert Watermark before saving to PDF.

    insertWatermarkImage(doc, "C:\\Temp\\Watermark.jpg");

     

    doc.saveToPdf("C:\\Temp\\out.pdf");

     

    private static void insertWatermarkImage(Document doc, String watermarkImagePath) throws Exception

    {

       // Create a watermark shape. This will be a shape.

       // You are free to try other shape types as watermarks.

       Shape watermark = new Shape(doc, ShapeType.IMAGE);

     

        // Set up the the watermark.

       watermark.getImageData().setImage(watermarkImagePath);

       watermark.setWidth(500);

       watermark.setHeight(100);

     

       // 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));

    }

     

    If it does not help you, could you please attach you input and output documents here for testing?

     

    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-08-2010, 3:34 PM 257677 in reply to 256996

    Re: How to add Watermark in Aspose.Words for Java

    Hi Andrey,

    your example works fine.
    But it doesn´t work for me with a text shape as a watermark.

    // Open the document.
    Document doc = new Document("C:\\Temp\\Test.doc");
    // Work with document.
    // Insert Watermark before saving to PDF.
    insertWatermarkText(doc, "Watermark");
    doc.saveToPdf("C:\\Temp\\out.pdf");

    -------------------

    public void insertWatermarkText(Document doc, String watermarkText) throws Exception {

        Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

        // Set up the text of the watermark.
        watermark.getTextPath().setText(watermarkText);
        watermark.getTextPath().setFontFamily("Arial");
        watermark.setWidth(200);
        watermark.setHeight(10);

        // Text will be directed from the bottom-left to the top-right corner.
        // watermark.setRotation(-90);

        // Remove the following two lines if you need a solid black text.
        // watermark.getFill().setColor(Color.BLACK); //GRAY); // Try LightGray to get more Word-style watermark
        // watermark.setStrokeColor(Color.BLACK); // 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);
        }
    }


    Kind regards
      Andreas
     
  •  09-08-2010, 11:20 PM 257711 in reply to 257677

    Re: How to add Watermark in Aspose.Words for Java

    Hi

     

    Thanks for your inquiry. The problem occurs because Aspose.Words for Java does not support WordArt upon rendering. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-14-2010, 2:52 AM 258476 in reply to 257711

    Re: How to add Watermark in Aspose.Words for Java

    Hi,

    thanks für your answer.

    Is is possible to generate an image with the desired watermark text and to insert the generated image in the document ?
    Maybe this could be a workaround for our problem.

    Kind regards
       Andreas
     
  •  09-14-2010, 4:00 AM 258491 in reply to 258476

    Re: How to add Watermark in Aspose.Words for Java

    Hi Andreas,

     

    Thanks for your inquiry. Sure, you can generate an image on the fly. For instance, you can try using code like the following:

     

    int width = 500;

    int height = 500;

     

    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();

    // Create buffered image that supports transparency

    BufferedImage bufimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    // Create Graphics2D.

    Graphics2D g2d = environment.createGraphics(bufimage);

    g2d.setFont(new java.awt.Font("TimesRoman", java.awt.Font.BOLD | java.awt.Font.ITALIC, 100));

    g2d.setColor(Color.gray);

    g2d.rotate(Math.PI / 4, 40, 50);

    g2d.drawString("Watermark", 40, 50);

     

    // For testing insert the generated image into the document.

    Document doc = new Document();

    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.insertImage(bufimage);

    doc.save("C:\\Temp\\out.doc");

     

    Hope this helps.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  03-10-2011, 12:22 PM 290155 in reply to 258491

    Re: How to add Watermark in Aspose.Words for Java

    Hi, 
    I tried your workaround, but the image is always shown on top of existing text (I tried with the WrapStyle options, but nothing really works for me).
    Is there another workaround or an estimation when this issue will be fixed?
     
  •  03-10-2011, 2:02 PM 290170 in reply to 290155

    Re: How to add Watermark in Aspose.Words for Java

    Hi

     

    Thanks for your inquiry. As you may know, we are working on synchronizing .NET and Java versions of Aspose.Words. You can check progress here:

    http://www.aspose.com/blogs/aspose-products/aspose-words-product-family/archive/2011/02/24/autoporting-aspose.words-for-java-progress-24th-feb-2011.html

     

    Once we finish this work all functionality which is supported in, NET version will be supported in Java version, including this feature. You will be notified as soon new version of Aspose.Words for java is released.

     

    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
Page 1 of 2 (17 items)   1 2 Next >
View as RSS news feed in XML