栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 使用 aspose 添加装订线

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 使用 aspose 添加装订线

java 使用 aspose 添加装订线代码如下

 
    @Test
    public void testSaveWordZhuangDingXian() throws Exception {
        java.io.File file = new java.io.File( Thread.class.getResource( "/" ).getPath() );
        String path = file.getParentFile().getPath();
        path = java.net.URLDecoder.decode( path, "UTF-8" );

        System.out.println( path );

        //1 创建 空白文档
        DocumentBuilder builder = new DocumentBuilder();
        Document doc = builder.getDocument();

        //2 设定 word 页面样式
        builder.getPageSetup().setPaperSize( PaperSize.A4 );//A4纸
        //builder.getPageSetup().setOrientation(Orientation.PORTRAIT);//方向
        //builder.getPageSetup().setVerticalAlignment(PageVerticalAlignment.TOP); //垂直对准
        //builder.getPageSetup().setLeftMargin(42);//页面左边距
        // builder.getPageSetup().setRightMargin(42);//页面右边距


        //3  页眉
        HeaderFooter header = new HeaderFooter( doc, HeaderFooterType.HEADER_PRIMARY );
        doc.getFirstSection().getHeadersFooters().add( header );

        Paragraph para = new Paragraph( doc );
        Run run = new Run( doc, "测试页眉" );
        run.getFont().setSize( 9.0 );
        para.appendChild( run );

        //4 装订线 start : 插入文本框  10.5 5号字体  ,643.8/10.5=61个汉字?  60个汉字
        double height = 643.8;
        Shape shape = new Shape( doc, ShapeType.TEXT_BOX );
        //文本框大小位置
        shape.setWidth( 76.95 );
        shape.setHeight( height );
        shape.setTop( 42.5 );
        shape.setLeft( -86.0 );
        shape.setBounds( new Rectangle2D.Float( -86.0f, 42.5f, 76.95f, 643.8f ) );
        shape.setFilled( false );
        shape.setFillColor( new java.awt.Color( 255, 255, 255 ) );
        shape.getFill().setOn( false );
        shape.getFill().setOpacity( 1.0 );
        shape.getFill().setColor( new java.awt.Color( 255, 255, 255 ) );
        shape.setStroked( false );
        shape.setStrokeColor( new java.awt.Color( 0, 0, 0 ) );
        shape.setFlipOrientation( FlipOrientation.NONE );
        shape.setWrapType( WrapType.SQUARE ); //2
        shape.setBehindText( false );
        shape.setRelativeHorizontalPosition( RelativeHorizontalPosition.COLUMN );//2
        shape.setHorizontalAlignment( HorizontalAlignment.NONE );//0
        shape.setRelativeVerticalPosition( RelativeVerticalPosition.PARAGRAPH );//2
        shape.setVerticalAlignment( VerticalAlignment.NONE );//0
        //文本框文字方向  :BOTTOM_TO_TOP 从底网上
        shape.getTextBox().setLayoutFlow( LayoutFlow.BOTTOM_TO_TOP );

        //空行
        String startLine = "……………○……………外……………○……………装……………○……………订……………○……………线……………○………………";
        Paragraph para0 = new Paragraph( doc );
        para0.getParagraphFormat().setAlignment( ParagraphAlignment.JUSTIFY );//两端对齐
        Run run0 = new Run( doc, startLine );
        run0.getFont().setName( "宋体" );
        run0.getFont().setSize( 10.5 );//5号字体
        para0.appendChild( run0 );


        String spaceLine = "                    ";
        Paragraph para1 = new Paragraph( doc );
        para1.getParagraphBreakFont().setSize( 10.5 );//5号字体
        //para1.getParagraphFormat().setFirstLineIndent(105.0);//首行缩进10个字符   105.0=10*10.5  5号字体
        para1.getParagraphFormat().setFirstLineIndent( 33 );//首行缩进10个字符   105.0=10*10.5  5号字体
        para1.getParagraphFormat().setAlignment( ParagraphAlignment.JUSTIFY );//两端对齐
        Run run1 = new Run( doc, "学校:" + spaceLine + "姓名:" + spaceLine + "班级:" + spaceLine + "考号:" );
        run1.getFont().setName( "宋体" );
        run1.getFont().setSize( 10.5 );//5号字体
        para1.appendChild( run1 );

        //空行
        Paragraph para2 = new Paragraph( doc );
        para2.getParagraphBreakFont().setSize( 9.0 );//小五号字体
        para2.getParagraphBreakFont().setName( "宋体" );
        para2.getParagraphFormat().setAlignment( ParagraphAlignment.JUSTIFY );//两端对齐

        //空行
        Paragraph para3 = new Paragraph( doc );
        para3.getParagraphBreakFont().setSize( 9.0 );//小五号字体
        para3.getParagraphBreakFont().setName( "宋体" );
        //Run run3 = new Run(doc,s3);
        //run3.getFont().setName("宋体");
        //run3.getFont().setSize(10.5);//5号字体
        //para3.appendChild(run3);
        para3.getParagraphFormat().setAlignment( ParagraphAlignment.JUSTIFY );//两端对齐

        //装订线
        //String endLine = "……………………………………装订线…………………………………………………………………装订线………………………………………";
        String endLine = "……………○……………内……………○……………装……………○……………订……………○……………线……………○………………";
        Paragraph para4 = new Paragraph( doc );
        para4.getParagraphFormat().setAlignment( ParagraphAlignment.JUSTIFY );//两端对齐
        Run run4 = new Run( doc, endLine );
        run4.getFont().setName( "宋体" );
        run4.getFont().setSize( 10.5 );//5号字体
        para4.appendChild( run4 );

        shape.appendChild( para0 );
        shape.appendChild( para1 );
        shape.appendChild( para2 );
        shape.appendChild( para3 );
        shape.appendChild( para4 );

        //画线条,一个线条是一个shape,线条在文本框文字下面
        double lineHeight = 99.2;
        double lineLeft = -45.0;
        Shape shapeLine1 = new Shape( doc, ShapeType.LINE );
        shapeLine1.getFont().setSize( 9.0 );//小五
        shapeLine1.setWidth( 0.0 );
        shapeLine1.setHeight( lineHeight );
        //shapeLine1.setTop(134.9);
        shapeLine1.setTop( 112.9 );
        shapeLine1.setLeft( lineLeft );

        Shape shapeLine2 = new Shape( doc, ShapeType.LINE );
        shapeLine2.getFont().setSize( 9.0 );//小五
        shapeLine2.setWidth( 0.0 );
        shapeLine2.setHeight( lineHeight );
        //shapeLine2.setTop(292.1);
        shapeLine2.setTop( 250.1 );
        shapeLine2.setLeft( lineLeft );

        Shape shapeLine3 = new Shape( doc, ShapeType.LINE );
        shapeLine3.getFont().setSize( 9.0 );//小五
        shapeLine3.setWidth( 0.0 );
        shapeLine3.setHeight( lineHeight );
        // shapeLine3.setTop(440.3);
        shapeLine3.setTop( 388.2 );
        shapeLine3.setLeft( lineLeft );

        Shape shapeLine4 = new Shape( doc, ShapeType.LINE );
        shapeLine4.getFont().setSize( 9.0 );//小五
        shapeLine4.setWidth( 0.0 );
        shapeLine4.setHeight( lineHeight );
        shapeLine4.setTop( 524.3 );
        shapeLine4.setLeft( lineLeft );

        para.appendChild( shape );
        para.appendChild( shapeLine1 );
        para.appendChild( shapeLine2 );
        para.appendChild( shapeLine3 );
        para.appendChild( shapeLine4 );

        // Add a paragraph with text to the footer.
        header.appendChild( para );

        //装订线 end


        //5 写入文字
        //5.1 插入文本
        Font font = builder.getFont();
        font.setSize( 10.5 );
        font.setBold( true );
        font.setColor( Color.BLACK );
        font.setName( "黑体" );
        builder.writeln( "保密★启用前" );


        //5.2 写入一段文字
        //获取ParagraphFormat对象,关于行的样式基本都在这里
        ParagraphFormat paragraphFormat = builder.getParagraphFormat();
        //文字对齐方式
        paragraphFormat.setAlignment( ParagraphAlignment.CENTER );
        // 单倍行距 = 12 , 1.5 倍 = 18
        paragraphFormat.setLineSpacing( 12 );

        //获取Font对象,关于文字的大小,颜色,字体等等基本都在这个里面
        Font font1 = builder.getFont();
        //字体大小
        font1.setSize( 22 );
        //是否粗体
        font.setBold( true );
        //下划线样式,None为无下划线
        font.setUnderline( Underline.NONE );
        //字体颜色
        font.setColor( Color.BLACK );
        //font.Color = System.Drawing.ColorTranslator.FromHtml("#3b3131");//自定义颜色
        //设置字体
        font.setNameFarEast( "宋体" );
        //添加文字
        builder.write( "2021-2022学年度高中数学5月月考卷" );
        //添加回车
        builder.writeln();
        //添加文字后回车
        builder.writeln( "添加的文字后回车" );


        //6、添加表格
        //开始添加表格
        //获取Font对象,关于文字的大小,颜色,字体等等基本都在这个里面
        Font font2 = builder.getFont();
        //字体大小
        font2.setSize( 10.5 );
        //是否粗体
        font2.setBold( true );
        //下划线样式,None为无下划线
        font2.setUnderline( Underline.NONE );
        //字体颜色
        font2.setColor( Color.BLACK );

        Table table = builder.startTable();
        // Insert a cell
        builder.insertCell();
        //AutoFitBehavior.AUTO_FIT_TO_CONTENTS 根据内容调整表格
        // AUTO_FIT_TO_WINDOW 根据窗口调整表格
        // FIXED_COLUMN_WIDTHS  固定列宽
        table.autoFit( AutoFitBehavior.AUTO_FIT_TO_CONTENTS );
        builder.getCellFormat().setVerticalAlignment( CellVerticalAlignment.CENTER );
        builder.write( "评卷人" );
        builder.insertCell();
        builder.write( "得分" );
        builder.insertCell();
        builder.write( "一、选择题" );
        // End row
        builder.endRow();
        // start a next row and set its properties
        builder.getRowFormat().setHeight( 20 );
        builder.getRowFormat().setHeightRule( HeightRule.EXACTLY );
        builder.insertCell();
        builder.write( "" );
        builder.insertCell();
        builder.write( "" );
        builder.insertCell();
        builder.write( "" );
        builder.endRow();
        // End table
        builder.endTable();

        //设置边框的颜色
        //table.setBorder(BorderType.LEFT, LineStyle.DOUBLE, 1, Color.WHITE, false);

        Cell nodes = table.getRows().get( 0 ).getCells().get( 2 );
        Cell nodes1 = table.getRows().get( 1 ).getCells().get( 2 );
        //合并单元格
        mergeCells( nodes, nodes1 );


        //插入文字
        ParagraphFormat paragraphFormat2 = builder.getParagraphFormat();
        //文字对齐方式
        paragraphFormat2.setAlignment( ParagraphAlignment.LEFT );
        // 单倍行距 = 12 , 1.5 倍 = 18
        paragraphFormat2.setLineSpacing( 12 );

        Font font3 = builder.getFont();
        font3.setSize( 10.5 );
        font3.setBold( true );
        font3.setColor( Color.BLACK );
        font3.setName( "宋体" );
        builder.writeln( "一、选择题" );


        //创建页脚 页码
        HeaderFooter footer = new HeaderFooter( doc, HeaderFooterType.FOOTER_PRIMARY );
        doc.getFirstSection().getHeadersFooters().add( footer );

        //页脚段落
        Paragraph footerpara = new Paragraph( doc );
        footerpara.getParagraphFormat().setAlignment( ParagraphAlignment.CENTER );

        Run footerparaRun = new Run( doc, "共 " );
        footerparaRun.getFont().setName( "宋体" );
        footerparaRun.getFont().setSize( 9.0 );//小5号字体
        footerpara.appendChild( footerparaRun );

        footerpara.appendField( FieldType.FIELD_NUM_PAGES, true );//总页码

        footerparaRun = new Run( doc, " 页,第 " );
        footerparaRun.getFont().setName( "宋体" );
        footerparaRun.getFont().setSize( 9.0 );//小5号字体
        footerpara.appendChild( footerparaRun );

        footerpara.appendField( FieldType.FIELD_PAGE, true );//当前页码

        footerparaRun = new Run( doc, " 页" );
        footerparaRun.getFont().setName( "宋体" );
        footerparaRun.getFont().setSize( 9.0 );//小5号字体
        footerpara.appendChild( footerparaRun );

        footer.appendChild( footerpara );


        doc.save( "D:\temp\页眉页脚.docx" );


    }

合并列

    public void mergeCells(Cell startCell, Cell endCell) {
        Table parentTable = startCell.getParentRow().getParentTable();
        // Find the row and cell indices for the start and end cell.
        Point startCellPos = new Point( startCell.getParentRow().indexOf( startCell ), parentTable.indexOf( startCell.getParentRow() ) );
        Point endCellPos = new Point( endCell.getParentRow().indexOf( endCell ), parentTable.indexOf( endCell.getParentRow() ) );
        // Create the range of cells to be merged based off these indices. Inverse each index if the end cell if before the start cell.
        Rectangle mergeRange = new Rectangle( Math.min( startCellPos.x, endCellPos.x ), Math.min( startCellPos.y, endCellPos.y ), Math.abs( endCellPos.x - startCellPos.x ) + 1,
                Math.abs( endCellPos.y - startCellPos.y ) + 1 );
        for (Row row : parentTable.getRows()) {
            for (Cell cell : row.getCells()) {
                Point currentPos = new Point( row.indexOf( cell ), parentTable.indexOf( row ) );

                // Check if the current cell is inside our merge range then merge it.
                if (mergeRange.contains( currentPos )) {
                    if (currentPos.x == mergeRange.x)
                        cell.getCellFormat().setHorizontalMerge( CellMerge.FIRST );
                    else
                        cell.getCellFormat().setHorizontalMerge( CellMerge.PREVIOUS );

                    if (currentPos.y == mergeRange.y)
                        cell.getCellFormat().setVerticalMerge( CellMerge.FIRST );
                    else
                        cell.getCellFormat().setVerticalMerge( CellMerge.PREVIOUS );
                }
            }
        }
    }

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017075.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号