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

Spire.Cloud 纯前端文档控件

Spire.Office 6.3.3已发布。本次更新带了一些新的功能,譬如:Spire PDF 支持添加 page-piece 字典,新增移除表单域的功能;Spire.XLS支持修改透视表的数据源并且新增设置形状顺序的功能等等。此外,该版本还修复了大量问题。详情请阅读以下内容。

该版本涵盖了最新版的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.3.3请点击:

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


Spire.PDF

新功能:

问题修复:


Spire.XLS

新功能:

问题修复:


Spire.Doc

问题修复:


Spire.SpreadSheet

问题修复:

本文介绍使用Spire.Doc for .NET读取Word文本框的方法。可读取文本框中的文本、图片和表格等,读取表格请参考这篇文章。文本内容位为读取文本和图片。

用于测试的Word源文档如图:

C#/VB.NET 读取 Word 文本框中的文本、图片

1、读取文本框中的文本

C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.IO;
using System.Text;

namespace ExtractText
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word源文档
            Document doc = new Document();
            doc.LoadFromFile("https://cdn.e-iceblue.cn/sample.docx");

            //获取文本框
            TextBox textbox = doc.TextBoxes[0];

            //创建StringBuilder类的对象
            StringBuilder sb = new StringBuilder();

            //遍历文本框中的对象,获取文本
            foreach (object obj in textbox.Body.ChildObjects)
            {
                if (obj is Paragraph)
                {
                    String text = ((Paragraph)obj).Text;
                    sb.AppendLine(text);
                }
            }

            //保存写入的txt文档到指定路径
            File.WriteAllText("ExtractedText.txt", sb.ToString());
            System.Diagnostics.Process.Start("ExtractedText.txt");
        }
    }
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.IO
Imports System.Text

Namespace ExtractText
	Class Program
		Private Shared Sub Main(args As String())
			'加载Word源文档
			Dim doc As New Document()
			doc.LoadFromFile("https://cdn.e-iceblue.cn/sample.docx")

			'获取文本框
			Dim textbox As TextBox = doc.TextBoxes(0)

			'创建StringBuilder类的对象
			Dim sb As New StringBuilder()

			'遍历文本框中的对象,获取文本
			For Each obj As Object In textbox.Body.ChildObjects
				If TypeOf obj Is Paragraph Then
					Dim text As [String] = DirectCast(obj, Paragraph).Text
					sb.AppendLine(text)
				End If
			Next

			'保存写入的txt文档到指定路径
			File.WriteAllText("ExtractedText.txt", sb.ToString())
			System.Diagnostics.Process.Start("ExtractedText.txt")
		End Sub
	End Class
End Namespace

文本读取结果:

C#/VB.NET 读取 Word 文本框中的文本、图片

2、读取文本框中的图片

C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

namespace ExtractImg
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word源文档
            Document doc = new Document();
            doc.LoadFromFile("https://cdn.e-iceblue.cn/sample.docx");

            //获取文本框
            TextBox textbox = doc.TextBoxes[0];    

            int index = 0 ;
            //遍历文本框中所有段落
            for (int i = 0 ; i < textbox.Body.Paragraphs.Count;i++)
            {
                Paragraph paragraph = textbox.Body.Paragraphs[i];
                //遍历段落中的所有子对象
                for (int j = 0; j < paragraph.ChildObjects.Count; j++)
                {
                    object obj = paragraph.ChildObjects[j];
                    
                    //判定对象是否为图片
                    if (obj is DocPicture)
                    {
                        //获取图片
                        DocPicture picture = (DocPicture) obj;
                        String imageName = String.Format("Image-{0}.png", index);
                        picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
                        index++;
                    }
                }
            }
                 
        }
    }
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace ExtractImg
	Class Program
		Private Shared Sub Main(args As String())
			'加载Word源文档
			Dim doc As New Document()
			doc.LoadFromFile("https://cdn.e-iceblue.cn/sample.docx")

			'获取文本框
			Dim textbox As TextBox = doc.TextBoxes(0)

			Dim index As Integer = 0
			'遍历文本框中所有段落
			For i As Integer = 0 To textbox.Body.Paragraphs.Count - 1
				Dim paragraph As Paragraph = textbox.Body.Paragraphs(i)
				'遍历段落中的所有子对象
				For j As Integer = 0 To paragraph.ChildObjects.Count - 1
					Dim obj As Object = paragraph.ChildObjects(j)

					'判定对象是否为图片
					If TypeOf obj Is DocPicture Then
						'获取图片
						Dim picture As DocPicture = DirectCast(obj, DocPicture)
						Dim imageName As [String] = [String].Format("Image-{0}.png", index)
						picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
						index += 1
					End If
				Next
			Next

		End Sub
	End Class
End Namespace

图片读取结果:

C#/VB.NET 读取 Word 文本框中的文本、图片

Spire.Doc 9.3.3已发布。该版本增强了转换Word到PDF/图片,以及RTF/XML/HTML到PDF的功能。 此外,本次版本更新还修复了加载Word文档时出现的问题。详情请阅读以下内容。

问题修复:


获取Spire.Doc 9.3.请点击:

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

Spire.XLS 11.3.4已发布。该版本支持设置形状顺序,支持操作工作表主题,同时新增了获取冻结窗格单元格范围的功能。此外,该版本增强了转换Excel到图片/PDF的功能,并且修复了保存Excel文档时出现的问题。详情请阅读以下内容。

新功能:

问题修复:


下载Spire.XLS 11.3.4请点击:

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

Spire.PDF 7.3.1已发布。该版本支持了移除表单域,同时也支持从stream流中添加PDF文档到PDF文件包,增强了转换PDF到Word/图片/ PDF/A-3A的功能。此外,该版本还修复了提取和替换文本时出现的问题等。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.PDF 7.3.1请点击:

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

本文将详细介绍如何在Java应用程序中给PowerPoint文档添加多行多列文本水印。

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

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

        Presentation presentation = new Presentation();
        presentation.loadFromFile("Sample.pptx");

        //设置文本水印的宽和高
        int width= 300;
        int height= 200;
        
        //绘制文本,设置文本格式并将其添加到第一张幻灯片
        float x = 30;
        float y = 80;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
             Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width, height);
              //添加一个shape到定义区域
              IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);
             //设置shape样式
             shape.getFill().setFillType(FillFormatType.NONE);
             shape.getShapeStyle().getLineColor().setColor(Color.white);
             shape.setRotation(-45);
             shape.getLocking().setSelectionProtection(true);
             shape.getLine().setFillType(FillFormatType.NONE);

             //添加文本到shape
             shape.getTextFrame().setText("内部使用");
             PortionEx textRange = shape.getTextFrame().getTextRange();

             //设置文本水印样式
             textRange.getFill().setFillType(FillFormatType.SOLID);
             textRange.getFill().getSolidColor().setColor(Color.pink);
             textRange.setFontHeight(20);
                x += (100 + presentation.getSlideSize().getSize().getWidth()/5);

            }
                x = 30;
                y += (100 + presentation.getSlideSize().getSize().getHeight()/6);

        }

        //保存文档
        presentation.saveToFile("output/result.pptx", FileFormat.PPTX_2010);

    }
}

效果图:

Java 添加多行多列文本水印至 PowerPoint 幻灯片文档

本文介绍使用Spire.Doc for Java来读取Word文本框的方法,读取时,可读取文本框中的文本、图片、表格等。其中,读取文本框中的表格可以参考这篇文章。以下内容为读取文本和图片。

用于测试的Word源文档如下图:

Java 读取 Word 文本框中的文本/图片/表格

1、读取文本框中的文本

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextBox;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class ExtractText {
    public static void main(String[] args) throws IOException {
        //加载含有文本框的Word文档
        Document doc = new Document();
        doc.loadFromFile("https://cdn.e-iceblue.cn/test.docx");

        //获取文本框
        TextBox textbox = doc.getTextBoxes().get(0);

        //保存文本框中的文本到指定文件
        File file = new File("ExtractedText.txt");
        if (file.exists())
        {
            file.delete();
        }
        file.createNewFile();
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);

        //遍历文本框中的对象
        for (Object object:textbox.getBody().getChildObjects())
        {
            //判定是否为文本段落
            if(object instanceof Paragraph)
            {
                //获取段落中的文本
                String text = ((Paragraph) object).getText();

                //写入文本到txt文档
                bw.write(text);
            }
        }
        bw.flush();
        bw.close();
        fw.close();
    }
}

文本读取结果:

Java 读取 Word 文本框中的文本/图片/表格

2、读取文本框中的图片

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextBox;
import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExtractImg {
    public static void main(String[] args) throws IOException {
        //加载含有文本框的Word文档
        Document doc = new Document();
        doc.loadFromFile("https://cdn.e-iceblue.cn/test.docx");

        //获取文本框
        TextBox textbox = doc.getTextBoxes().get(0);

        //创建List对象
        ArrayList<BufferedImage> images = new ArrayList();


        //遍历文本框中所有段落
        for (int i = 0 ; i < textbox.getBody().getParagraphs().getCount();i++)
        {
            Paragraph paragraph = textbox.getBody().getParagraphs().get(i);

            //遍历段落中的所有子对象
            for (int j = 0; j < paragraph.getChildObjects().getCount(); j++)
            {
                Object object = paragraph.getChildObjects().get(j);

                //判定对象是否为图片
                if (object instanceof DocPicture)
                {
                    //获取图片
                    DocPicture picture = (DocPicture) object;
                    images.add(picture.getImage());
                }
            }
        }

        //将图片以PNG文件格式保存
        for (int z = 0; z < images.size(); z++) {
            File file = new File(String.format("图片-%d.png", z));
            ImageIO.write(images.get(z), "PNG", file);
        }
    }
}

图片读取结果:

Java 读取 Word 文本框中的文本/图片/表格

Spire.Spreadsheet 5.3.0已发布。该版本主要修复了文本和数字的显示问题。详情请阅读以下内容。

问题修复:


获取Spire.Spreadsheet 5.3.0请点击:

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

该文将详细介绍在保存Excel工作簿到PDF文档时,在 PDF 结果文档中添加页码。

C#
using Spire.Xls;

namespace ExcelDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            //加载示例文档
            Workbook wbk = new Workbook();
            wbk.LoadFromFile("Sample.xlsx");

            foreach (Worksheet sheet in wbk.Worksheets)
            {
            //&P 指Page Number, &N 指总页数
            sheet.PageSetup.RightFooter = "&P/&N";
            }

            //保存文档
            wbk.SaveToFile("Result.pdf", FileFormat.PDF);

        }
    }
}
VB.NET
Imports Spire.Xls

Namespace ExcelDemo
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            '加载示例文档
            Dim wbk As Workbook = New Workbook
            wbk.LoadFromFile("Sample.xlsx")
            For Each sheet As Worksheet In wbk.Worksheets
                '&P 指Page Number, &N 指总页数
                sheet.PageSetup.RightFooter = "&P/&N"
            Next
            '保存文档
            wbk.SaveToFile("Result.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

效果图:

C#/VB.NET 转换 Excel 到 PDF 时在页脚处添加页码

本文介绍如何使用Spire.Doc for Java给Word添加行号。

import com.spire.doc.*;

public class AddLineNumber {
    public static void main(String[] args) {
        //加载Word文档
        Document doc = new Document();
        doc.loadFromFile("https://cdn.e-iceblue.cn/test.docx");

        //遍历Word中的所有section
        for(int i = 0; i < doc.getSections().getCount();i++)
        {
            Section section = doc.getSections().get(i);//获取所有section
            section.getPageSetup().setLineNumberingStartValue(1);//设置行号起始编号
            section.getPageSetup().setLineNumberingDistanceFromText(35);//设置行号距离正文距离
            section.getPageSetup().setLineNumberingStep(1);//设置行号间隔
            section.getPageSetup().setLineNumberingRestartMode(LineNumberingRestartMode.Restart_Section);//设置行号的编号模式
        }

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

行号添加效果:

Java 在 Word 中添加行号