zoukankan      html  css  js  c++  java
  • Java 给Word不同页面设置不同背景

    Word文档中,可直接通过【设计】-【页面颜色】页面颜色,通过Java代码可参考如下设置方法:

    1. 设置单一颜色背景

    doc.getBackground().setType(BackgroundType.Color);
    doc.getBackground().setColor(Color.PINK);

    2. 设置渐变背景

    doc.getBackground().setType(BackgroundType.Gradient);
    doc.getBackground().getGradient().setColor1(Color.white);
    doc.getBackground().getGradient().setColor2(Color.green);

    3. 设置图片背景

    String img= "lye.png";
    Document doc = new Document(input);
    doc.getBackground().setType(BackgroundType.Picture);
    doc.getBackground().setPicture(img);

    但是通过这些方式添加的页面背景只能应用于整个文档页面,如果需要只对某些页面设置不同其他页面的背景,这种方法并不奏效。因此,本文总结了可实现多个页面设置不同背景的方法。

    考虑到只需设置首页背景不同,或者多个页面不同背景的情况,简单分为了两种情况来介绍,但是方法都是类似的。

    程序开发环境:

    1. IDEA

    2. jdk1.8.0

    3.Spire.Doc.jar

    情况1只需设置首页页面背景不同

    【Java】

    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("测试.docx");
    
            //获取第一节
            Section section = doc.getSections().get(0);
    
            //设置首页页眉页脚不同
            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);
    
            //获取页面宽度、高度
            int width = (int) section.getPageSetup().getPageSize().getWidth();
            int height = (int) section.getPageSetup().getPageSize().getHeight();
    
            //设置图片大小,铺满页面
            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("result.docx",FileFormat.Docx_2013);
            doc.dispose();
        }
    }

     

    情况2:设置多个页面背景不同

    需要说明的是,给多个页面设置不同页面是基于不同节上设置的,因此需要在文档中设置分节(插入分节符),这里测试文档中已经设置了多个分节,如果需要代码设置分节可以参考插入分节符的方法:

    Document doc = new Document();
    doc.loadFromFile("测试.docx");
    //在指定段落后添加分节符
    Paragraph paragraph = doc.getSections().get(0).getParagraphs().get(5);
    paragraph.insertSectionBreak(SectionBreakType.No_Break);

    【Java】

    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 DifferentPageBackground2 {
        public static void main(String[] args) {
            //加载Word测试文档
            Document doc = new Document();
            doc.loadFromFile("测试.docx");
    
            //获取第一节中的页眉,添加图片,调整图片格式,铺满页面
            Section section1 = doc.getSections().get(0);
            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);
            int width = (int) section1.getPageSetup().getPageSize().getWidth();
            int height = (int) section1.getPageSetup().getPageSize().getHeight();
            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("result2.docx",FileFormat.Docx_2013);
            doc.dispose();
        }
    }

     

    总结

    对Word中的不同页面设置不同背景,需要几个重要步骤:

    1. 设置文档分节

    2. 设置页眉图片,并调整图片格式以铺满整个页面

    3. 运行程序生成文档

    同理,在设置Word水印时,默认的方法也只能生成一个水印文字效果,要实现水印平铺的效果,也可以通过在页眉中添加文字的方法来实现,需要的可以参考这篇文章,里面介绍了如何来实现,这里不作赘述了。

  • 相关阅读:
    rsync特性
    01 什么是爬虫
    celery的使用
    redis的使用
    GIT使用大全
    多项式的高级运算
    SP1557 GSS2
    题解 CF997E 【Good Subsegments】
    P3920 [WC2014]紫荆花之恋
    题解 P3750 【[六省联考2017]分手是祝愿】
  • 原文地址:https://www.cnblogs.com/Yesi/p/14331364.html
Copyright © 2011-2022 走看看