彩票走势图

LEADTOOLS教程分享:C#, VB和Java中如何将HTML转化成PDF

原创|使用教程|编辑:何跃|2021-09-17 14:23:48.943|阅读 134 次

概述:LEAD有一个世界级的文档转换器可以轻松地将HTML转换为PDF。PDF的可移植性使你可以通过多种设备在线或离线访问网页的内容。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

你可能已经听说过LEAD有一个世界级的文档转换器。我们的文档转换器可以轻松地将HTML转换为PDF。PDF的可移植性使你可以通过多种设备在线或离线访问网页的内容。

接下来我们将用代码给您展示如何在C#、VB和Java中将HTML转化成PDF文档。

这里用到的SDK是LEADTOOLS PDF Pro Developer Toolkit,点击这里即可下载

// 初始化转换器
namespace Convert_Files_with_Document_Converter
{
  class Program
  {
    static void Main(string[] args)
    {
      string directory = @"C:\InputFileDirectory";
      string input_file = Directory.GetFiles(directory, "example.html");
      string output_file = Path.Combine(directory, $"example.pdf");

      SetLicense(); //Sets the LEADTOOLS License

      using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
      using (DocumentConverter docConverter = new DocumentConverter())
      {
        ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime");
        docConverter.SetOcrEngineInstance(ocrEngine, false);
        // Change the DocumentFormat enumeration to whichever format is needed
        ConvertFile(input_file, output_file, docConverter, DocumentFormat.Pdf);
      }
    }
  }
}
// 执行转化功能
static void ConvertFile(string inFile, string outFile, DocumentConverter docConverter, DocumentFormat docFormat)
{
  Console.WriteLine($"Converting {inFile}...");
  DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, docFormat);
  jobData.JobName = "Convert to Webpage Job";
  DocumentConverterJob job = docConverter.Jobs.CreateJob(jobData);
  docConverter.Jobs.RunJob(job);
  if (job.Errors.Count > 0)
    foreach (var error in job.Errors)
      Console.WriteLine($"Error during conversion: {error.Error.Message}\n");
  else
    Console.WriteLine($"Successfully Convereted {inFile} to {outFile}\n");
}
// VB中实现HTML转PDF
Using documentConverter As DocumentConverter = New DocumentConverter()
  Dim inFile = Path.Combine(ImagesPath.Path, "example.html")
  Dim outFile = Path.Combine(ImagesPath.Path, "example.pdf")
  Dim format = DocumentFormat.Pdf
  Dim jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format)
  jobData.JobName = "TXT conversion to PDF"
  Dim documentWriter = New DocumentWriter()
  documentConverter.SetDocumentWriterInstance(documentWriter)
  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中实现HTML转PDF
String inFile = Path.Combine(ImagesPath.Path, @"example.html");
String outFile = Path.Combine(ImagesPath.Path, @"example.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("HTML 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);


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP