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ライブラリでも動作します。
Aspose.PDF for .NETは、さまざまな形式の画像をPDFファイルに変換することを可能にします。当社のライブラリは、BMP、CGM、DICOM、EMF、JPG、PNG、SVG、CDR、HEIC、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は、プログラムの読み取り速度に最適なバイナリ(最小のファイルサイズを生成し、データ転送を高速化する)またはクリアテキストエンコーディング(ユーザーがテキストエディタでファイルを読み取り、変更できる)など、3つの異なるエンコーディング方法をサポートするベクターグラフィックス形式です。
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画像に変換できます。
さらに、次のコードスニペットは、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形式は、1987年(GIF87a)にCompuServeによってビットマップ画像をネットワーク経由で送信するために開発されました。 Aspose.PDF for .NET APIを使用してGIFをPDFファイルに変換できます。したがって、GIF画像を変換するための手順は次のとおりです。
次のコードスニペットは、これらの手順に従い、C#を使用してBMPを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に変換する方法を考える必要はありません。なぜなら、Aspose.PDF for .NETライブラリが最適な解決策を提供するからです。
Aspose.PDF for .NETを使用してJPG画像をPDFに非常に簡単に変換できます。手順は次のとおりです。
以下のコードスニペットは、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画像に変換できます。
さらに、以下のコードスニペットは、C#を使用して.NETアプリケーションで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)は、静的および動的(インタラクティブまたはアニメーション)2次元ベクターグラフィックス用のXMLベースのファイル形式の仕様のファミリーです。SVG仕様は、1999年からWorld Wide Web Consortium(W3C)によって開発されているオープンスタンダードです。
SVG画像とその動作はXMLテキストファイルで定義されています。これは、検索、インデックス作成、スクリプト化、必要に応じて圧縮できることを意味します。XMLファイルとして、SVG画像は任意のテキストエディタで作成および編集できますが、Inkscapeなどの描画プログラムを使用して作成する方が便利です。
オンラインでSVG形式をPDFに変換してみてください
Aspose.PDF for .NETは、機能と品質を調査するために使用できるオンライン無料アプリケーション“SVG to PDF”を提供しています。
SVGファイルをPDFに変換するには、LoadOptions
オブジェクトを初期化するために使用されるSvgLoadOptions
というクラスを使用します。後で、このオブジェクトはDocumentオブジェクトの初期化中に引数として渡され、PDFレンダリングエンジンがソースドキュメントの入力形式を決定するのに役立ちます。
SvgLoadOptions
クラスのインスタンスを作成します。Document
クラスのインスタンスを作成します。次のコードスニペットは、Aspose.PDF for .NETを使用して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_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">6つのインクカラー入力値。ここに表示されます</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ドキュメントに変換し、幅やアスペクト比などのパラメータを制御する必要がある場合は、次の手順に従ってください。
次のコードスニペットは、マルチページまたはマルチフレームTIFF画像をC#で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");
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.