|
|
@@ -6,9 +6,7 @@ import com.aspose.words.Shape;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.awt.*;
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
|
|
|
@@ -74,7 +72,9 @@ public class Word2Pdf {
|
|
|
//lo.setEncoding(Encoding.getUTF8());
|
|
|
Document doc = new Document(inputStream);// 原始excel路径
|
|
|
removeWatermark(doc);
|
|
|
- insertWatermarkText(doc, watermark);
|
|
|
+ insertWatermarkText(doc, watermark, 0, 0);
|
|
|
+ insertWatermarkText(doc, watermark, 200, 0);
|
|
|
+ insertWatermarkText(doc, watermark, 400, 0);
|
|
|
|
|
|
/*PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(SaveFormat.PDF);
|
|
|
pdfSaveOptions.setOnePagePerSheet(true);
|
|
|
@@ -93,6 +93,36 @@ public class Word2Pdf {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static void wordWatermark(String watermark, OutputStream outputStream, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+// URL url = new URL(Address);
|
|
|
+// HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
|
+// //得到输入流
|
|
|
+// InputStream inputStream = conn.getInputStream();
|
|
|
+//
|
|
|
+// //TxtLoadOptions lo = new TxtLoadOptions();
|
|
|
+// //lo.setEncoding(Encoding.getUTF8());
|
|
|
+ ByteArrayOutputStream baos=new ByteArrayOutputStream();
|
|
|
+ baos=(ByteArrayOutputStream) outputStream;
|
|
|
+ ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
|
|
|
+
|
|
|
+// BufferedOutputStream bos = new BufferedOutputStream(outputStream);
|
|
|
+// ByteArrayInputStream swapStream = new ByteArrayInputStream(bos..getBytes());
|
|
|
+ Document doc = new Document(swapStream);// 原始excel路径
|
|
|
+ removeWatermark(doc);
|
|
|
+ insertWatermarkText(doc, watermark, 0, 0);
|
|
|
+ insertWatermarkText(doc, watermark, 200, 0);
|
|
|
+ insertWatermarkText(doc, watermark, 400, 0);
|
|
|
+ OutputStream output = response.getOutputStream();
|
|
|
+// output.write(swapStream);
|
|
|
+// output.flush();
|
|
|
+ doc.save(output, SaveFormat.DOCX);
|
|
|
+// inputStream.close();
|
|
|
+ }catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* Inserts a watermark into a document.
|
|
|
@@ -101,7 +131,7 @@ public class Word2Pdf {
|
|
|
* @param watermarkText Text of the watermark.
|
|
|
*
|
|
|
*/
|
|
|
- private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
|
|
|
+ private static void insertWatermarkText(Document doc, String watermarkText, double top, double left) 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);
|
|
|
@@ -116,11 +146,13 @@ public class Word2Pdf {
|
|
|
watermark.getFill().setColor(Color.lightGray); // Try LightGray to get more Word-style watermark
|
|
|
watermark.setStrokeColor(Color.lightGray); // Try LightGray to get more Word-style watermark
|
|
|
// Place the watermark in the page center.
|
|
|
- watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
|
|
|
- watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
|
|
|
+// watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
|
|
|
+// watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
|
|
|
watermark.setWrapType(WrapType.NONE);
|
|
|
- watermark.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
- watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
+// watermark.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+// watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
+ watermark.setTop(top);
|
|
|
+ watermark.setLeft(left);
|
|
|
// Create a new paragraph and append the watermark to this paragraph.
|
|
|
Paragraph watermarkPara = new Paragraph(doc);
|
|
|
watermarkPara.appendChild(watermark);
|