转换 PDF 文件

将 PDF 页面转换为不同的图像格式 (Facades)

为了将 PDF 页面转换为不同的图像格式,您需要创建 PdfConverter 对象,并使用 BindPdf 方法打开 PDF 文件。之后,您需要调用 DoConvert 方法进行初始化任务。然后,您可以使用 HasNextImageGetNextImage 方法循环遍历所有页面。GetNextImage 方法允许您创建特定页面的图像。您还需要将 ImageFormat 传递给此方法,以便创建特定类型的图像,即 JPEG、GIF 或 PNG 等。最后,调用 PdfConverter 类的 Close 方法。以下代码片段向您展示如何将 PDF 页面转换为图像。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPdfPagesToImages01()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PdfConverter object
    using (var converter = new Aspose.Pdf.Facades.PdfConverter())
    {
        // Bind PDF document
        converter.BindPdf(dataDir + "ConvertPdfPagesToImages.pdf");

        // Initialize the converting process
        converter.DoConvert();

        // Check if pages exist and then convert to image one by one
        while (converter.HasNextImage())
        {
            // Generate output file name with '_out' suffix
            var outputFileName = dataDir + System.DateTime.Now.Ticks.ToString() + "_out.jpg";
            // Convert the page to image and save it
            converter.GetNextImage(outputFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

在下一个代码片段中,我们将展示如何更改一些参数。通过 CoordinateType 我们设置框架为 ‘CropBox’。此外,我们可以通过指定每英寸的点数来更改 Resolution。下一个 FormPresentationMode - 表单呈现模式。然后我们指明 StartPage,设置转换开始的页面编号。我们还可以通过设置范围来指定最后一页。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPdfPagesToImages02()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PdfConverter object
    using (var converter = new Aspose.Pdf.Facades.PdfConverter())
    {
        // Bind PDF document
        converter.BindPdf(dataDir + "ConvertPdfPagesToImages.pdf");

        // Initialize the converting process
        converter.DoConvert();

        // Set additional conversion settings
        converter.CoordinateType = Aspose.Pdf.PageCoordinateType.CropBox;
        converter.Resolution = new Aspose.Pdf.Devices.Resolution(600);
        converter.FormPresentationMode = Aspose.Pdf.Devices.FormPresentationMode.Production;
        converter.StartPage = 2;

        // Check if pages exist and then convert to image one by one
        while (converter.HasNextImage())
        {
            // Generate output file name
            var outputFileName = dataDir + System.DateTime.Now.Ticks.ToString() + "_out.jpg";
            // Convert the page to image and save it
            converter.GetNextImage(outputFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

使用自定义字体替换将 PDF 页面转换为图像格式

在下一个代码片段中,我们演示如何在 PDF 到图像转换过程中应用自定义字体替换。我们使用 FontRepository.Substitutions 集合来注册自定义替换规则。在此示例中,当遇到字体 “Helvetica” 时,它将被替换为 “Arial”。

另请参见

Aspose.PDF for .NET 允许将 PDF 文档转换为各种格式,也可以从其他格式转换为 PDF。此外,您可以检查 Aspose.PDF 转换的质量,并使用 Aspose.PDF 转换器应用程序在线查看结果。了解 转换 部分以解决您的任务。