کار با واترمارک
این موضوع در مورد چگونگی کار برنامه نویسی با یک علامت آبی با استفاده از Aspose.Words بحث می کند. علامت آبی یک تصویر پس زمینه است که در پشت متن در یک سند نمایش داده می شود. یک علامت آبی می تواند حاوی یک متن یا یک تصویر باشد که توسط کلاس Watermark نشان داده شده است.
سعی کنید آنلاین
شما می توانید این قابلیت را با ما امتحان کنید علامت آبی سند آنلاین رایگان.
یک علامت آبی به یک سند اضافه کنید
در Microsoft Word، یک علامت آبی را می توان به راحتی در یک سند با استفاده از دستور Insert Watermark وارد کرد. Aspose.Words کلاس watermark را برای اضافه کردن یا حذف علامت آبی در اسناد فراهم می کند. Aspose.Words شمارش WatermarkTypeرا فراهم می کند که سه نوع ممکن از علامت های آبی (متن، تصویر و هیچ) را برای کار با آن تعریف می کند.
اضافه کردن علامت آبی متن
مثال کد زیر نشان می دهد که چگونه یک علامت آبی متن را در یک سند با تعریف TextWatermarkOptions با استفاده از روش SetText وارد کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
TextWatermarkOptions options = new TextWatermarkOptions(); | |
options.setFontFamily("Arial"); | |
options.setFontSize(36); | |
options.setColor(Color.BLACK); | |
options.setLayout(WatermarkLayout.HORIZONTAL); | |
options.isSemitrasparent(false); | |
doc.getWatermark().setText("Test", options); | |
doc.save(dataDir + "AddTextWatermark_out.docx"); |
علامت آب تصویر را اضافه کنید
مثال کد زیر نشان می دهد که چگونه یک علامت آبی تصویر را در یک سند با تعریف ImageWatermarkOptions با استفاده از روش SetImage وارد کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
ImageWatermarkOptions options = new ImageWatermarkOptions(); | |
options.setScale(5); | |
options.isWashout(false); | |
ImageInputStream stream = ImageIO.createImageInputStream(new File(dataDir + "Watermark.png")); | |
doc.getWatermark().setImage(ImageIO.read(stream), options); | |
doc.save(dataDir + "AddImageWatermark_out.docx"); |
علامت آبی را می توان با استفاده از کلاس شکل نیز وارد کرد. بسیار آسان است که هر شکل یا تصویر را در یک سر یا پای صفحه قرار دهید و بنابراین یک علامت آبی از هر نوع قابل تصور ایجاد کنید.
مثال کد زیر یک علامت آبی را در یک سند ورد قرار می دهد:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
@Test | |
public void addWatermark() throws Exception | |
{ | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
insertWatermarkText(doc, "CONFIDENTIAL"); | |
doc.save(getArtifactsDir() + "WorkWithWatermark.AddWatermark.docx"); | |
} | |
/// <summary> | |
/// Inserts a watermark into a document. | |
/// </summary> | |
/// <param name="doc">The input document.</param> | |
/// <param name="watermarkText">Text of the watermark.</param> | |
private void insertWatermarkText(Document doc, String watermarkText) throws Exception | |
{ | |
// Create a watermark shape, this will be a WordArt shape. | |
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); { watermark.setName("Watermark"); } | |
watermark.getTextPath().setText(watermarkText); | |
watermark.getTextPath().setFontFamily("Arial"); | |
watermark.setWidth(500.0); | |
watermark.setHeight(100.0); | |
// 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.setFillColor(Color.GRAY); | |
watermark.setStrokeColor(Color.GRAY); | |
// 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 : (Iterable<Section>) doc.getSections()) | |
{ | |
// There could be up to three different headers in each section. | |
// Since we want the watermark to appear on all pages, insert it into all headers. | |
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); | |
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST); | |
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN); | |
} | |
} | |
private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, | |
/*HeaderFooterType*/int headerType) | |
{ | |
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType); | |
if (header == null) | |
{ | |
// There is no header of the specified type in the current section, so we need to 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)); | |
} |
علامت آب را از یک سند حذف کنید
کلاس Watermark روش Remove
را برای حذف علامت آبی از یک سند فراهم می کند.
نمونه های کد زیر نشان می دهد که چگونه یک علامت آبی را از اسناد حذف کنیم:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "AddTextWatermark_out.docx"); | |
if (doc.getWatermark().getType() == WatermarkType.TEXT) { | |
doc.getWatermark().remove(); | |
} | |
doc.save(dataDir + "RemoveWatermark_out.docx"); |
برای حذف علامت آبی از یک سند شما باید فقط نام شکل علامت آبی را در هنگام قرار دادن تنظیم کنید و سپس شکل علامت آبی را با یک نام اختصاص داده شده حذف کنید.
مثال کد زیر به شما نشان می دهد که چگونه نام شکل علامت آبی را تنظیم کنید و آن را از سند حذف کنید:
// Set name to be able to remove it afterwards
watermark.Name("WaterMark");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static final String dataDir = Utils.getDataDir(RemoveWatermark.class); | |
public static void main(String[] args) throws Exception { | |
Document doc = new Document(dataDir + "RemoveWatermark.docx"); | |
removeWatermarkText(doc); | |
doc.save(dataDir + "RemoveWatermark_out.doc"); | |
} | |
private static void removeWatermarkText(Document doc) throws Exception { | |
for (HeaderFooter hf : (Iterable<HeaderFooter>) doc.getChildNodes(NodeType.HEADER_FOOTER, true)) { | |
for (Shape shape : (Iterable<Shape>) hf.getChildNodes(NodeType.SHAPE, true)) { | |
if (shape.getName().contains("WaterMark")) | |
shape.remove(); | |
} | |
} | |
} |
یک علامت آبی به یک سلول میز اضافه کنید
گاهی اوقات شما باید یک علامت/تصویر را در سلول جدول قرار دهید و آن را در خارج از جدول نمایش دهید، می توانید از ویژگی IsLayoutInCell استفاده کنید. این ویژگی یک پرچم را دریافت یا تنظیم می کند که نشان می دهد آیا شکل در داخل یک جدول یا خارج از آن نمایش داده می شود. توجه داشته باشید که این ویژگی تنها زمانی کار می کند که شما سند را برای Microsoft Word 2010 با استفاده از روش OptimizeFor بهینه سازی کنید.
مثال کد زیر نشان می دهد که چگونه از این ویژگی استفاده کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "LayoutInCell.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); | |
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE); | |
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE); | |
watermark.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell. | |
watermark.setWidth(300); | |
watermark.setHeight(70); | |
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); | |
watermark.setVerticalAlignment(VerticalAlignment.CENTER); | |
watermark.setRotation(-40); | |
watermark.getFill().setColor(Color.GRAY); | |
watermark.setStrokeColor(Color.GRAY); | |
watermark.getTextPath().setText("watermarkText"); | |
watermark.getTextPath().setFontFamily("Arial"); | |
watermark.setName("WaterMark_0"); | |
watermark.setWrapType(WrapType.NONE); | |
Run run = (Run) doc.getChildNodes(NodeType.RUN, true).get(doc.getChildNodes(NodeType.RUN, true).getCount() - 1); | |
builder.moveTo(run); | |
builder.insertNode(watermark); | |
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010); | |
// Save the document to disk. | |
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx"; | |
doc.save(dataDir); |