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);
}
}
结果文档:

以.tiff格式后缀结尾的文件是一种灵活的位图格式,即Tag Image File Format,TIFF,具有可移植、存放灵活多变、支持多种色彩系统、图像质量佳、独立于操作系统、不依赖于具体硬件等特点使其得以广泛应用。本文,将介绍使用 Spire.Doc for Java 在Java程序中实现Word到Tiff图片的转换方法。
首先,您需要在 Java 程序中添加 Spire.Doc.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.doc</artifactId>
<version>13.12.2</version>
</dependency>
</dependencies>
以下是实现格式转换的详细步骤:
import com.spire.doc.*;
public class WordToTiff {
public static void main(String[] args) {
//创建Document类的实例
Document doc = new Document();
//加载Word文档
doc.loadFromFile("https://cdn.e-iceblue.cn/inputfile.docx");
//保存为tiff格式
doc.saveToTiff("ToTiff.tiff");
}
}用于测试的Word文档如下:

转换后的Tiff文件:

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
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);
问题修复:
Spire.PDF for Java 4.6.8已发布。该版本支持合并PDF文档后保存到流,增强了转换PDF到图片/Excel流的功能。同时,本次更新还修复了替换图片时出现的问题。详情请阅读以下内容。
新功能:
PdfDocumentBase pdfDocumentBase = PdfDocument.mergeFiles(inputFiles);
pdfDocumentBase.save(outputStream);
问题修复:

将Word文档转换为PDF是许多C#应用程序中的常见需求,但依赖于Microsoft Office Interop可能会显得繁琐且低效。幸运的是,像Spire.Doc for .NET这样的第三方库提供了一种强大且无缝的替代方案,可以高质量地进行转换,而 无需依赖Interop 。无论是需要 保留格式 、 用密码保护PDF ,还是 优化文件大小 ,Spire.Doc都能提供灵活的解决方案,代码量极少。
在本指南中,我们将探讨如何使用Spire.Doc在 C#中将Word转换为PDF ,涵盖基本转换、高级自定义和最佳实践,以获得最佳效果。
Spire.Doc for .NET是一个功能强大的API,允许开发者以编程方式创建、编辑和转换Word文档。它支持将Word(DOC、DOCX)转换为PDF,同时保留格式、图像、超链接等元素。
使用Spire.Doc,您可以获得以下优势:
开始前,请从官网下载Spire.Doc并将DLL引用到您的项目中。或者,您可以通过以下NuGet命令安装它:
PM> Install-Package Spire.Doc
使用默认设置将Word文档转换为PDF非常简单,以下示例展示了如何加载一个DOCX文件并将其保存为PDF:
using Spire.Doc;
namespace ConvertWordToPdf
{
class Program
{
static void Main(string[] args)
{
// 创建一个Document对象
Document doc = new Document();
// 加载Word文档
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");
// 将文档保存为PDF
doc.SaveToFile("ToPDF.pdf", FileFormat.PDF);
// 释放资源
doc.Dispose();
}
}
}
结果:

为了更好地控制转换过程,Spire.Doc提供了ToPdfParameterList类。使用此类,您可以:
以下是可用选项的总结:
| 选项 | 实现方式 |
|---|---|
| 转换为PDF/A | PdfConformanceLevel |
| 用密码保护PDF | PdfSecurity |
| 限制权限(例如,打印) | PdfSecurity |
| 嵌入所有字体 | IsEmbeddedAllFonts |
| 嵌入特定字体 | EmbeddedFontNameList |
| 保留书签 | CreateWordsBookmarks |
| 从标题创建书签 | CreateWordBookmarksUsingHeadings |
| 禁用超链接 | DisableLink |
以下代码通过设置打开密码对输出的PDF文件进行加密,确保文档免受未经授权的访问:
using Spire.Doc;
namespace ConvertWordToPasswordProtectedPdf
{
class Program
{
static void Main(string[] args)
{
// 创建一个Document对象
Document doc = new Document();
// 加载Word文档
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");
// 创建一个ToPdfParameterList对象
ToPdfParameterList parameters = new ToPdfParameterList();
// 设置打开密码
parameters.PdfSecurity.Encrypt("openPsd");
// 将Word保存为PDF
doc.SaveToFile("PasswordProtected.pdf", parameters);
// 释放资源
doc.Dispose();
}
}
}
结果:

以下代码将Word文档中使用的所有字体嵌入生成的PDF中,确保文档在任何查看者的系统字体下都能保持其预期外观:
using Spire.Doc;
namespace EmbedFonts
{
class Program
{
static void Main(string[] args)
{
// 创建一个Document对象
Document doc = new Document();
// 加载Word文档
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.docx");
// 创建一个ToPdfParameterList对象
ToPdfParameterList parameters = new ToPdfParameterList();
// 在生成的PDF中嵌入Word中使用的所有字体
parameters.IsEmbeddedAllFonts = true;
// 将文档保存为PDF
doc.SaveToFile("EmbedFonts.pdf", FileFormat.PDF);
// 释放资源
doc.Dispose();
}
}
}
结果:

为了实现最佳的PDF输出,您可能需要在转换之前准备您的Word文档。考虑以下调整:
以下代码将图像质量降低到50%,从而生成更小的PDF:
using Spire.Doc;
namespace SetImageQualityWhenConverting
{
class Program
{
static void Main(string[] args)
{
// 创建一个Document对象
Document doc = new Document();
// 加载Word文档
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");
// 将图像质量降低到50%
doc.JPEGQuality = 50;
// 将文档保存为PDF
doc.SaveToFile("CompressImage.pdf", FileFormat.PDF);
// 释放资源
doc.Dispose();
}
}
}
在C#中将Word文档转换为PDF并不复杂——Spire.Doc for .NET简化了这一过程,并提供了广泛的自定义选项。从基本转换到高级功能,如PDF加密、字体嵌入和图像压缩,全部无需Interop。
通过遵循本指南中概述的技术,您可以高效地将Word到PDF的功能集成到您的应用程序中。如需进一步帮助,请查阅Spire.Doc的在线教程或利用其免费试用版测试其功能。
您可以在代码中创建一个循环来一次处理多个文件。例如:
string[] files = Directory.GetFiles("input_folder", "*.docx");
foreach (string file in files)
{
Document document = new Document();
document.LoadFromFile(https://cdn.e-iceblue.cn/file);
document.SaveToPDF(Path.ChangeExtension(file, ".pdf"), FileFormat.PDF);
document.Dispose();
}
您可以先合并Word文件(使用Spire.Doc),然后将合并后的文档转换为PDF。例如:
Document mergedDoc = new Document();
string[] filesToMerge = Directory.GetFiles("input_folder ", "*.docx");
foreach (string file in filesToMerge)
{
mergedDoc.InsertTextFromFile(file, FileFormat.Docx);
}
mergedDoc.SaveToFile("Merged.pdf", FileFormat.PDF);
mergedDoc.Dispose();
这个问题可能是由于系统缺少自定义字体导致的。为了解决此问题,请在执行转换的机器上安装所需的字体。或者,您可以在使用Spire.Doc进行转换时直接将字体嵌入PDF中。
我司提供另外一个产品Spire.PDF for .NET,支持将PDF转回Word,请参考:如何使用C#将PDF转换为Word
如果您需要去除生成文档中的评估提示或解除功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。