Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
本文解释了如何使用 C# 将各种图像格式转换为 PDF。它涵盖了以下主题。
以下代码片段也适用于 Aspose.PDF.Drawing 库。
格式:BMP
格式:CGM
格式:DICOM
格式:EMF
格式:GIF
格式:JPG
格式:PNG
格式:SVG
格式:TIFF
格式:CDR
格式:DJVU
本文还涵盖的其他主题
Aspose.PDF for .NET 允许您将不同格式的图像转换为 PDF 文件。我们的库展示了转换最流行图像格式的代码片段,例如 - BMP、CGM、DICOM、EMF、JPG、PNG、SVG 和 TIFF 格式。
使用 Aspose.PDF for .NET 库将 BMP 文件转换为 PDF 文档。
BMP 图像是具有扩展名的文件。BMP 代表位图图像文件,用于存储位图数字图像。这些图像独立于图形适配器,也称为设备独立位图(DIB)文件格式。 您可以使用 Aspose.PDF for .NET API 将 BMP 转换为 PDF 文件。因此,您可以按照以下步骤将 BMP 图像转换:
因此,以下代码片段遵循这些步骤,并展示如何使用 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 是计算机图形元文件格式的文件扩展名,通常用于 CAD(计算机辅助设计)和演示图形应用程序。CGM 是一种矢量图形格式,支持三种不同的编码方法:二进制(最佳程序读取速度)、基于字符(生成最小文件大小并允许更快的数据传输)或明文编码(允许用户使用文本编辑器读取和修改文件)。
请查看下一个代码片段以将 CGM 文件转换为 PDF 格式。
// 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 格式是医疗行业标准,用于创建、存储、传输和可视化被检查患者的数字医学图像和文档。
Aspose.PDF for .NET 允许您转换 DICOM 和 SVG 图像,但由于技术原因,添加图像时需要指定要添加到 PDF 的文件类型:
以下代码片段展示了如何使用 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 以设备无关的方式存储图形图像。EMF 的元文件由可变长度的记录按时间顺序组成,可以在任何输出设备上解析后呈现存储的图像。此外,您可以使用以下步骤将 EMF 转换为 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");
}
}
使用 Aspose.PDF for .NET 库将 GIF 文件转换为 PDF 文档。
GIF 能够以不超过 256 种颜色的格式存储压缩数据而不损失质量。无硬件依赖的 GIF 格式由 CompuServe 于 1987 年(GIF87a)开发,用于通过网络传输位图图像。 您可以使用 Aspose.PDF for .NET API 将 GIF 转换为 PDF 文件。因此,您可以按照以下步骤将 GIF 图像转换:
因此,以下代码片段遵循这些步骤,并展示如何使用 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,因为 Apose.PDF for .NET 库提供了最佳解决方案。
您可以通过以下步骤非常轻松地将 JPG 图像转换为 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 文档的页面尺寸,步骤如下:
以下代码片段展示了如何使用 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");
}
}
Aspose.PDF for .NET 支持将 PNG 图像转换为 PDF 格式的功能。请查看下一个代码片段以实现您的任务。
PNG 是一种使用无损压缩的光栅图像文件格式,这使其在用户中非常受欢迎。
您可以使用以下步骤将 PNG 转换为 PDF 图像:
此外,以下代码片段展示了如何在您的 .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");
}
}
Aspose.PDF for .NET 解释了如何将 SVG 图像转换为 PDF 格式,以及如何获取源 SVG 文件的尺寸。
可缩放矢量图形(SVG)是一系列基于 XML 的二维矢量图形文件格式的规范,既包括静态的,也包括动态的(交互式或动画)。SVG 规范是一个开放标准,自 1999 年以来由万维网联盟(W3C)开发。
SVG 图像及其行为在 XML 文本文件中定义。这意味着它们可以被搜索、索引、脚本化,并在需要时进行压缩。作为 XML 文件,SVG 图像可以使用任何文本编辑器创建和编辑,但通常使用绘图程序(如 Inkscape)创建更为方便。
要将 SVG 文件转换为 PDF,请使用名为 SvgLoadOptions 的类,该类用于初始化 LoadOptions
对象。随后,该对象作为参数传递给 Document 对象的初始化,并帮助 PDF 渲染引擎确定源文档的输入格式。
SvgLoadOptions
类的实例。Document
类的实例,并提及源文件名和选项。以下代码片段展示了将 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 覆盖输出 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 标签 |
示例用法 |
---|---|
circle |
|
defs |
<defs>
|
tref |
<defs> |
use |
<defs> |
ellipse |
<ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" /> |
g |
<g fill="none" stroke="dimgray" stroke-width="1.5" > |
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" > |
tspan |
<tspan dy="25" x="25">六种墨水颜色输入值。这里将会 </tspan> |
Aspose.PDF 文件格式支持,无论是单帧还是多帧 TIFF 图像。这意味着您可以在 .NET 应用程序中将 TIFF 图像转换为 PDF。
TIFF 或 TIF,标记图像文件格式,表示用于在符合此文件格式标准的各种设备上使用的光栅图像。TIFF 图像可以包含多个帧,具有不同的图像。Aspose.PDF 文件格式也支持,无论是单帧还是多帧 TIFF 图像。
您可以以与其他光栅文件格式图形相同的方式将 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())
{
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 文档并控制一些参数,例如宽度或纵横比,请按照以下步骤操作:
以下代码片段展示了如何使用 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 是由 Corel Corporation 开发的文件格式,主要用于矢量图形图像和图纸。大多数图像编辑程序都能识别 CDR 文件格式。CDR 格式是 Corel Draw 应用程序的默认格式。
请查看下一个代码片段以将 CDR 文件转换为 PDF 格式。
// 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 是一种压缩图像格式,由 LizardTech 开发。该文件格式主要用于存储不同类型的扫描文档;尤其是包含文本、图片、索引彩色图像和线条图的文档。
请查看下一个代码片段以将 DJVU 文件转换为 PDF 格式。
// 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 文件是一种高效容器图像文件格式,可以将多个图像作为集合存储在一个文件中。 要加载 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");
}
}
}
平台 | 支持 | 备注 |
---|---|---|
Windows .NET Framework | 2.0-4.6 | |
Windows .NET Core | 2.0-3.1 | |
.NET 5 Windows | ||
Linux .NET Core | 2.0-3.1 | |
.NET 5 Linux |
本文还涵盖了这些主题。代码与上述相同。
格式:BMP
格式:CGM
格式:DICOM
格式:EMF
格式:DjVu
格式:CDR
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.