2013-07-31 69 views
1

這是我的代碼。但它不工作。如何在Java文件中使用Aspose.Word設置水印

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"); 
    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); 
    } 
} 
+0

「不工作」的方式來模糊。請告訴我們什麼是不工作,你得到什麼錯誤,你實際得到什麼,比你期望的,等等。 – fvu

+0

我想我正在使用舊的jar文件。 – Avinash

+0

你還沒有說出了什麼問題.... – fvu

回答

1

我解決了我的問題。其實它是jar文件的衝突。 Here我得到了新的jar文件。現在我的代碼正在工作。 下面是更新後的代碼:

/** 
* Inserts a watermark into a document. 
* 
* @param doc The input document file. 
* @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"); 
    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); 
    } 
    System.out.println("Watermark Set"); 
} 

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)); 
} 
相關問題