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

Spire.Cloud 纯前端文档控件

在 Word 文档中,缩进是一种段落格式,用于调整段落正文和页边距之间的距离。 它包括左缩进、右缩进、首行缩进和悬挂缩进。 左缩进和右缩进可以应用于段落的所有行,而首行缩进只能应用于段落的第一行。 至于悬挂缩进,它可以应用于除第一行之外的段落的每一行。在本文中,您将学习如何使用 Spire.Doc for .NET 以编程方式在 Word 文档中设置段落缩进。

安装 Spire.Doc for .NET

首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。

PM> Install-Package Spire.Doc

设置 Word 文档段落缩进

下表列出了一些用于在 Word 文档中设置不同段落缩进的核心类和属性。

名称 描述
ParagraphFormat 类 表示段落的格式。
ParagraphFormat.LeftIndent 属性 返回或设置表示段落左缩进的值。
ParagraphFormat.RightIndent 属性 返回或设置表示段落右缩进的值。
ParagraphFormat.FirstLineIndent 属性 获取或设置第一行或悬挂缩进的值;正值代表首行缩进,负值代表悬挂缩进。

详细步骤如下:

  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordIndent
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Document实例。
            Document doc = new Document();

            //加载示例 Word 文档
            doc.LoadFromFile("编程语言发展历程.docx");

            //获取第一段并设置左缩进
            Paragraph para1 = doc.Sections[0].Paragraphs[0];
            para1.Format.LeftIndent = 30;

            //获取第二段并设置右缩进
            Paragraph para2 = doc.Sections[0].Paragraphs[1];
            para2.Format.RightIndent = 30;

            //获取第三段并设置首行缩进
            Paragraph para3 = doc.Sections[0].Paragraphs[2];
            para3.Format.FirstLineIndent = 30;

            //获取第四段并设置悬挂缩进
            Paragraph para4 = doc.Sections[0].Paragraphs[3];
            para4.Format.FirstLineIndent = -30;

            //保存文档
            doc.SaveToFile("缩进文档.docx", FileFormat.Docx2010);
        }
    }
}
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordIndent
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '创建一个Document实例。
            Dim doc As Document = New Document()

            '加载示例 Word 文档
            doc.LoadFromFile("编程语言发展历程.docx")

            '获取第一段并设置左缩进
            Dim para1 As Paragraph = doc.Sections(0).Paragraphs(0)
            para1.Format.LeftIndent = 30

            '获取第二段并设置右缩进
            Dim para2 As Paragraph = doc.Sections(0).Paragraphs(1)
            para2.Format.RightIndent = 30

            '获取第三段并设置首行缩进
            Dim para3 As Paragraph = doc.Sections(0).Paragraphs(2)
            para3.Format.FirstLineIndent = 30

            '获取第四段并设置悬挂缩进
            Dim para4 As Paragraph = doc.Sections(0).Paragraphs(3)
            para4.Format.FirstLineIndent = -30

            '保存文档
            doc.SaveToFile("缩进文档.docx", FileFormat.Docx2010)
        End Sub
    End Class
End Namespace

C#/VB.NET 设置 Word 文档段落缩进

申请临时 License

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

Spire.Doc 10.9.1 已发布。该版本增强了 Word 到 PDF 和 PDFA1A 的转换。此外,该版本还修复了统计的字符、单词数量与微软Word结果不一致的问题。详情请阅读以下内容。

问题修复:


获取Spire.Doc 10.9.1请点击:

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

Spire.Doc for Java 10.9.0 已发布。此版本增强了 Word 到 PDF 的转换功能。同时此版本还修复了一些已知问题,如:修复了DOC转DOCX2007时,内容对齐不一致等问题。详情请阅读以下内容。

问题修复:


获取Spire.Doc for Java 10.9.0请点击:

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

在日常工作中,我们可能经常面临更改 PDF 页面大小的情况。 例如,一个包含不同大小页面的合并 PDF 文件,我们需要将页面大小调整为相同大小以便于阅读和打印。 在本文中,将详细为您介绍如何使用 Spire.PDF for Java 在 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>11.11.11</version>
    </dependency>
</dependencies>

将 PDF 页面大小更改为标准纸张大小

更改 PDF 文件页面大小的方法是创建一个新的 PDF 文件并向其添加所需大小的页面,然后从原 PDF 文件中的页面创建模板,然后将模板绘制到 新的 PDF 文件。 此过程将保留原 PDF 中存在的文本、图像和其他元素。

Spire.PDF for Java 支持各种标准纸张尺寸,如 letter、legal、A0、A1、A2、A3、A4、B0、B1、B2、B3、B4 等等。 以下步骤将向您展示如何将 PDF 文件的页面大小更改为标准纸张大小。

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;

import java.awt.geom.Point2D;

public class ChangePageSizeToStandardPaperSize {
    public static void main(String []args){
        //加载原 PDF 文档
        PdfDocument originPdf = new PdfDocument();
        originPdf.loadFromFile("都江堰.pdf");

        //创建一个新的PDF文档
        PdfDocument newPdf = new PdfDocument();

        //循环遍历原 PDF 中的页面
        for(int i = 0; i< originPdf.getPages().getCount(); i++)
        {
            //将A1大小页面添加到新的 PDF 文件        
    PdfPageBase newPage = newPdf.getPages().add(PdfPageSize.A1, new PdfMargins((0)));
            //初始化一个 PdfTextLayout 实例
            PdfTextLayout layout = new PdfTextLayout();
            //将文本布局设置为一页(如果不设置,内容将无法适应页面大小)
            layout.setLayout(PdfLayoutType.One_Page);
            //基于原始PDF中的页面创建模板
            PdfTemplate template = originPdf.getPages().get(i).createTemplate();
            //在新的PDF中将模板绘制到页面上
            template.draw(newPage, new Point2D.Float(0,0), layout);
        }

        //保存结果文档
        newPdf.saveToFile("更改页面至标准纸张大小.pdf");
    }
}

Java 更改 PDF 页面大小

将 PDF 页面大小更改为自定义纸张大小

Spire.PDF for Java 使用 point(1/72 英寸)作为度量单位。 如果要将 PDF 的页面大小更改为以英寸或毫米等其他度量单位表示的自定义纸张大小,可以使用 PdfUnitConvertor 类将它们转换为 points。

以下步骤将向您展示如何将 PDF 文件的页面大小更改为以英寸为单位的自定义纸张大小:

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

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class ChangePageSizeToCustomPaperSize {
    public static void main(String []args){
        //加载原 PDF 文件
        PdfDocument originPdf = new PdfDocument();
        originPdf.loadFromFile("都江堰.pdf");

        //初始化一个新的 PdfDocument 实例
        PdfDocument newPdf = new PdfDocument();

        //初始化 PdfUnitConvertor 实例
        PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
        //将自定义大小以英寸为单位转换为points
        float width = unitCvtr.convertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
        float height = unitCvtr.convertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);

        //从自定义大小创建一个 Dimension2D 实例,然后它将用作新 PDF 的页面大小
        Dimension2D size = new Dimension();
        size.setSize(width, height);

        //循环遍历原PDF 中的页面
        for(int i = 0; i< originPdf.getPages().getCount(); i++)
        {
            //将自定义大小的页面添加到新的 PDF 文件
            PdfPageBase newPage = newPdf.getPages().add(size, new PdfMargins((0)));
            //创建一个 PdfTextLayout 实例
            PdfTextLayout layout = new PdfTextLayout();
            //将文本布局设置为一页(如果未设置内容将不会缩放以适应页面大小)
            layout.setLayout(PdfLayoutType.One_Page);
            //根据原 PDF 中的页面创建模板
            PdfTemplate template = originPdf.getPages().get(i).createTemplate();
            //在新 PDF 的页面上绘制模板
            template.draw(newPage, new Point2D.Float(0,0), layout);
        }

        //保存结果文档
        newPdf.saveToFile("更改页面至自定义纸张大小.pdf");
    }
}

Java 更改 PDF 页面大小

申请临时 License

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

Spire.PDF for Android via Java 8.8.1 已发布。本次更新修复了 PDF 转 DOCX 后图片丢失的问题和 PDF 转 DOCX 时出现空指针异常的问题。详情请阅读以下内容。

问题修复:


获取Spire.PDF for Android via Java 8.8.1请点击:

https://www.e-iceblue.cn/Downloads/pdf-for-android-via-java.html

Spire.SpreadSheet 6.8 已发布。本次更新修复了 Worksheet.Resize 方法无效的问题。详情请阅读以下内容。

问题修复:


获 取Spire.Spreadsheet 6.8 请点击:

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

Spire.XLS for Java 12.8.4已发布。此版本支持通过Worksheet.getMaxDispalyRange()方法获取所有单元格范围,包含其中的图片、形状等对象,并且支持=Days() 公式。此外,本次更新还增强了Excel 到 PDF的转换功能。一些已知问题也得到了修复,如修复了应用获取图表DataRange时抛出“NullPointerException”等问题。详情请阅读以下内容。

新功能:

问题修复:


下载Spire.XLS for Java 12.8.4 请点击:

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

Tab 1

此演示向您展示如何在 PowerPoint 文档中创建图表。

Data

e-iceblue

Option

e-iceblue
downloads

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

package ppt;

import com.spire.data.table.DataColumn;
import com.spire.data.table.DataRow;
import com.spire.data.table.DataTable;
import com.spire.pdf.tables.table.DataTypes;
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;

import java.awt.geom.Rectangle2D;

public class ChartDemo {
    public void chartDemoPpt(String resultFilePath, ChartType chartType) throws Exception {
        Presentation presentation = new Presentation();
        Rectangle2D rect1 = new Rectangle2D.Double(90, 100, 550, 320);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(chartType, rect1, false);
        //chart title
        chart.getChartTitle().getTextProperties().setText("Chart");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle( true);
        DataTable dataTable = getDataTable();
        insertDatatableToChart(chart,dataTable);
        //set series label
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
        System.out.println(chart.getSeries().size());
       if (chartType.getName().contains("Scatter") || chartType.getName().contains("Bubble")){
            chart.getSeries().get(0).setXValues(chart.getChartData().get("A2", "A7"));
            chart.getSeries().get(0).setYValues(chart.getChartData().get("B2", "B7"));
            chart.getSeries().get(1).setYValues(chart.getChartData().get("C2", "C7"));
            chart.getSeries().get(2).setYValues(chart.getChartData().get("D2", "D7"));
            if (chartType.getName().contains("Bubble")){
                for (int i = 0; i < chart.getSeries().size();i++){
                    chart.getSeries().get(i).getBubbles().add(1);
                    chart.getSeries().get(i).getBubbles().add(4);
                    chart.getSeries().get(i).getBubbles().add(3);
                    chart.getSeries().get(i).getBubbles().add(4);
                    chart.getSeries().get(i).getBubbles().add(2);
                    chart.getSeries().get(i).getBubbles().add(9);
                }
            }
        }else {
            //set category label
            chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
            //set values for series
            chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
            chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
            chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
            if (chartType.getName().contains("3D")){
                chart.getRotationThreeD().setXDegree(10);
                chart.getRotationThreeD().setYDegree(10);
            }
        }
        presentation.saveToFile(resultFilePath, FileFormat.PPTX_2013);

    }

    private DataTable getDataTable() throws Exception {
        DataTable dataTable = new DataTable();
        dataTable.getColumns().add(new DataColumn("SalesPers", DataTypes.DATATABLE_STRING));
        dataTable.getColumns().add(new DataColumn("SaleAmt", DataTypes.DATATABLE_INT));
        dataTable.getColumns().add(new DataColumn("ComPct", DataTypes.DATATABLE_INT));
        dataTable.getColumns().add(new DataColumn("ComAmt", DataTypes.DATATABLE_INT));
        DataRow row1 = dataTable.newRow();
        row1.setString("SalesPers", "Joe");
        row1.setInt("SaleAmt", 250);
        row1.setInt("ComPct", 150);
        row1.setInt("ComAmt", 99);
        DataRow row2 = dataTable.newRow();
        row2.setString("SalesPers", "Robert");
        row2.setInt("SaleAmt", 270);
        row2.setInt("ComPct", 150);
        row2.setInt("ComAmt", 99);
        DataRow row3 = dataTable.newRow();
        row3.setString("SalesPers", "Michelle");
        row3.setInt("SaleAmt", 310);
        row3.setInt("ComPct", 120);
        row3.setInt("ComAmt", 49);
        DataRow row4 = dataTable.newRow();
        row4.setString("SalesPers", "Erich");
        row4.setInt("SaleAmt", 330);
        row4.setInt("ComPct", 120);
        row4.setInt("ComAmt", 49);
        DataRow row5 = dataTable.newRow();
        row5.setString("SalesPers", "Dafna");
        row5.setInt("SaleAmt", 360);
        row5.setInt("ComPct", 150);
        row5.setInt("ComAmt", 141);
        DataRow row6 = dataTable.newRow();
        row6.setString("SalesPers", "Rob");
        row6.setInt("SaleAmt", 380);
        row6.setInt("ComPct", 150);
        row6.setInt("ComAmt", 135);
        dataTable.getRows().add(row1);
        dataTable.getRows().add(row2);
        dataTable.getRows().add(row3);
        dataTable.getRows().add(row4);
        dataTable.getRows().add(row5);
        dataTable.getRows().add(row6);
        return dataTable;
    }

    private void insertDatatableToChart(IChart chart, DataTable dataTable) throws Exception {
        for (int c = 0; c < dataTable.getColumns().size(); c++) {
            chart.getChartData().get(0, c).setText(dataTable.getColumns().get(c).getColumnName());
        }
        for (int r = 0; r < dataTable.getRows().size(); r++) {
            Object[] datas = dataTable.getRows().get(r).getArrayList();
            for (int c = 0; c < datas.length; c++) {
                chart.getChartData().get(r + 1, c).setValue(datas[c]);
            }
        }
    }
}
                        

Tab 3

using Spire.Presentation;
using Spire.Presentation.Charts;
using System;
using System.Data;
using System.Drawing;

namespace DemoOnlineCode
{
    class CreateCharts
    {
        public void ChartDemo(String resultFileName, ChartType chartType)
        {
            Presentation presentation = new Presentation();
            RectangleF rect1 = new RectangleF(90, 100, 550, 320);
            IChart chart = presentation.Slides[0].Shapes.AppendChart(chartType, rect1, false);
            //chart title
            chart.ChartTitle.TextProperties.Text = "Chart";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;
            DataTable dataTable = getDataTable();
            insertDatatableToChart(chart, dataTable);
            //set series label
            chart.Series.SeriesLabel = chart.ChartData["B1", "D1"];
            if (chartType.ToString().Contains("Scatter") || chartType.ToString().Contains("Bubble"))
            {
                chart.Series[0].XValues = chart.ChartData["A2", "A7"];
                chart.Series[0].YValues = chart.ChartData["B2", "B7"];
                chart.Series[1].XValues = chart.ChartData["A2", "A7"];
                chart.Series[1].YValues = chart.ChartData["C2", "C7"];
                chart.Series[2].XValues = chart.ChartData["A2", "A7"];
                chart.Series[2].YValues = chart.ChartData["D2", "D7"];
                if (chartType.ToString().Contains("Bubble"))
                {
                    for (int i = 0; i < chart.Series.Count; i++)
                    {
                        chart.Series[i].Bubbles.Add(1);
                        chart.Series[i].Bubbles.Add(4);
                        chart.Series[i].Bubbles.Add(3);
                        chart.Series[i].Bubbles.Add(4);
                        chart.Series[i].Bubbles.Add(2);
                        chart.Series[i].Bubbles.Add(9);
                    }
                }
            }
            else
            {
                //set category label
                chart.Categories.CategoryLabels = chart.ChartData["A2", "A7"];
                //set values for series
                chart.Series[0].Values = chart.ChartData["B2", "B7"];
                chart.Series[1].Values = chart.ChartData["C2", "C7"];
                chart.Series[2].Values = chart.ChartData["D2", "D7"];
                if (chartType.ToString().Contains("3D"))
                {
                    chart.RotationThreeD.XDegree = 10;
                    chart.RotationThreeD.YDegree = 10;
                }
            }
            presentation.SaveToFile(resultFileName+".pptx", FileFormat.Pptx2013);
        }

        private static DataTable getDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("SalesPers", typeof(String)));
            dataTable.Columns.Add(new DataColumn("SaleAmt", typeof(Int32)));
            dataTable.Columns.Add(new DataColumn("ComPct", typeof(Int32)));
            dataTable.Columns.Add(new DataColumn("ComAmt", typeof(Int32)));
            dataTable.Rows.Add("Jeo", 250, 150, 99);
            dataTable.Rows.Add("Robert", 270, 150, 99);
            dataTable.Rows.Add("Michelle", 310, 120, 49);
            dataTable.Rows.Add("Erich", 330, 120, 49);
            dataTable.Rows.Add("Dafna", 360, 150, 141);
            dataTable.Rows.Add("Rob", 380, 150, 135);
            return dataTable;
        }

        private static void insertDatatableToChart(IChart chart, DataTable dataTable)
        {
            for (int c = 0; c < dataTable.Columns.Count; c++)
            {
                chart.ChartData[0, c].Text = dataTable.Columns[c].ColumnName;
            }
            for (int r = 0; r < dataTable.Rows.Count; r++)
            {
                Object[] datas = dataTable.Rows[r].ItemArray;
                for (int c = 0; c < datas.Length; c++)
                {
                    chart.ChartData[r + 1, c].Value = datas[c];
                }
            }
        }
    }
}

                        

Tab 1

此演示向您展示如何将文本水印和图像水印添加到 PowerPoint 文档。

Set text watermark

Text:
Font:
Font Size:
Color:
Rotate:
e-iceblue
downloads

Set image watermark

Image:
Click here to browse files
downloads

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

package ppt;

import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;

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

public class WatermarkDemo {

    public void watermark(String filePath, String watermarkType, String text, String imageFile, String resultFilePath) throws Exception {
        Presentation presentation = new Presentation();
        presentation.loadFromFile(filePath);

        switch (watermarkType){
            case "TEXT":
                addTextWatermark(presentation,text);
                break;
            case "IMAGE":
                File file = new File(imageFile);
                BufferedImage bufferedImage = ImageIO.read(file);
                addImageWatermark(presentation,bufferedImage);
                break;
        }
        presentation.saveToFile(resultFilePath,FileFormat.PPTX_2013);

    }

    private void addTextWatermark(Presentation presentation, String text) throws Exception {
        int width= 400;
        int height= 300;
        Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
                (presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);
        for (int i = 0; i< presentation.getSlides().getCount();i++){
            IAutoShape shape = presentation.getSlides().get(i).getShapes().appendShape(ShapeType.RECTANGLE,rect);
            shape.getFill().setFillType(FillFormatType.NONE);
            shape.getShapeStyle().getLineColor().setColor(Color.white);
            shape.setRotation(-45);
            shape.getLocking().setSelectionProtection(true);
            shape.getLine().setFillType(FillFormatType.NONE);
            shape.getTextFrame().setText(text);
            PortionEx textRange = shape.getTextFrame().getTextRange();
            TextFont font = new TextFont("Arial Rounded MT Bold");
            textRange.setLatinFont(font);
            textRange.getFill().setFillType(FillFormatType.SOLID);
            textRange.getFill().getSolidColor().setColor(Color.RED);
            textRange.setFontHeight(22);
        }
    }

    private void addImageWatermark(Presentation presentation, BufferedImage bufferedImage) throws DocumentEditException {
        IImageData image = presentation.getImages().append(bufferedImage);
        for (int i = 0; i< presentation.getSlides().getCount();i++) {
            ISlide slide = presentation.getSlides().get(i);
            slide.getSlideBackground().setType(BackgroundType.CUSTOM);
            slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
            slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
            slide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image);
        }
    }
}
                        

Tab 3

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
using System.Windows.Forms;

namespace DemoOnlineCode
{
    class AddWatermark
    {

        public void AddTextOrImageWatermarkDemo(string filePath,string format,string text, string imagePath,  string resultFileName)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(filePath);
            switch (format)
            {
                case "TEXT":
                    AddTextWatermark(presentation,text,resultFileName);
                    break;
                case "IMAGE":
                    AddImageWatermark(presentation,imagePath,resultFileName);
                    break;
            }
        }


        private static void AddTextWatermark(Presentation presentation, string text, string resultFileName)
        {
            Form frm = new Form();
            Graphics gc = frm.CreateGraphics();
            SizeF size = gc.MeasureString(text, new Font("Lucida Sans Unicode", 50));

            //Define a rectangle range
            RectangleF rect = new RectangleF((presentation.SlideSize.Size.Width - size.Width) / 2, (presentation.SlideSize.Size.Height - size.Height) / 2, size.Width, size.Height);

            foreach (ISlide slide in presentation.Slides)
            {
                IAutoShape shape =slide.Shapes.AppendShape(ShapeType.Rectangle, rect);
                //Set the style of the shape
                shape.Fill.FillType = FillFormatType.None;
                shape.ShapeStyle.LineColor.Color = Color.White;
                shape.Rotation = -45;
                shape.Locking.SelectionProtection = true;
                shape.Line.FillType = FillFormatType.None;

                //Add text to the shape
                shape.TextFrame.Text = text;
                TextRange textRange = shape.TextFrame.TextRange;
                //Set the style of the text range
                textRange.Fill.FillType = FillFormatType.Solid;
                textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink);
                textRange.FontHeight = 50;
            }
            presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
        }

        private static void AddImageWatermark(Presentation presentation, string imageFile, string resultFileName)
        {
            IImageData image = presentation.Images.Append(Image.FromFile(imageFile));
            foreach (ISlide slide in presentation.Slides)
            {
                slide.SlideBackground.Type = BackgroundType.Custom;
                slide.SlideBackground.Fill.FillType = FillFormatType.Picture;
                slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
                slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
            }
            presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
        }
    }
}

                        

Tab 1

此演示向您展示如何将文本和图像添加到 PowerPoint 文档。

Add text

Text:
Font:
Font Size:
Color:
downloads

Add image

Image:
Click here to browse files
downloads

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

package ppt;

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;



public class AddTextOrImageDemo {

    public void addTextOrImage(String filePath, String addType, String text, String imageFile, String resultFilePath) throws Exception {
        Presentation presentation = new Presentation();
        presentation.loadFromFile(filePath);

        switch (addType){
            case "TEXT":
                addText(presentation,text);
                break;
            case "IMAGE":
                addImage(presentation,imageFile);
                break;
        }
        presentation.saveToFile(resultFilePath,FileFormat.PPTX_2013);
    }

    private void addText(Presentation presentation, String text) throws Exception {
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle(50, 70, 620, 150));
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        shape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        shape.getTextFrame().getParagraphs().get(0).setIndent(50);
        shape.getTextFrame().getParagraphs().get(0).setLineSpacing(150);
        shape.getTextFrame().setText(text);
        shape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Arial Rounded MT Bold"));
        shape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(16);
        shape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
        shape.getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().getSolidColor().setColor(Color.RED);
    }

    private void addImage(Presentation presentation, String imageFile) throws Exception {
        Rectangle2D.Double rect1 = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);
        IEmbedImage image = presentation.getSlides().get(0).getShapes().appendEmbedImage(ShapeType.RECTANGLE, imageFile, rect1);
        image.getLine().setFillType(FillFormatType.NONE);
    }
}
                        

Tab 3

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoOnlineCode
{
    class AddTextOrImage
    {
        public void AddTextOrImageDemo(string format, string text, string imageFilePath, string resultFileName)
        {
            Presentation presentation = new Presentation();
            switch (format)
            {
                case "TEXT":
                    AddText(presentation,text,resultFileName);
                    break;
                case "IMAGE":
                    AddImage(presentation, imageFilePath, resultFileName);
                    break;
            }

        }
        private static void AddText(Presentation presentation, string text, string resultFileName)
        {
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 620, 150));
            shape.Fill.FillType = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.White;

            //Set the alignment of paragraph
            shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;
            //Set the indent of paragraph
            shape.TextFrame.Paragraphs[0].Indent = 50;
            //Set the linespacing of paragraph
            shape.TextFrame.Paragraphs[0].LineSpacing = 150;
            //Set the text of paragraph
            shape.TextFrame.Text = text;

            //Set the Font
            shape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Rounded MT Bold");
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;

            presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
        }

        private static void AddImage(Presentation presentation, string imageFilePath, string resultFileName)
        {
            RectangleF rect1 = new RectangleF(presentation.SlideSize.Size.Width / 2 - 280, 140, 120, 120);
            IEmbedImage image = presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, imageFilePath, rect1);
            image.Line.FillType = FillFormatType.None;
            presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
        }

    }
}