冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

前面我们介绍了如何使用 Spire.Doc for Java 添加多行文本水印到 Word 文档。该文将详细介绍在Word页眉中添加多行图片水印的效果。

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class WordImageWatermark {
    public static void main(String[] args) throws Exception {
        //加载示例文档
        Document doc=new Document();
        doc.loadFromFile("Sample.docx");
        //加载图片
        DocPicture picture = new DocPicture(doc);
        picture.loadImage("https://cdn.e-iceblue.cn/Logo.png");

        //设置图片环绕方式
        picture.setTextWrappingStyle(TextWrappingStyle.Behind);

        for (int n = 0; n < doc.getSections().getCount(); n++) {
            Section section = doc.getSections().get(n);
            //获取section的页眉
            HeaderFooter header = section.getHeadersFooters().getHeader();
            Paragraph paragrapg1;
            if(header.getParagraphs().getCount()>0){
                paragrapg1=header.getParagraphs().get(0);

            }else {
                //添加段落到页眉
                paragrapg1 = header.addParagraph();
            }

            for (int p = 0; p < 3; p++) {

                for (int q = 0; q < 2; q++) {
                    //复制图片并设置多行多列位置
                    picture = (DocPicture)picture.deepClone();
                    picture.setVerticalPosition(100 + 200 * p);
                    picture.setHorizontalPosition(50 + 210 * q);
                    paragrapg1.getChildObjects().add(picture);
                }
            }
        }
        //保存文档
        doc.saveToFile("多行图片水印.docx", FileFormat.Docx_2013);
    }
}

效果图:

Java 添加多行图片水印到 Word 文档

本文介绍使用Spire.Doc for Java给Word不同页面设置不同背景的方法。可设置纯色背景、图片背景等。设置背景时,可以下两种方法设置,具体见下文。

1、仅设置首页页面背景和其他页面不同

1.1 设置纯色背景

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.documents.VerticalOrigin;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

public class DifferentPageBackground1 {
    public static void main(String[] args) {
        //加载Word测试文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //获取第一节
        Section section = doc.getSections().get(0);

        //获取页面宽度、高度
        float width = (float) section.getPageSetup().getPageSize().getWidth();
        float height = (float) section.getPageSetup().getPageSize().getHeight();

        //设置首页页眉页脚不同
        section.getPageSetup().setDifferentFirstPageHeaderFooter(true);

        //设置首页页面背景颜色
        HeaderFooter firstpageheader = section.getHeadersFooters().getFirstPageHeader();//获取首页页眉
        firstpageheader.getParagraphs().clear();//清除首页页眉默认的段落格式
        Paragraph firstpara= firstpageheader.addParagraph();//重新添加段落
        ShapeObject shape = firstpara.appendShape(width, height, ShapeType.Rectangle);//添加形状
        shape.setBehindText(true);//设置形状衬于文字下方
        shape.setHorizontalAlignment(ShapeHorizontalAlignment.Center);//设置对齐方式,铺满页面
        shape.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        shape.setFillColor(new Color(255,228,196));//形状颜色

        //同理,设置其他页面背景颜色
        HeaderFooter otherheader = section.getHeadersFooters().getHeader();
        otherheader.getParagraphs().clear();
        Paragraph otherpara = otherheader.addParagraph();
        ShapeObject shape2 = otherpara.appendShape(width, height, ShapeType.Rectangle);//添加形状
        shape2.setBehindText(true);//设置形状衬于文字下方
        shape2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);//设置对齐方式,铺满页面
        shape2.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        shape2.setFillColor(new Color(221,160,221));//形状颜色
        
        //保存文档
        doc.saveToFile("ColorBackground.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

Java 给 Word 不同页面设置不同背景

1.2 设置图片背景

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.VerticalOrigin;
import com.spire.doc.fields.DocPicture;


public class DifferentPageBackground1 {
    public static void main(String[] args) {
        //加载Word测试文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //获取第一节
        Section section = doc.getSections().get(0);

        //获取页面宽度、高度
        float width = (float) section.getPageSetup().getPageSize().getWidth();
        float height = (float) section.getPageSetup().getPageSize().getHeight();

        //设置首页页眉页脚不同
        section.getPageSetup().setDifferentFirstPageHeaderFooter(true);

        //设置首页页面背景图片
        HeaderFooter firstpageheader = section.getHeadersFooters().getFirstPageHeader();
        firstpageheader.getParagraphs().clear();
        Paragraph firstpara= firstpageheader.addParagraph();
        DocPicture pic0 = firstpara.appendPicture("1.png");//添加图片到首页页眉段落
        pic0.setTextWrappingStyle(TextWrappingStyle.Behind); 
        pic0.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic0.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic0.setWidth(width);
        pic0.setHeight(height);


        //同理,设置其他页面背景图片
        HeaderFooter otherheader = section.getHeadersFooters().getHeader();
        otherheader.getParagraphs().clear();
        Paragraph otherpara = otherheader.addParagraph();
        DocPicture pic1 = otherpara.appendPicture("2.png");
        pic1.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic1.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic1.setWidth(width);
        pic1.setHeight(height);

        //保存文档
        doc.saveToFile("ImageBackground.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

Java 给 Word 不同页面设置不同背景

2、设置多个页面背景不同

给多个页面设置不同背景时,是基于不同节上的页眉来添加形状或者图片。本次测试文档中,已设置了多个节。如需手动在文档中插入分节符,可参考这篇文章。

2.1 设置纯色背景

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class DifferentPageBackground2 {
    public static void main(String[] args) {
        //加载Word测试文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //获取第一节中的页眉,添加形状,设置颜色,铺满页面
        Section section1 = doc.getSections().get(0); 
        int width = (int) section1.getPageSetup().getPageSize().getWidth();
        int height = (int) section1.getPageSetup().getPageSize().getHeight();
        HeaderFooter header1 = section1.getHeadersFooters().getHeader();
        header1.getParagraphs().clear();
        Paragraph paragraph1 = header1.addParagraph();
        ShapeObject shape1 = paragraph1.appendShape(width,height, ShapeType.Rectangle); 
        shape1.setBehindText(true); 
        shape1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        shape1.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        shape1.setFillColor(new Color(144,238,144)); 

        //获取第二节中的页眉,添加形状
        Section section2 = doc.getSections().get(1);
        HeaderFooter header2 = section2.getHeadersFooters().getHeader();
        header2.getParagraphs().clear();
        Paragraph paragraph2 = header2.addParagraph();
        ShapeObject shape2 = paragraph2.appendShape(width,height, ShapeType.Rectangle);
        shape2.setBehindText(true);
        shape2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        shape2.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        shape2.setFillColor(new Color(255,182,193));

        //获取第三节中的页眉,添加形状
        Section section3 = doc.getSections().get(2);
        HeaderFooter header3 = section3.getHeadersFooters().getHeader();
        header3.getParagraphs().clear();
        Paragraph paragraph3 = header3.addParagraph();
        ShapeObject shape3 = paragraph3.appendShape(width,height, ShapeType.Rectangle);
        shape3.setBehindText(true);
        shape3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        shape3.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        shape3.setFillColor(new Color(135,206,235));

        //保存文档
        doc.saveToFile("ColorBackground2.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

Java 给 Word 不同页面设置不同背景

2.2 设置图片背景

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;

public class DifferentPageBackground2 {
    public static void main(String[] args) {
        //加载Word测试文档
        Document doc = new Document();
        doc.loadFromFile("test.docx");

        //获取第一节中的页眉,添加图片,调整图片格式,铺满页面
        Section section1 = doc.getSections().get(0);
        int width = (int) section1.getPageSetup().getPageSize().getWidth();
        int height = (int) section1.getPageSetup().getPageSize().getHeight();
        HeaderFooter header1 = section1.getHeadersFooters().getHeader();
        header1.getParagraphs().clear();
        Paragraph para1= header1.addParagraph();
        DocPicture pic1 = para1.appendPicture("1.png"); 
        pic1.setTextWrappingStyle(TextWrappingStyle.Behind); 
        pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic1.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic1.setWidth(width);//设置图片宽度、高度等(铺满页面)
        pic1.setHeight(height);

        //同理设置第二节页眉中的图片
        Section section2 = doc.getSections().get(1);
        HeaderFooter header2 = section2.getHeadersFooters().getHeader();
        header2.getParagraphs().clear();
        Paragraph para2= header2.addParagraph();
        DocPicture pic2 = para2.appendPicture("2.png");
        pic2.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic2.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic2.setWidth(width);
        pic2.setHeight(height);

        //同理设置第三节中的页眉中的图片
        Section section3 = doc.getSections().get(2);
        HeaderFooter header3 = section3.getHeadersFooters().getHeader();
        header3.getParagraphs().clear();
        Paragraph para3= header3.addParagraph();
        DocPicture pic3 = para3.appendPicture("3.png");
        pic3.setTextWrappingStyle(TextWrappingStyle.Behind);
        pic3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
        pic3.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic3.setWidth(width);
        pic3.setHeight(height);

        //保存文档
        doc.saveToFile("ImageBackground2.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

Java 给 Word 不同页面设置不同背景

Spire.Presentation for Java 4.1.6已发布。该版本增强了转换PPT 到 图片/PDF/HTML的功能,修复了加载PPT时出现的问题。详情请阅读以下内容。

问题修复:


获取Spire.Presentation for Java 4.1.6,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html

PDF 注释是添加到文档中的注解或标记,可以用于添加评论、给出解释、给予反馈等。文档共同创作者之间经常用注释就文档内容进行沟通。然而,当与注释相关的问题已经处理完毕或文件已经定稿时,就有必要删除文档中的注释,使文件更加简洁和专业。本文将介绍如何使用 Spire.PDF for Java 通过程序删除 PDF 注释。

安装 Spire.PDF for Java

首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.9.0</version>
    </dependency>
</dependencies>

移除指定 PDF 注释

注释是页面级的文档元素,因此删除指定注释需要先获取该注释所在的页面,然后再使用 PdfPageBase.getAnnotationsWidget().removeAt() 方法删除该注释。详细操作步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class RemoveAnnotation {
    public static void main(String[] args) {

        //创建PdfDocument的对象
        PdfDocument pdf = new PdfDocument();

        //载入PDF文档
        pdf.loadFromFile("C:/注释.pdf");

        //获取文档第一页
        PdfPageBase page = pdf.getPages().get(0);

        //删除第一个注释
        page.getAnnotationsWidget().removeAt(0);

        //保存文档
        pdf.saveToFile("删除注释.pdf");
    }
}

Java 删除 PDF 中的注释

移除指定 PDF 页面的所有注释

Spire.PDF for Java 中的 PdfPageBase.getAnnotationsWidget().clear() 方法可以删除指定页面的所有注释。详细操作步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class RemoveAllAnnotationPage {
    public static void main(String[] args) {

        //创建PdfDocument的对象
        PdfDocument pdf = new PdfDocument();

        //载入PDF文档
        pdf.loadFromFile("C:/注释.pdf");

        //获取文档第一页
        PdfPageBase page = pdf.getPages().get(0);

        //删除该页所有注释
        page.getAnnotationsWidget().clear();

        //保存文档
        pdf.saveToFile("删除一页所有注释.pdf");
    }
}

Java 删除 PDF 中的注释

移除 PDF 文档的所有注释

在删除 PDF 文档中的所有注释时,我们需要循环遍历文档中的所有页面并删除每一页的所有注释。详细步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class RemoveAllAnnotations {
    public static void main(String[] args) {

        //创建PdfDocument的对象
        PdfDocument pdf = new PdfDocument();

        //载入PDF文档
        pdf.loadFromFile("C:/注释.pdf");

        //循环遍历文档中的页面
        for (PdfPageBase page : (Iterable) pdf.getPages()) {
            //删除每页的所有注释
            page.getAnnotationsWidget().clear();
        }

        //保存文档
        pdf.saveToFile("删除所有注释.pdf");
    }
}

Java 删除 PDF 中的注释

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

本文介绍如何使用Spire.PDF for Java获取PDF中图片的坐标信息。

import com.spire.pdf.exporting.PdfImageInfo;
import java.awt.geom.Rectangle2D;

public class GetCoordinateOfImage {

    public static void main(String[] args) {

        //创建PdfDocument对象
        PdfDocument doc = new PdfDocument();

        //加载一个PDF文档
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Images.pdf");

        //获取第一页
        PdfPageBase page = doc.getPages().get(0);

        //获取第一页上的图片信息
        PdfImageHelper imageHelper = new PdfImageHelper();
        PdfImageInfo[] imageInfo = imageHelper.getImagesInfo(page);

        //遍历图片信息
        for (int i = 0; i < imageInfo.length; i++) {

            //获取指定图片的边界属性
            Rectangle2D rect = imageInfo[i].getBounds();
            
            //获取左上角坐标
            System.out.println(String.format("第%d张图片的左上角坐标为:(%f, %f)", i+1, rect.getX(), rect.getY()));
        }
    }
}

Java 获取 PDF 中图片的坐标信息

在PPT中,当多个形状重叠排列时,可设置形状置于顶层或底层、设置形状上移一层或下移一层。下面通过使用Spire.Presentation for Java来演示如何实现。

测试文档如下,需要将图片形状置于顶层:

Java 设置 PPT 中的形状排列方式

import com.spire.presentation.*;

public class ShapeArrange {
    public static void main(String[] args) throws Exception{
        //加载PPT测试文档
        Presentation ppt = new Presentation();
        ppt.loadFromFile("test.pptx");

        //获取指定幻灯片及形状
        ISlide slide = ppt.getSlides().get(1);
        IShape shape1 =  slide.getShapes().get(0);

        shape1.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.BringToFront);//置于顶层
        //shape1.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.BringForward);//上移一层
        //shape1.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);//置于底层
        //shape1.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendBackward);//下移一层

        //保存文档
        ppt.saveToFile("result.pptx",FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

设置效果:

Java 设置 PPT 中的形状排列方式

Spire.Office 6.1已正式发布。本次更新带了一系列新的功能,例如:Spire.XLS 支持计算 MS Excel2019 新增公式,增加了在气泡图中不显示图表标题和转换Shape到图片等新功能;Spire.PDF 支持了自定义签名图片的功能;Spire.Presentation 支持嵌入 zip 对象到 PPTX 文件;Spire.PDFViewer 支持了显示页面标签的功能等等。此外,该版本还修复了大量问题。详情请阅读以下内容。

该版本涵盖了最新版的Spire.Doc, Spire.PDF, Spire.XLS, Spire.Presentation, Spire.Email, Spire.DocViewer, Spire.PDFViewer, Spire.Spreadsheet, Spire.OfficeViewer, Spire.DataExport, Spire.Barcode。

版本信息如下:

获取Spire.Office 6.1, 请点击:

https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html


Spire.XLS

功能优化:

新功能:

问题修复:


Spire.Doc

问题修复:


Spire.PDF

新功能:

问题修复:


Spire.Presentation

新功能:

问题修复:


Spire.Email

调整变化:


Spire.PDFViewer

新功能:

问题修复:


Spire.Barcode

新功能:

问题修复:

Spire.XLS 11.1已发布。该本优化了转换Excel到HTML的速度,支持计算MS Excel2019的新增公式,增加了在气泡图中不显示图表标题和转换Shape到图片等新功能。此外,该版本还增强了转换Excel到PDF/HTML的功能并修复了加载保存文档时出现的问题。详情请阅读以下内容。

功能优化:

新功能:

问题修复:


下载Spire.XLS 11.1,请点击:

https://www.e-iceblue.cn/Downloads/Spire-XLS-NET.html

Spire.Barcode 5.1.4已发布。该本新增了BarcodeScan类实现可以创建不同的实例去调用Scan方法,修复了调用BarcodeScanner.Scan方法时出现的问题。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.Barcode 5.1.4 请点击:

https://www.e-iceblue.cn/Downloads/Spire-Barcode-NET.html

Spire.Doc for Java 4.1.3已发布。该本新增了锁住内容控件的功能,增强了转换Word到PDF/HTML/SVG格式的功能,修复了加载Doc文档时出现的问题。详情请阅读以下信息。

新功能:

问题修复:


获取Spire.Doc for Java 4.1.3,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-JAVA.html