将各种图像格式转换为PDF在.NET中

概述

本文解释了如何使用C#将各种图像格式转换为PDF。它涵盖了以下主题。

以下代码片段也适用于Aspose.PDF.Drawing库。

C#图像到PDF转换

Aspose.PDF for .NET允许您将不同格式的图像转换为PDF文件。我们的库演示了将最流行的图像格式(如BMP、CGM、DICOM、EMF、JPG、PNG、SVG、CDR、HEIC和TIFF格式)转换的代码片段。

将BMP转换为PDF

使用Aspose.PDF for .NET库将BMP文件转换为PDF文档。

BMP图像是具有扩展名的文件。BMP表示用于存储位图数字图像的位图图像文件。这些图像独立于图形适配器,也称为设备独立位图(DIB)文件格式。 您可以使用Aspose.PDF for .NET API将BMP转换为PDF文件。因此,您可以按照以下步骤转换BMP图像:

将BMP转换为PDF

  1. 初始化一个新的Document类对象。
  2. 加载输入的BMP图像。
  3. 最后,保存输出的PDF文件。

因此,以下代码片段遵循这些步骤,并展示如何使用C#将BMP转换为PDF:

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

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        
        // Load BMP file
        image.File = dataDir + "BMPtoPDF.bmp";
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "BMPtoPDF_out.pdf");
    }
}

将CGM转换为PDF

CGM是计算机图形元文件格式的文件扩展名,通常用于CAD(计算机辅助设计)和演示图形应用程序。CGM是一种矢量图形格式,支持三种不同的编码方法:二进制(最佳程序读取速度)、基于字符(生成最小文件大小并允许更快的数据传输)或明文编码(允许用户使用文本编辑器读取和修改文件)。

请查看下一个代码片段以将CGM文件转换为PDF格式。

将CGM转换为PDF

  1. 创建CgmLoadOptions类的实例。
  2. 创建一个Document类的实例,并提及源文件名和选项。
  3. 使用所需的文件名保存文档。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertCGMtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    var option = new Aspose.Pdf.CgmLoadOptions();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "CGMtoPDF.cgm", option))
    {
        // Save PDF document
        document.Save(dataDir + "CGMtoPDF_out.pdf");
    }
}

将DICOM转换为PDF

DICOM格式是医疗行业标准,用于创建、存储、传输和可视化数字医学图像和被检查患者的文档。

Aspose.PDF for .NET允许您转换DICOM和SVG图像,但由于技术原因,添加图像时需要指定要添加到PDF的文件类型:

将DICOM转换为PDF

  1. 创建Image类的对象。
  2. 将图像添加到页面的段落集合中。
  3. 指定FileType属性。
  4. 指定文件的路径或来源。
    • 如果图像位于硬盘上的某个位置,请使用Image.File属性指定路径位置。
    • 如果图像放置在MemoryStream中,请将持有图像的对象传递给Image.ImageStream属性。

以下代码片段展示了如何使用Aspose.PDF将DICOM文件转换为PDF格式。您应该加载DICOM图像,将图像放置在PDF文件的页面上,并将输出保存为PDF。

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

    // Create PDF document 
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        
        var image = new Aspose.Pdf.Image
        {
            FileType = ImageFileType.Dicom,
            File = dataDir + "DICOMtoPDF.dcm"
        };
        page.Paragraphs.Add(image);

        // Save PDF document
        document.Save(dataDir + "DICOMtoPDF_out.pdf");
    }
}

将EMF转换为PDF

EMF以设备无关的方式存储图形图像。EMF的元文件由按时间顺序排列的可变长度记录组成,可以在任何输出设备上解析后呈现存储的图像。此外,您可以使用以下步骤将EMF转换为PDF图像:

将EMF转换为PDF

  1. 首先,初始化Document类对象。
  2. 加载EMF图像文件。
  3. 将加载的EMF图像添加到页面。
  4. 保存PDF文档。

此外,以下代码片段展示了如何在您的.NET代码片段中使用C#将EMF转换为PDF:

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

    // Create PDF document 
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load EMF file
        image.File = dataDir + "EMFtoPDF.emf";

        // Specify page dimension properties
        page.PageInfo.Margin.Bottom = 0;
        page.PageInfo.Margin.Top = 0;
        page.PageInfo.Margin.Left = 0;
        page.PageInfo.Margin.Right = 0;
        page.PageInfo.Width = image.BitmapSize.Width;
        page.PageInfo.Height = image.BitmapSize.Height;

        page.Paragraphs.Add(image);

        // Save PDF document
        document.Save(dataDir + "EMFtoPDF_out.pdf");
    }
}

将GIF转换为PDF

使用Aspose.PDF for .NET库将GIF文件转换为PDF文档。

GIF能够以不超过256种颜色的格式存储无损压缩的数据。硬件无关的GIF格式由CompuServe于1987年(GIF87a)开发,用于通过网络传输位图图像。 您可以使用Aspose.PDF for .NET API将GIF转换为PDF文件。因此,您可以按照以下步骤转换GIF图像:

将GIF转换为PDF

  1. 初始化一个新的Document类对象。
  2. 加载输入的GIF图像。
  3. 最后,保存输出的PDF文件。

因此,以下代码片段遵循这些步骤,并展示如何使用C#将GIF转换为PDF:

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

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        
        // Load sample GIF image file
        image.File = dataDir + "GIFtoPDF.gif";
        page.Paragraphs.Add(image);

        // Save PDF document
        document.Save(dataDir + "GIFtoPDF_out.pdf");
    }
}

将JPG转换为PDF

不必担心如何将JPG转换为PDF,因为Apose.PDF for .NET库提供了最佳解决方案。

您可以通过以下步骤非常轻松地将JPG图像转换为PDF:

将JPG转换为PDF

  1. 初始化Document类对象。
  2. 向PDF文档添加一个新页面。
  3. 加载JPG图像并添加到段落中。
  4. 保存输出PDF。

下面的代码片段展示了如何使用C#将JPG图像转换为PDF:

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

    // Create PDF document 
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load input JPG file
        image.File = dataDir + "JPGtoPDF.jpg";
        
        // Add image on a page
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "JPGtoPDF_out.pdf");
    }
}

然后,您可以看到如何将图像转换为PDF,页面的高度和宽度相同。我们将获取图像的尺寸,并相应地设置PDF文档的页面尺寸,步骤如下:

  1. 加载输入图像文件。
  2. 设置页面的高度、宽度和边距。
  3. 保存输出PDF文件。

以下代码片段展示了如何使用C#将图像转换为PDF,页面高度和宽度相同:

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

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load JPEG file
        image.File = dataDir + "JPGtoPDF.jpg";
        
        // Read Height of input image
        page.PageInfo.Height = image.BitmapSize.Height;
        // Read Width of input image
        page.PageInfo.Width = image.BitmapSize.Width;
        page.PageInfo.Margin.Bottom = 0;
        page.PageInfo.Margin.Top = 0;
        page.PageInfo.Margin.Right = 0;
        page.PageInfo.Margin.Left = 0;
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "JPGtoPDF_out.pdf");
    }
}

将PNG转换为PDF

Aspose.PDF for .NET支持将PNG图像转换为PDF格式的功能。请查看下一个代码片段以实现您的任务。

PNG指的是一种使用无损压缩的光栅图像文件格式,这使得它在用户中非常受欢迎。

您可以使用以下步骤将PNG转换为PDF图像:

将PNG转换为PDF

  1. 加载输入的PNG图像。
  2. 读取高度和宽度值。
  3. 创建新的Document对象并添加页面。
  4. 设置页面尺寸。
  5. 保存输出文件。

此外,下面的代码片段展示了如何在您的.NET应用程序中使用C#将PNG转换为PDF:

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

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load PNG file
        image.File = dataDir + "PNGtoPDF.png";
        
        // Read Height of input image
        page.PageInfo.Height = image.BitmapSize.Height;
        // Read Width of input image
        page.PageInfo.Width = image.BitmapSize.Width;
        page.PageInfo.Margin.Bottom = 0;
        page.PageInfo.Margin.Top = 0;
        page.PageInfo.Margin.Right = 0;
        page.PageInfo.Margin.Left = 0;
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "PNGtoPDF_out.pdf");
    }
}

将SVG转换为PDF

Aspose.PDF for .NET解释了如何将SVG图像转换为PDF格式,以及如何获取源SVG文件的尺寸。

可缩放矢量图形(SVG)是一系列基于XML的文件格式规范,用于二维矢量图形,包括静态和动态(交互式或动画)。SVG规范是一个开放标准,自1999年以来一直由万维网联盟(W3C)开发。

SVG图像及其行为在XML文本文件中定义。这意味着它们可以被搜索、索引、脚本化,并在需要时进行压缩。作为XML文件,SVG图像可以使用任何文本编辑器创建和编辑,但通常使用绘图程序(如Inkscape)创建更方便。

要将SVG文件转换为PDF,请使用名为SvgLoadOptions的类,该类用于初始化LoadOptions对象。稍后,此对象作为参数传递给Document对象的初始化,并帮助PDF渲染引擎确定源文档的输入格式。

将SVG转换为PDF

  1. 创建SvgLoadOptions类的实例。
  2. 创建一个Document类的实例,并提及源文件名和选项。
  3. 使用所需的文件名保存文档。

以下代码片段展示了将SVG文件转换为PDF格式的过程,使用Aspose.PDF for .NET。

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

    var option = new Aspose.Pdf.SvgLoadOptions();
    // Open SVG file 
    using (var document = new Aspose.Pdf.Document(dataDir + "SVGtoPDF.svg", option))
    {
        // Save PDF document
        document.Save(dataDir + "SVGtoPDF_out.pdf");
    }
}

获取SVG尺寸

还可以获取源SVG文件的尺寸。如果我们希望SVG覆盖输出PDF的整个页面,这些信息可能会很有用。SvgLoadOption类的AdjustPageSize属性满足此要求。此属性的默认值为false。如果将值设置为true,则输出PDF将具有与源SVG相同的大小(尺寸)。

以下代码片段展示了获取源SVG文件的尺寸并生成PDF文件的过程。

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

    var loadopt = new Aspose.Pdf.SvgLoadOptions();
    loadopt.AdjustPageSize = true;
    // Open SVG file
    using (var document = new Aspose.Pdf.Document(dataDir + "SVGtoPDF.svg", loadopt))
    {
        document.Pages[1].PageInfo.Margin.Top = 0;
        document.Pages[1].PageInfo.Margin.Left = 0;
        document.Pages[1].PageInfo.Margin.Bottom = 0;
        document.Pages[1].PageInfo.Margin.Right = 0;

        // Save PDF document
        document.Save(dataDir + "SVGtoPDF_out.pdf");
    }
    
}

SVG支持的特性

SVG标签

示例用法

circle

< circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"> 

defs

<defs> 
<rect id="r1" width="15" height="15" stroke="blue" stroke-width="2" /> 
<circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"/> 
<circle id="r3" cx="10" cy="10" r="10" stroke="blue" stroke-width="3"/> 
</defs> 
<use x="25" y="40" xlink:href="#r1" fill="red"/> 
<use x="35" y="15" xlink:href="#r2" fill="green"/> 
<use x="58" y="50" xlink:href="#r3" fill="blue"/>

tref

<defs> 
    <text id="ReferencedText"> 
      引用的字符数据 
    </text> 
</defs> 
<text x="10" y="100" font-size="15" fill="red" > 
    <tref xlink:href="#ReferencedText"/> 
</text>

use

<defs> 
    <text id="Text" x="400" y="200" 
          font-family="Verdana" font-size="100" text-anchor="middle" > 
      被遮罩的文本 
    </text> 
<use xlink:href="#Text" fill="blue"  />

ellipse 

<ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" />

<g fill="none" stroke="dimgray" stroke-width="1.5" > 
                <line x1="-7" y1="-7" x2="-3" y2="-3"/> 
                <line x1="7" y1="7" x2="3" y2="3"/> 
                <line x1="-7" y1="7" x2="-3" y2="3"/> 
                <line x1="7" y1="-7" x2="3" y2="-3"/> 
</g> 

image

<image id="ShadedRelief" x="24" y="4" width="64" height="82" xlink:href="relief.jpg" /> 

line

<line style="stroke:#eea;stroke-width:8" x1="10" y1="30" x2="260" y2="100"/> 

path

<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/> 

style

<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/>

polygon

<polygon style="stroke:#24a;stroke-width:1.5;fill:#eefefe" points="10,10 180,10 10,250 10,10" />

polyline

<polyline fill="none" stroke="dimgray" stroke-width="1" points="-3,-6 3,-6 3,1 5,1 0,7 -5,1 -3,1 -3,-5"/>

rect 

<rect x="0" y="0" width="400" height="600" stroke="none" fill="aliceblue" />

svg

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="10cm" height="5cm" >

text

<text font-family="sans-serif" fill="dimgray" font-size="22px" font-weight="bold" x="58" y="30" pointer-events="none">地图标题</text>

font

<text x="10" y="100" font-size="15" fill="red" > 
    示例文本 
</text>

tspan

<tspan dy="25" x="25">六种墨水颜色输入值。在这里它将 </tspan>

将TIFF转换为PDF

Aspose.PDF文件格式支持,无论是单帧还是多帧TIFF图像。这意味着您可以在您的.NET应用程序中将TIFF图像转换为PDF。

TIFF或TIF,标签图像文件格式,表示用于在符合此文件格式标准的各种设备上使用的光栅图像。TIFF图像可以包含多个帧,具有不同的图像。Aspose.PDF文件格式也支持,无论是单帧还是多帧TIFF图像。

您可以以与其他光栅文件格式图形相同的方式将TIFF转换为PDF:

将TIFF转换为PDF

  1. 创建新的Document类对象并添加页面。
  2. 加载输入的TIFF图像。
  3. 保存PDF文档。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertTIFFtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        
        // Load sample Tiff image file
        image.File = dataDir + "TIFFtoPDF.tiff";
        document.Pages[1].Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "TIFFtoPDF_out.pdf");
    }
}

如果您需要将多页TIFF图像转换为多页PDF文档并控制一些参数,例如宽度或纵横比,请按照以下步骤操作:

  1. 实例化Document类的实例。
  2. 加载输入的TIFF图像。
  3. 获取帧的FrameDimension。
  4. 为每个帧添加新页面。
  5. 最后,将图像保存到PDF页面。

以下代码片段展示了如何使用C#将多页或多帧TIFF图像转换为PDF:

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

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        using (var bitmap = new System.Drawing.Bitmap(File.OpenRead(dataDir + "TIFFtoPDF.tif")))
        {
            // Convert multi page or multi frame TIFF to PDF
            var dimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
            var frameCount = bitmap.GetFrameCount(dimension);

            // Iterate through each frame
            for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
            {
                var page = document.Pages.Add();

                bitmap.SelectActiveFrame(dimension, frameIdx);

                using (var currentImage = new MemoryStream())
                {
                    bitmap.Save(currentImage, ImageFormat.Tiff);

                    var imageht = new Aspose.Pdf.Image
                    {
                        ImageStream = currentImage,
                        //Apply some other options
                        //ImageScale = 0.5
                    };
                    page.Paragraphs.Add(imageht);
                }
            }
        }

        // Save PDF document
        document.Save(dataDir + "TIFFtoPDF_out.pdf");
    }
}

将CDR转换为PDF

CDR是一种文件格式,由Corel公司开发,主要用于矢量图形图像和绘图。CDR文件格式被大多数图像编辑程序识别。CDR格式是Corel Draw应用程序的默认格式。

请查看下一个代码片段以将CDR文件转换为PDF格式。

将CDR转换为PDF

  1. 创建CdrLoadOptions类的实例。
  2. 创建一个Document类的实例,并提及源文件名和选项。
  3. 使用所需的文件名保存文档。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertCDRtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open CDR file
    using (var document = new Aspose.Pdf.Document(dataDir + "CDRtoPDF.cdr", new CdrLoadOptions()))
    {
        // Save PDF document
        document.Save(dataDir + "CDRtoPDF_out.pdf");
    }
}

将DJVU转换为PDF

DjVu是一种压缩图像格式,由LizardTech开发。该文件格式主要用于存储不同类型的扫描文档;特别是包含文本、图片、索引彩色图像和线条图的文档。

请查看下一个代码片段以将DJVU文件转换为PDF格式。

将DJVU转换为PDF

  1. 创建DjvuLoadOptions类的实例。
  2. 创建一个Document类的实例,并提及源文件名和选项。
  3. 使用所需的文件名保存文档。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertDJVUtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
    
    // Open DJVU file
    using (var document = new Aspose.Pdf.Document(dataDir + "CDRtoPDF.djvu", new DjvuLoadOptions()))
    {
        // Save PDF document
        document.Save(dataDir + "CDRtoPDF_out.pdf");
    }
}

将HEIC转换为PDF

将HEIC转换为PDF

HEIC文件是一种高效容器图像文件格式,可以将多个图像作为集合存储在一个文件中。 要加载heic图像,您需要添加对https://www.nuget.org/packages/FileFormat.Heic/ nuget包的引用。 使用Aspose.PDF将HEIC图像转换为PDF:

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

    // Open HEIC file
    using (var fs = new FileStream(dataDir + "HEICtoPDF.heic", FileMode.Open))
    {
        var image = FileFormat.Heic.Decoder.HeicImage.Load(fs);
        var pixels = image.GetByteArray(PixelFormat.Rgb24);
        var width = (int)image.Width;
        var height = (int)image.Height;

        using (var document = new Aspose.Pdf.Document())
        {
            var page = document.Pages.Add();
            var asposeImage = new Aspose.Pdf.Image();
            asposeImage.BitmapInfo = new Aspose.Pdf.BitmapInfo(pixels, width, height, Aspose.Pdf.BitmapInfo.PixelFormat.Rgb24);
            page.PageInfo.Height = height;
            page.PageInfo.Width = width;
            page.PageInfo.Margin.Bottom = 0;
            page.PageInfo.Margin.Top = 0;
            page.PageInfo.Margin.Right = 0;
            page.PageInfo.Margin.Left = 0;

            page.Paragraphs.Add(asposeImage);

            // Save PDF document
            document.Save(dataDir + "HEICtoPDF_out.pdf");
        }
    }
}