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

Spire.Cloud 纯前端文档控件

本文将介绍如何使用Spire.Presentation for Java在PowerPoint文档中查找和高亮指定文本。

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.TextHighLightingOptions;

import java.awt.*;

public class SearchAndHighlightText {
    public static void main(String[] args) throws Exception {
        //创建Presentation实例
        Presentation presentation = new Presentation();
        //加载PowerPoint文档
        presentation.loadFromFile("Input.pptx");

        //获取第一张幻灯片上的第一个形状
        IAutoShape shape = (IAutoShape)presentation.getSlides().get(0).getShapes().get(0);

        //设置高亮选项
        TextHighLightingOptions options = new TextHighLightingOptions();
        options.setWholeWordsOnly(true);
        options.setCaseSensitive(true);

        //使用hightLightText方法高亮指定文本
        shape.getTextFrame().highLightText("Spire", Color.yellow, options);

        //保存结果文档
        presentation.saveToFile("HighlightSpecifiedText.pptx", FileFormat.PPTX_2013);
    }
}

结果文档:

Java 在 PowerPoint 文档中查找和高亮文本

该文将详细介绍如何使用Spire.Doc for Java 获取 Word文本框中的表格内容及删除Word文本框中的表格。

获取表格:

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

import java.io.*;

public class readTableFromTextBox {
    public static void main(String[] args) throws IOException{

        //加载示例文档
        Document doc = new Document();
        doc.loadFromFile("Sample.docx");

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

        //获取文本框中第一个表格
        Table table = textbox.getBody().getTables().get(0);

        //保存文本
        String output = "output/readTableFromTextBox.txt";
        File file = new File(output);
        if (!file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);

        //遍历表格中的段落并提取文本
        for (int i = 0; i < table.getRows().getCount(); i++) {
            TableRow row = table.getRows().get(i);
            for (int j = 0; j < row.getCells().getCount(); j++) {
                TableCell cell = row.getCells().get(j);
                for (int k = 0; k < cell.getParagraphs().getCount(); k++) {
                    Paragraph paragraph = cell.getParagraphs().get(k);
                    bw.write(paragraph.getText() + "\t");
                }
            }
            bw.write("\r\n");
        }

        bw.flush();
        bw.close();
        fw.close();
    }
}

提取表格内容效果图:

Java 获取和删除 Word 文本框中的表格

删除表格:

import com.spire.doc.*;
import com.spire.doc.fields.*;

public class deleteTableFromTextBox {
    public static void main(String[] args) {

        //加载示例文档
        Document doc = new Document();
        doc.loadFromFile("Sample.docx");

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

        //获取文本框中第一个表格
        Table table = textbox.getBody().getTables().get(0);

        //删除第一个表格
        textbox.getBody().getTables().removeAt(0);

        //保存文档
        String output = "output/deleteTableFromTextBox.docx";
        doc.saveToFile(output, FileFormat.Docx_2013);
    }
}

Java 获取和删除 Word 文本框中的表格

本文介绍使用Spire.Office for Java 添加二维码到PDF文档的方法。

import com.spire.barcode.*;
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;

import java.awt.*;
import java.awt.image.BufferedImage;

public class AddQRCode {
    public static void main(String[] args) {
        //创建PdfDocument实例,添加一页到PDF
        PdfDocument pdf = new PdfDocument();
        PdfPageBase page = pdf.getPages().add();

        //创建二维码
        BarcodeSettings settings = new BarcodeSettings();
        settings.setType(BarCodeType.QR_Code);
        settings.setData("123456789");
        settings.setData2D("123456789");
        settings.setX(1f);
        settings.setLeftMargin(0);
        settings.setShowTextOnBottom(true);
        settings.setQRCodeECL(QRCodeECL.Q);
        settings.setQRCodeDataMode(QRCodeDataMode.Numeric);

        //生成二维码图片
        BarCodeGenerator generator = new BarCodeGenerator(settings);
        Image image = generator.generateImage();

        //绘制二维码图片到PDF页面指定位置
        PdfImage pdfImage = PdfImage.fromImage((BufferedImage)image);
        page.getCanvas().drawImage(pdfImage,100,0);


        //保存PDF文档
        pdf.saveToFile("添加二维码.pdf");
        pdf.dispose();
    }
}

二维码添加效果:

Java 添加二维码到 PDF 文档

Spire.Presentation for Java 3.8.6于今日发布。该版本支持更新PPT文档中的Video视频数据, 同时支持获取Audio和Video的PartName属性及强调类型动画的Amount属性。除此之外,它还修复了一些PPT转PDF、获取/设置动画、保存文档时出现的问题。具体新功能及问题修复参见下文。

新功能:

优化:

问题修复:


获取Spire.Presentation for Java 3.8.6,请点击:

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

该文将介绍如何在.NET应用程序中使用象征符号绘制复选框到Word文档。

C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.Collections.Generic;


namespace CheckBox
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建word实例,添加section,段落并插入文本
            Document doc = new Document();
            Section sec = doc.AddSection();
            Paragraph para = sec.AddParagraph();
            para.AppendText("指定字符替换成复选框, symbol1, symbol2, symbol3.");

            //设置段落样式
            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "paraStyle";
            style.CharacterFormat.FontName = "宋体";
            style.CharacterFormat.FontSize = 11;
            doc.Styles.Add(style);
            para.ApplyStyle("paraStyle");

            //复选框打勾
            TextSelection selection1 = doc.FindString("symbol1", true, true);
            TextRange tr1 = selection1.GetAsOneRange();
            tr1.CharacterFormat.FontName = "Wingdings 2";
            //doc.Replace(selection1.SelectedText, "\u0052", true, true);
            //16进制复选框打勾是0052,10进制复选框打勾是82
            doc.Replace(selection1.SelectedText, ((char)82).ToString(), true, true);
            
            //复选框打叉
            TextSelection selection2 = doc.FindString("symbol2", true, true);
            TextRange tr2 = selection2.GetAsOneRange();
            tr2.CharacterFormat.FontName = "Wingdings 2";
            //16进制复选框打叉是0053,10进制复选框打叉是83
            doc.Replace(selection2.SelectedText, "\u0053", true, true);
            
                   
           //复选框不勾选
           TextSelection selection3 = doc.FindString("symbol3", true, true);
           TextRange tr3 = selection3.GetAsOneRange();
           tr3.CharacterFormat.FontName="Wingdings 2";
           //16进制复选框不勾选是00A3,10进制是163
           doc.Replace(selection3.SelectedText, "\u00A3", true, true);

           //保存文档
           doc.SaveToFile("symbolTest.docx",FileFormat.Docx2013);         
                   
        }
    }
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Imports System.Collections.Generic

Namespace CheckBox
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim doc As Document = New Document
            Dim sec As Section = doc.AddSection
            Dim para As Paragraph = sec.AddParagraph
            para.AppendText("指定字符替换成复选框,  symbol1, symbol2, symbol3.”), 
            ParagraphStyle, style=newParagraphStyle(docUnknown)
            style.Name = "paraStyle"
            style.CharacterFormat.FontName = "宋体"
            style.CharacterFormat.FontSize = 11
            doc.Styles.Add(style)
            para.ApplyStyle("paraStyle")
            '复选框打勾
            Dim selection1 As TextSelection = doc.FindString("symbol1", true, true)
            Dim tr1 As TextRange = selection1.GetAsOneRange
            tr1.CharacterFormat.FontName = "Wingdings 2"
            '16进制复选框打勾是0052,10进制复选框打勾是82
            'doc.Replace(selection1.SelectedText, "\u0052", true, true);
            doc.Replace(selection1.SelectedText, CType(82,Char).ToString, true, true)
            '复选框打叉
            Dim selection2 As TextSelection = doc.FindString("symbol2", true, true)
            Dim tr2 As TextRange = selection2.GetAsOneRange
            tr2.CharacterFormat.FontName = "Wingdings 2"
            '16进制复选框打叉是0053,10进制复选框打叉是83
            doc.Replace(selection2.SelectedText, "\u0053", true, true)

             '复选框不勾选
            Dim selection3 As TextSelection = doc.FindString("symbol3", true, true)
            Dim tr3 As TextRange = selection3.GetAsOneRange
            tr3.CharacterFormat.FontName = "Wingdings 2"
            '16进制复选框不勾选是00A3,10进制是163
            doc.Replace(selection3.SelectedText, "\u00A3", true, true)

            '保存文档
            doc.SaveToFile("symbolTest.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

效果图:

C#/VB.NET 使用象征符号在 Word 中绘制复选框

该文将介绍如何使用象征符号绘制复选框到Word文档。

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;


public class drawCombox {
    public static void main(String[] args) {

        //新建word 文档
        Document doc = new Document();
        Section section = doc.addSection();
        Paragraph para = section.addParagraph();
        para.setText("指定字符替换成复选框 symbol1, symbol2, symbol3.");

        //设置段落中字体样式
        ParagraphStyle style= new ParagraphStyle(doc);
        style.setName("paraStyle");
        style.getCharacterFormat().setFontName("宋体");
        style.getCharacterFormat().setFontSize(11f);
        doc.getStyles().add(style);
        para.applyStyle("paraStyle");

        //复选框打勾
        TextSelection selection1 = doc.findString("symbol1",true,true);
        TextRange tr1 = selection1.getAsOneRange();
        tr1.getCharacterFormat().setFontName("Wingdings 2");
        //除了16进制,也可以用10进制来表示这个符号,复选框打勾是82
        doc.replace(selection1.getSelectedText(), "\u0052", true, true);
        //doc.replace(selection1.getSelectedText(),String.valueOf(((char)82)), true, true);

        //复选框打叉
        TextSelection selection2 = doc.findString("symbol2",true,true);
        TextRange tr2 = selection2.getAsOneRange();
        tr2.getCharacterFormat().setFontName("Wingdings 2");
        //16进制复选框打叉是0053,10进制是83
        doc.replace(selection2.getSelectedText(), "\u0053", true, true);

        //复选框不勾选
        TextSelection selection3 = doc.findString("symbol3", true, true);
        TextRange tr3 = selection3.getAsOneRange();
        tr3.getCharacterFormat().setFontName("Wingdings 2");
        //16进制复选框不勾选是00A3,10进制是163
        doc.replace(selection3.getSelectedText(), "\u00A3", true, true);

        doc.saveToFile("output/symbolTest.docx",FileFormat.Docx_2013);
    }
}

效果图:

Java 使用象征符号在 Word 中绘制复选框

本文介绍如何使用Spire.Doc for Java在Word文档中添加上角标和下角标。

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SubSuperScript;
import com.spire.doc.fields.TextRange;

public class InsertSupSubScript {

    public static void main(String[] args) {

        //创建Document对象
        Document document = new Document();

        //添加节
        Section section = document.addSection();

        //添加段落
        Paragraph paragraph = section.addParagraph();

        //添加文字到段落
        paragraph.appendText("E = mc");

        //添加文字并设置为上角标
        TextRange textRange = paragraph.appendText("2");
        textRange.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);

        //添加换行
        paragraph.appendBreak(BreakType.Line_Break);

        //添加文字并设置部分文字为下角标
        paragraph.appendText("a");
        paragraph.appendText("n").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
        paragraph.appendText(" = S");
        paragraph.appendText("n").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);
        paragraph.appendText(" - S");
        paragraph.appendText("n-1").getCharacterFormat().setSubSuperScript(SubSuperScript.Sub_Script);

        //保存文档
        document.saveToFile("InsertSubSuperScript.docx", FileFormat.Docx);
    }

Java Word 中添加上角标和下角标

本文将介绍如何使用Spire.Doc for .NET比较两个Word文档的内容差异。

以下是两个原Word文档截图,红框标记部分是它们的内容差异:

C#/VB.NET 比较两个 Word 文档的内容

C#
using Spire.Doc;

namespace CompareWordDocuments
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document实例
            Document doc1 = new Document();
            //加载第一个Word文档
            doc1.LoadFromFile("Doc1.docx");
            doc1.TrackChanges = false;
            // 如果含有修订,接受修订
            if (doc1.HasChanges)
            {
                doc1.AcceptChanges();
            }

            //创建Document实例
            Document doc2 = new Document();
            //加载第二个Word文档
            doc2.LoadFromFile("Doc2.docx");
            doc2.TrackChanges = false;
            // 如果含有修订,接受修订
            if (doc2.HasChanges)
            {
                doc2.AcceptChanges();
            }

            //比较这两个文档的内容差异
            doc1.Compare(doc2, "Shawn");

            //保存结果文档
            doc1.SaveToFile("Result.docx");
            doc1.Dispose();
        }
    }
}
VB.NET
Imports Spire.Doc

Namespace CompareWordDocuments
    Class Program
        Private Shared Sub Main(ByVal args As String())
            '创建Document实例
            Dim doc1 As Document = New Document()
            '加载第一个Word文档
            doc1.LoadFromFile("Doc1.docx")
            doc1.TrackChanges = False
            '如果含有修订,接受修订
            If doc1.HasChanges Then
                doc1.AcceptChanges()
            End If

            '创建Document实例
            Dim doc2 As Document = New Document()
            '加载第二个Word文档
            doc2.LoadFromFile("Doc2.docx")
            doc2.TrackChanges = False
            '如果含有修订,接受修订
            If doc2.HasChanges Then
                doc2.AcceptChanges()
            End If

            '比较这两个文档的内容差异
            doc1.Compare(doc2, "Shawn")

            '保存结果文档
            doc1.SaveToFile("Result.docx")
            doc1.Dispose()
        End Sub
    End Class
End Namespace

比较结果:

C#/VB.NET 比较两个 Word 文档的内容

默认情况下,源数据的更改变动不会自动更新到透视表,需要在透视表上进行刷新才能显示最新数据变化。下面通过使用Spire.XLS for Java来展示如何刷新透视表。

import com.spire.xls.*;

public class RefreshPivotTable {
    public static void main(String[] args) {
        //创建实例,加载Excel
        Workbook wb = new Workbook();
        wb.loadFromFile("数据透视表.xlsx");

        //获取第一个工作表
        Worksheet sheet = wb.getWorksheets().get(0);

        //更改透视表的数据源数据
        sheet.getCellRange("C2:C4").setText("产品A");
        sheet.getCellRange("C5:C7").setText("产品B");
        sheet.getCellRange("C8:C10").setText("产品C");

        //获取透视表,刷新数据
        PivotTable pivotTable = (PivotTable) sheet.getPivotTables().get(0);
        pivotTable.getCache().isRefreshOnLoad();

        //保存文档
        wb.saveToFile("刷新透视表.xlsx",FileFormat.Version2013);
    }
}

透视表更新前后效果:

Java 刷新 Excel 中的透视表

Spire.Office 5.8.5 已正式发布。该版本新增了一些功能,如Spire.Doc添加了比较功能、Spire.Presentation支持添加节和设置SmartArt节点的轮廓线样式、Spire.PDF将PDF转换为Excel时支持图表。此外,也修正了将Word转换为PDF/ Epub、Excel转换为PDF、PPT转换为PDF、XPS转换为PDF、更新Excel数据透视表、合并PDF文档以及从PDF文件中提取文本时出现的一些问题。更多新功能及问题修复详情如下。

该版本涵盖了最新版的Spire.Doc、Spire.PDF、Spire.XLS、Spire.Presentation、Spire.DataExport、Spire.Barcode、Spire.DocViewer、Spire.PDFViewer、Spire.OfficeViewer、Spire.Email。

版本信息如下:

获取Spire.Office 5.8.5, 请点击:

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


Spire.Doc

新功能:

调整:

问题修复:


Spire.XLS

问题修复:


Spire.Presentation

新功能:

问题修复:


Spire.PDF

新功能:

问题修复: