How to add Watermark in Aspose.Words for Java

Last post 10-30-2011, 2:40 PM by aspose.notifier. 16 replies.
Page 2 of 2 (17 items)   < Previous 1 2
Sort Posts: Previous Next
  •  03-10-2011, 2:45 PM 290177 in reply to 290170

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

    Hi

     

    Thanks for your inquiry. Please try using the following code.

     

    @Test

    public void Test001() throws Exception {

     

        // Open the document.

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

        // Insert Watermark before saving to PDF.

        insertWatermarkText(doc, "Watermark");

        // Save to PDF

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

     

    }

     

    private static void insertWatermarkText(Document doc, String watermarkText) 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);

     

        GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();

        // Create buffered image that supports transparency

        BufferedImage bufimage = new BufferedImage(500, 500, BufferedImage.TYPE_INT_BGR);

        // Create Graphics2D.

        Graphics2D g2d = environment.createGraphics(bufimage);

     

        g2d.setColor(Color.white);

        g2d.fillRect(0, 0, 500, 500);

        g2d.setColor(Color.gray);

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

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

        g2d.drawString(watermarkText, 40, 50);

     

        // Set up the the watermark.

        watermark.getImageData().setImage(bufimage);

        watermark.setWidth(500);

        watermark.setHeight(500);

     

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

    }

     

    This code does not produce an excellent result, but the result is acceptable and I think you can use this approach while you are waiting for a fix.

     

    Best regards, 


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  10-30-2011, 2:40 PM 338977 in reply to 52366

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

    The issues you have found earlier (filed as WORDSJAVA-35) have been fixed in this .NET update and in this Java update.


    This message was posted using Notification2Forum from Downloads module by aspose.notifier.
    (9)
     
Page 2 of 2 (17 items)   < Previous 1 2
View as RSS news feed in XML