本文介绍如何使用Spire.Presentation for .NET设置形状中文字的对齐方式。
using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Drawing;
namespace TextAlignment
{
class Program
{
static void Main(string[] args)
{
//创建Presentation对象
Presentation presentation = new Presentation();
presentation.SlideSize.Type = SlideSizeType.Screen16x9;
//添加形状
IAutoShape textShape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 400, 200));
textShape.ShapeStyle.LineColor.Color = Color.DarkGray;
textShape.Fill.FillType = FillFormatType.None;
//删除默认段落
textShape.TextFrame.Paragraphs.Clear();
//添加段落和文字
textShape.TextFrame.Paragraphs.Append(new TextParagraph());
textShape.TextFrame.Paragraphs[0].TextRanges.Append(new TextRange("文字对齐方式"));
textShape.TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 20f;
textShape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("黑体");
textShape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
textShape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
//设置文字水平靠右
textShape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Right;
//设置文字垂直靠下
textShape.TextFrame.AnchoringType = TextAnchorType.Bottom;
//保存文档
presentation.SaveToFile("AlignText.pptx", FileFormat.Pptx2013);
}
}
}
Imports Spire.Presentation
Imports System.Drawing
Imports Spire.Presentation.Drawing
Namespace TextAlignment
Class Program
Shared Sub Main(ByVal args() As String)
'创建Presentation对象
Dim presentation As Presentation = New Presentation()
presentation.SlideSize.Type = SlideSizeType.Screen16x9
'添加形状
Dim textShape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle,New RectangleF(50,50,400,200))
textShape.ShapeStyle.LineColor.Color = Color.DarkGray
textShape.Fill.FillType = FillFormatType.None
'删除默认段落
textShape.TextFrame.Paragraphs.Clear()
'添加段落和文字
textShape.TextFrame.Paragraphs.Append(New TextParagraph())
textShape.TextFrame.Paragraphs(0).TextRanges.Append(New TextRange("文字对齐方式"))
textShape.TextFrame.Paragraphs(0).TextRanges(0).FontHeight = 20f
textShape.TextFrame.Paragraphs(0).TextRanges(0).LatinFont = New TextFont("黑体")
textShape.TextFrame.Paragraphs(0).TextRanges(0).Fill.FillType = FillFormatType.Solid
textShape.TextFrame.Paragraphs(0).TextRanges(0).Fill.SolidColor.Color = Color.Black
'设置文字水平靠右
textShape.TextFrame.Paragraphs(0).Alignment = TextAlignmentType.Right
'设置文字垂直靠下
textShape.TextFrame.AnchoringType = TextAnchorType.Bottom
'保存文档
presentation.SaveToFile("AlignText.pptx", FileFormat.Pptx2013)
End Sub
End Class
End Namespace

Spire.PDF 7.6.15已发布。该版本增强了转换PDF到图片/PDFA1A/PDFA1B的功能,修复了打印和合并PDF时出现的问题。详情请阅读以下内容。
问题修复:
Spire.SpreadSheet 5.6.1已发布。该版本修复了文本和图片的显示问题。详情请阅读以下内容。
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Spreadsheet-NET.html
本文将介绍如何使用Spire.Presentation for Java获取PowerPoint文档中文本的位置。
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import java.awt.geom.Point2D;
public class GetPositionOfText {
public static void main(String []args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.loadFromFile("Sample.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//获取第一个形状
IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
//获取形状中文本的位置
Point2D location =shape.getTextFrame().getTextLocation();
//打印文本位置(相对于幻灯片)的x和y坐标
String point1="文本位置(相对于幻灯片): x= "+location.getX()+" y = "+location.getY();
System.out.println(point1);
//打印文本位置(相对于形状)的x和y坐标
String point2 = "文本位置(相对于形状): x= " + (location.getX() - shape.getLeft()) + " y = " + (location.getY() - shape.getTop());
System.out.println(point2);
}
}
输出结果:

本文介绍使用Spire.Doc for Java来设置Word文本框中的文字旋转方向。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextBox;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class SetTextDirection {
public static void main(String[] args) {
//创建Word文档
Document doc = new Document();
Section section = doc.addSection();
//设置页面边距
section.getPageSetup().getMargins().setLeft(90f);
section.getPageSetup().getMargins().setRight(90f);
Paragraph paragraph = section.addParagraph();
//添加第一个文本框
TextBox textBox1 = paragraph.appendTextBox(280, 250);
//设置文本框为固定定位
textBox1.getFormat().setHorizontalOrigin(HorizontalOrigin.Page);
textBox1.getFormat().setHorizontalPosition(150);
textBox1.getFormat().setVerticalOrigin(VerticalOrigin.Page);
textBox1.getFormat().setVerticalPosition(80);
//设置文字旋转方向
textBox1.getFormat().setTextAnchor(ShapeVerticalAlignment.Center);
textBox1.getFormat().setLayoutFlowAlt(TextDirection.Left_To_Right);//旋转文字方向
//textBox1.getFormat().setLayoutFlowAlt(TextDirection.Left_To_Right_Rotated);//文字竖排显示
//添加文字并设置字体
Paragraph textboxPara1 = textBox1.getBody().addParagraph();
TextRange txtrg = textboxPara1.appendText("姓名_______学号_________班级__________");
txtrg.getCharacterFormat().setFontName("等线");
txtrg.getCharacterFormat().setFontSize(10);
txtrg.getCharacterFormat().setTextColor(Color.black);
textboxPara1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//保存文档
doc.saveToFile("Result.docx");
doc.dispose();
}
}
在生成的Word文档中可查看文本框中的文字旋转效果,如图:
Left_To_Right旋转效果:

Left_To_Right_Rotated竖排显示效果:

本文介绍如何使用Spire.Prensetaion for Java为PowerPoint中的文本设置透明色。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class ApplyTransparency {
public static void main(String[] args) throws Exception {
//创建Prensetaion对象
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//添加一个图形
IAutoShape textbox = presentation .getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50, 70, 300, 120));
textbox.getShapeStyle().getLineColor().setColor(new Color(1,1,1,0));
textbox.getFill().setFillType(FillFormatType.NONE);
//删除默认段落
textbox.getTextFrame().getParagraphs().clear();
//添加三个段落,并对段落文字设置不同透明度的颜色
int alpha = 55;
for (int i = 0; i < 3; i++)
{
textbox.getTextFrame().getParagraphs().append(new ParagraphEx());
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().append(new PortionEx("Text Transparency"));
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.NONE);
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
textbox.getTextFrame().getParagraphs().get(i).getTextRanges().get(0).getFill().getSolidColor().setColor(new Color(176, 48, 96, alpha));
alpha += 100;
}
//保存文档
presentation.saveToFile("TextTransparency.pptx", FileFormat.PPTX_2013);
}
}

该文将介绍如何使用Spire.Doc for .NET设置文本框内的文字旋转方向并固定文本框位置。
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
namespace WordTextbox
{
class Program
{
static void Main(string[] args)
{
//实例化document对象
Document document = new Document();
//添加一个section
Section section = document.AddSection();
//设置页面边距
section.PageSetup.Margins.Left = 90;
section.PageSetup.Margins.Right = 90;
Paragraph paragraph = section.AddParagraph();
//添加第一个文本框
TextBox textBox1 = paragraph.AppendTextBox(section.PageSetup.Margins.Left - 20, section.PageSetup.PageSize.Height + 20);
//设置文本框为固定定位
textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page;
textBox1.Format.HorizontalPosition = 0;
textBox1.Format.VerticalPosition = -10f;
textBox1.Format.VerticalOrigin = VerticalOrigin.Page;
//设置文字旋转方向
textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center;
textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight;
//添加文字并设置字体
Paragraph textboxPara1 = textBox1.Body.AddParagraph();
TextRange txtrg = textboxPara1.AppendText("姓名_______学号_________班级__________");
txtrg.CharacterFormat.FontName = "等线";
txtrg.CharacterFormat.FontSize = 10;
txtrg.CharacterFormat.TextColor = System.Drawing.Color.Black;
textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center;
//保存文档
document.SaveToFile("Result.docx");
}
}
}
Namespace WordTextbox
Class Program
Private Shared Sub Main(ByVal args() As String)
Dim document As Document = New Document()
Dim section As Section = document.AddSection()
section.PageSetup.Margins.Left = 90
section.PageSetup.Margins.Right = 90
Dim paragraph As Paragraph = section.AddParagraph()
Dim textBox1 As TextBox = paragraph.AppendTextBox((section.PageSetup.Margins.Left - 20), (section.PageSetup.PageSize.Height + 20))
textBox1.Format.HorizontalOrigin = HorizontalOrigin.Page
textBox1.Format.HorizontalPosition = 0
textBox1.Format.VerticalPosition = -10!
textBox1.Format.VerticalOrigin = VerticalOrigin.Page
textBox1.Format.TextAnchor = ShapeVerticalAlignment.Center
textBox1.Format.LayoutFlowAlt = TextDirection.LeftToRight
Dim textboxPara1 As Paragraph = textBox1.Body.AddParagraph
Dim txtrg As TextRange = textboxPara1.AppendText("姓名_______学号_________班级__________")
txtrg.CharacterFormat.FontName= "等线"
txtrg.CharacterFormat.FontSize = 10
txtrg.CharacterFormat.TextColor = System.Drawing.Color.Black
textboxPara1.Format.HorizontalAlignment = HorizontalAlignment.Center
document.SaveToFile("Result.docx")
End Sub
End Class
End Namespace
效果图:

本文将介绍如何使用Spire.Presentation for Java平均分布PowerPoint表格的行和列。
原文档:

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
public class DistributeRowsAndColumns {
public static void main(String []args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.loadFromFile("Input.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//获取表格
ITable table = (ITable) slide.getShapes().get(0);
//平均分布表格行
table.distributeRows(0,4);
//平均分布表格列
table.distributeColumns(0,4);
//保存结果文档
ppt.saveToFile("DistributeRowsAndColumns.pptx", FileFormat.PPTX_2013);
}
}
结果文档:


在文档归档、合同定稿或对外传输时,Word 文档往往需要以不可编辑的形式进行交付。以 .tiff 为后缀的 Tag Image File Format(TIFF)凭借高图像质量、良好的可移植性以及对操作系统和硬件的独立性,成为 Word 文档图像化输出的常用选择。本文将介绍如何使用 Spire.Doc for Java,在 Java 程序中高效地将 Word 文档转换为 TIFF 图片。
Spire.Doc for Java 是一款功能完善的 Word 文档处理组件,支持高质量的文档渲染与多格式输出,非常适合用于 Word 转 TIFF 等文档图像化场景,使用前只需在 Java 项目中引入对应的 Spire.Doc.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.doc</artifactId>
<version>14.9.5</version>
</dependency>
</dependencies>
此外,你也可以手动下载安装包,解压后将 jar 文件作为依赖项添加到项目中。
将整个 Word 文档转换为 TIFF 图像是最直接也是最常见的需求,通常用于文档归档或正式分发等场合。借助 Spire.Doc for Java 提供的 Document.saveToTiff() 方法,开发者只需指定生成的 TIFF 文件路径,即可将当前 Word 文档直接转换为 TIFF 格式。整个转换流程清晰直观,仅需三行核心代码即可完成。无需处理页面拆分或渲染细节,快速完成完整文档的图像化输出。
转换 Word 文档为 TIFF 的基本步骤如下:
import com.spire.doc.*;
public class ConvertWordToTiff {
public static void main(String[] args) {
// 创建 Document 类的实例
Document doc = new Document();
// 加载 Word 文档
doc.loadFromFile("/input/示例文档.docx");
// 保存为 TIFF 格式
doc.saveToTiff("/output/word转tiff.tiff");
}
}
Word 文档与转换后的 TIFF 文件对比:

有时,我们并不需要将整个 Word 文档全部导出为 TIFF,而是只转换其中的某一节内容。例如,只导出正文部分、某个章节的分析结果,或将附录单独生成图像文件。相比整篇文档转换,这种方式在内容控制和文件体积上更加灵活。
由于 Spire.Doc for Java 没有提供直接针对单个节对象的 TIFF 导出方法,我们需要转换一下思路:你可以先将 Word 拆分为多个独立文档将目标章节提取出来,或者将目标节复制到新的 Document 对象中,再调用 Document.saveToTiff() 方法完成转换。
下面是第二种方案具体的代码步骤:
下方的示例代码展示了将 Word 文档中的第二个节转换为 TIFF:
import com.spire.doc.Document;
import com.spire.doc.Section;
public class ConvertWordToTiff {
public static void main(String[] args) {
// 创建 Document 对象,加载源 Word 文档
Document doc = new Document();
doc.loadFromFile("/input/示例文档.docx");
// 获取指定的节
Section targetSection = doc.getSections().get(1);
// 创建新的 Document
Document newDoc = new Document();
// 复制源文档的默认样式,确保格式一致
doc.cloneDefaultStyleTo(newDoc);
// 将目标节深度复制到新文档中
newDoc.getSections().add(targetSection.deepClone());
// 将新文档保存为 TIFF
newDoc.saveToTiff("/output/指定节转换为TIFF.tiff");
// 释放资源
doc.dispose();
newDoc.dispose();
}
}
源文档对比 TIFF:

在将 Word 文档转换为 TIFF 图像时,除了控制转换范围,输出质量同样是一个不可忽视的因素。默认设置下生成的 TIFF 文件能够正常查看,但在打印、或放大查看时,可能会出现清晰度不足的问题。
Spire.Doc for Java 允许开发者在转换前设置图像的输出分辨率,从而精确控制 TIFF 文件的显示和打印效果,并提升转换结果的专业程度。
// 设置导出 TIFF 的水平和垂直分辨率
doc.getImageDpi().setX(300);
doc.getImageDpi().setY(300);
// 保存为 TIFF 文件
doc.saveToTiff("HighResolution.tiff");
在示例中,我们通过将 DPI 设置为 300,可以确保生成的 TIFF 图像在打印或高分辨率显示设备上依然保持清晰,适用于正式报表等专业场合。
需要注意的是,分辨率越高,生成的 TIFF 文件体积也会相应增大。因此,在实际应用中,应根据使用场景在清晰度与文件大小之间做出合理取舍。通过合理设置转换参数,即使不改变原有的文档结构,也能显著提升 Word 转 TIFF 的输出质量,使生成的图像文件更加符合业务交付和规范化管理的需求。
通过 Spire.Doc for Java,开发者可以灵活地将 Word 文档按需转换为 TIFF 图像,无论是整个文档还是指定章节,都能在保证排版布局一致性的同时实现高效输出。这种方式为文件归档、分享和规范化管理提供了可靠支持。如需了解更多 Spire.Doc 的功能或获取技术支持,欢迎随时联系我们!
Spire.Office for Java 4.6.6已发布。本次更新带来了一些新功能,比如:Spire.PDF for Java支持合并PDF文档后保存到流,增强了转换PDF到图片/Excel流的功能,同时还修复了替换图片时出现的问题。详情请阅读以下内容。
获取Spire.Office for Java 4.6.6请点击:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
新功能:
PdfDocumentBase pdfDocumentBase = PdfDocument.mergeFiles(inputFiles);
pdfDocumentBase.save(outputStream);
问题修复: