page 117

We have demonstrated how to add text and image header footer to Word document by using Spire.Doc for Java. This article will show you how to create different headers/footers for odd and even pages on Word document in Java applications.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class oddAndEvenHeaderFooter {
    public static void main(String[] args) throws Exception {

        String input = "multiPages.docx";
        String output = "output/oddAndEvenHeaderFooter.docx";

        //load the document
        Document doc = new Document();
        doc.loadFromFile(input);

        //get the first section
        Section section = doc.getSections().get(0);

        //set the DifferentOddAndEvenPagesHeaderFooter property as true
        section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);

        //add odd header
        Paragraph P3 = section.getHeadersFooters().getOddHeader().addParagraph();
        TextRange OH = P3.appendText("Odd Header");
        P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OH.getCharacterFormat().setFontName("Arial");
        OH.getCharacterFormat().setFontSize(14);
        OH.getCharacterFormat().setTextColor(Color.BLUE);

        //add even header
        Paragraph P4 = section.getHeadersFooters().getEvenHeader().addParagraph();
        TextRange EH = P4.appendText("Even Header from E-iceblue Using Spire.Doc");
        P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EH.getCharacterFormat().setFontName("Arial");
        EH.getCharacterFormat().setFontSize(14);
        EH.getCharacterFormat().setTextColor(Color.GREEN);

        //add odd footer
        Paragraph P2 = section.getHeadersFooters().getOddFooter().addParagraph();
        TextRange OF = P2.appendText("Odd Footer");
        P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OF.getCharacterFormat().setFontName("Arial");
        OF.getCharacterFormat().setFontSize(14);
        OF.getCharacterFormat().setTextColor(Color.BLUE);

        //add even footer
        Paragraph P1 = section.getHeadersFooters().getEvenFooter().addParagraph();
        TextRange EF = P1.appendText("Even Footer from E-iceblue Using Spire.Doc");
        EF.getCharacterFormat().setFontName("Arial");
        EF.getCharacterFormat().setFontSize(14);
        P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EF.getCharacterFormat().setTextColor(Color.GREEN);

        //save the document
        doc.saveToFile(output, FileFormat.Docx);

    }
}

Output:

Java Add different headers/footers for odd and even pages on Word

This article shows you how to create a chart in PowerPoint using the data from an existing Excel document. This solution relies on Spire.Office.jar. Please download the latest version from here and add it as a dependency in your project.

Below is a screenshot of the Excel document.

Create PowerPoint Chart from Excel Data in Java

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

import java.awt.geom.Rectangle2D;

public class CreateChartFromExcelData {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Add a clustered column chart to slide
        Rectangle2D rect = new Rectangle2D.Float(200, 100, 550, 320);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED,rect);

        //Clear the default dummy data
        chart.getChartData().clear(0,0,5,5 );

        //Load an existing Excel file to Workbook object
        Workbook wb = new Workbook();
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");

        //Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Import data from the sheet to chart table
        for (int r = 0; r < sheet.getAllocatedRange().getRowCount(); r++)
        {
            for (int c = 0; c < sheet.getAllocatedRange().getColumnCount(); c++)
            {
                chart.getChartData().get(r,c).setValue(sheet.getCellRange(r+1, c+1).getValue2());
            }
        }

        //Add chart title
        chart.getChartTitle().getTextProperties().setText("Male/Female Ratio Per Dept.");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(25f);
        chart.hasTitle(true);

        //Set the series label
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","C1"));

        //Set the category labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2","A5"));

        //Set the series values
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2","B5"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C5"));

        //Apply built-in chart style
        chart.setChartStyle(ChartStyle.STYLE_11);

        //Set overlap
        chart.setOverLap(-50);

        //Set gap width
        chart.setGapWidth(200);

        //Save to file
        presentation.saveToFile("output/Chart.pptx", FileFormat.PPTX_2013);
    }
}

Create PowerPoint Chart from Excel Data in Java

Save PowerPoint Shapes as Images in Java

2021-03-30 02:37:10 Written by Koohji

This article shows you how to export shapes in a specific slide as images using Spire.Presentation for Java. Below is a screenshot of the sample PowerPoint document.

Save PowerPoint Shapes as Images in Java

import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class SaveShapeAsImage {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();

        //Load the sample PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\chart and table.pptx");

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Declare a BufferedImage variable
        BufferedImage image;

        //Loop through the shapes in the slide
        for (int i = 0; i < slide.getShapes().getCount(); i++) {

            //Save the specific shape as image data
            image = slide.getShapes().saveAsImage(i);

            //Write data to png file
            File file = new File(String.format("ToImage-%d.png", i));
            ImageIO.write(image, "PNG", file);
        }
    }
}

Output

Save PowerPoint Shapes as Images in Java

page 117