下载LEADTOOLS Document Imaging Suite SDK
最近,我们收到了几个关于使用.NET Core将Office格式(如Excel)转换为PDF的问题。LEADTOOLS Document Converter SDK类使其成为一项快速而简单的任务,同时还能为任何场景提供灵活性。
一旦你安装了LEADTOOLS SDK,请看一下LEADTOOLS21\Examples\DotNetCore\CommandLine\DocumentConverterDemo文件夹中的.NET Core Document Converter演示。文档转换器演示将文档以及图像转换为文档格式,如PDF。产生的PDF文件可以是文本搜索或基于图像的。它利用LEAD的AI-powered OCR将图像和不可搜索的文件转换为文本搜索的文件格式。
值得一提的是,LEADTOOLS Document Converter不需要第三方组件来执行XLS和XLSX的转换。换句话说,该转换器不需要微软Office互操作组件来将任何LEADTOOLS支持的文件格式转换为任何其他LEADTOOLS支持的格式,如XLS和XLSX。这简化了部署以及许可。
为了在下载SDK之前展示其工作原理,下面是一个将XLSX文件转换为PDF的简化版本的代码。
使用C#将XLSX转换为PDF
using (DocumentConverter documentConverter = new DocumentConverter())
{ var inFile = Path.Combine(ImagesPath.Path, @"input.xlsx");
var outFile = Path.Combine(ImagesPath.Path, @"output.pdf");
var format = DocumentFormat.Pdf;
var jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format);
jobData.JobName = "XLSX conversion to PDF";
var documentWriter = new DocumentWriter();
documentConverter.SetDocumentWriterInstance(documentWriter); // Handles any annotations or comments in the input spreadsheet var renderingEngine = new AnnWinFormsRenderingEngine();
documentConverter.SetAnnRenderingEngineInstance(renderingEngine);
var job = documentConverter.Jobs.CreateJob(jobData);
documentConverter.Jobs.RunJob(job);
if (job.Status == DocumentConverterJobStatus.Success)
Console.WriteLine("Success");
else
{
Console.WriteLine("{0} Errors", job.Status);
foreach (var error in job.Errors)
Console.WriteLine(" {0} at {1}: {2}", error.Operation,
error.InputDocumentPageNumber, error.Error.Message);
}
}
使用VB将XLSX转换为PDF
Using documentConverter As DocumentConverter = New DocumentConverter()
Dim inFile = Path.Combine(ImagesPath.Path, "input.xlsx")
Dim outFile = Path.Combine(ImagesPath.Path, "output.pdf")
Dim format = DocumentFormat.Pdf
Dim jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format)
jobData.JobName = "XLSX conversion to PDF"
Dim documentWriter = New DocumentWriter()
documentConverter.SetDocumentWriterInstance(documentWriter)
Dim renderingEngine = New AnnWinFormsRenderingEngine()
documentConverter.SetAnnRenderingEngineInstance(renderingEngine)
Dim job = documentConverter.Jobs.CreateJob(jobData)
documentConverter.Jobs.RunJob(job)
If job.Status = DocumentConverterJobStatus.Success Then
Console.WriteLine("Success")
Else
Console.WriteLine("{0} Errors", job.Status)
For Each [error] In job.Errors
Console.WriteLine(" {0} at {1}: {2}", [error].Operation, [error].InputDocumentPageNumber, [error].[Error].Message)
Next
End If
End Using
使用Java将XLSX转换为PDF
String inFile = Path.Combine(ImagesPath.Path, @"input.xlsx");
String outFile = Path.Combine(ImagesPath.Path, @"output.pdf");
DocumentFormat format = DocumentFormat.Pdf;
DocumentConverter documentConverter = new DocumentConverter();
DocumentWriter docWriter = new DocumentWriter();
docConverter.setDocumentWriterInstance(docWriter);
DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format);
jobData.setJobName("Xlsx Conversion");
DocumentConverterJob job = docConverter.getJobs().createJob(jobData);
docConverter.getJobs().runJob(job);
if (job.getErrors().size() > 0) for (DocumentConverterJobError error : job.getErrors()) System.out.println("%2fnError during conversion: " + error.getError().getMessage()); else System.out.println("Successfully converted file to " + outFile);
免费试用
免费下载LEADTOOLS SDK。它具有60天的全功能,并提供免费的聊天和电子邮件支持。