목차
NuGet을 통해 설치됨
PM> Install-Package Spire.PDF
관련된 링크들

PDF 파일은 안정적인 레이아웃, 쉬운 전송성 및 높은 가독성으로 인해 일상 업무에 널리 사용됩니다. 어떤 경우에는 PDF를 이미지로 변환해야 할 수도 있습니다. 콘텐츠를 이미지로 저장하면 로딩 속도가 빨라질 뿐만 아니라 실수로 편집할 위험도 줄어듭니다. 또한 PDF 파일이나 페이지를 다른 문서나 웹 페이지에 삽입하려는 경우 이를 지정된 이미지 형식으로 변환하는 것이 탁월한 선택입니다. 이 구절에서는 다음의 세부 사항을 다루고 있습니다. C#을 사용하여 PDF를 이미지로 변환합니다. 또한 원본 콘텐츠의 시각적 충실도를 유지하면서 C# 코드를 사용하여 PDF를 원하는 이미지 형식으로 손쉽게 변환할 수 있습니다.
- PDF 변환용 C# 라이브러리
- 이미지 형식의 분류
- C#에서 PDF를 PNG/JPG/BMP/EMF/TIFF로 변환
- C#에서 PDF를 SVG로 변환
- C#에서 PDF를 여러 페이지 TIFF로 변환
PDF 변환용 C# 라이브러리
이 문서에서는 .NET 애플리케이션 내에서 PDF 파일을 조작하고 변환할 수 있는 안정적인 PDF 처리 라이브러리인 Spire.PDF for .NET을 사용하여 PDF를 이미지로 변환하는 방법을 보여줍니다. 이를 사용하면 PDF에서 이미지로 변환하는 동안 크기, 픽셀과 같은 이미지 속성을 조정할 수도 있습니다.
이미지 외에도 이 라이브러리를 사용하면 PDF를 Word로, PDF를 HTML로, PDF를 XPS로 변환하는 등의 작업을 수행할 수 있습니다.
다음 중 하나를 수행할 수 있습니다 다운로드 DLL 파일을 수동으로 참조하거나 다음 명령을 사용하여 NuGet을 통해 설치합니다.
PM> Install-Package Spire.PDF
이미지 형식의 분류
컴퓨터 그래픽에서 이미지는 두 가지 주요 범주로 분류됩니다.
- 비트맵 이미지 개별 픽셀의 격자로 구성되며, 각 픽셀에는 특정 색상이 할당됩니다. 일반적인 비트맵 이미지 형식에는 PNG, JPG, BMP, EMF 및 TIFF 가 포함됩니다.
- 벡터 그래픽, 반면에 수학 방정식과 선, 곡선, 다각형과 같은 기하학적 모양을 사용하여 이미지를 표현합니다. 일반적인 벡터 그래픽 형식 SVG 및 EPS를 포함합니다.
현재 Spire.PDF for .NET는 PNG/JPG/BMP/EMF/TIFF/SVG 형식을 지원합니다. 다음으로 각 전환에 대한 세부정보를 알려드리겠습니다.
C#에서 PDF를 PNG/JPG/BMP/EMF/TIFF로 변환
단계
- PdfDocument 인스턴스를 만들고 LoadFromFile() 메서드를 사용하여 디스크에서 PDF 파일을 로드합니다.
- SaveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) 메서드를 호출하여 지정된 페이지를 이미지로 변환합니다.
- 마지막으로 Image.Save(문자열 파일명, ImageFormat 형식) 메서드를 사용하여 이미지를 JPG 파일로 저장합니다. 이 경우 PDF를 JPG로 예로 들어 보겠습니다. 원하는 경우 형식을 변경할 수도 있습니다.
샘플 코드
- C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;
namespace PDFtoImage
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a sample PDF file
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");
//Convert the first page to an image and set the image Dpi
Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 500, 500);
//Save images as JPG format to a specified folder
image.Save("C:\\Users\\Administrator\\Desktop\\Image\\ToImage.jpg", ImageFormat.Jpeg);
//Close the document
pdf.Close();
}
}
}
C#에서 PDF를 SVG로 변환
단계
- PdfDocument 인스턴스를 만들고 LoadFromFile() 메서드를 사용하여 디스크에서 PDF 파일을 로드합니다.
- SaveToFile(string filename, int startIndex, int endIndex, FileFormat fileFormat) 메서드를 사용하여 첫 번째 페이지를 SVG로 변환합니다. 매개변수에 변환하려는 페이지 색인을 지정할 수 있습니다.
샘플 코드
- C#
using Spire.Pdf;
namespace PDFtoSVG
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a sample PDF file
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");
//Convert the first page to SVG
pdf.SaveToFile("C:\\Users\\Administrator\\Desktop\\Image\\ToImage.svg", 0, 0, FileFormat.SVG);
//Close the document
pdf.Close();
}
}
}
C#에서 PDF를 여러 페이지 TIFF로 변환
다른 이미지 형식과 비교하여 TIFF를 사용하면 여러 이미지 또는 페이지를 단일 파일에 저장할 수 있습니다. 이 기능은 어떤 경우에는 사용자들 사이에서 인기 있는 선택이 됩니다.
이 섹션에서는 PDF 파일을 여러 페이지로 구성된 TIFF 파일로 변환하는 방법을 보여 드리겠습니다. 이 변환을 달성하기 위해 다음 방법을 사용자 정의할 수 있습니다.- SaveAsImage() 메서드는 각 PDF 페이지를 이미지로 변환한 다음 이미지 배열을 반환하는 것을 지원합니다.
- GetEncoderInfo() 메서드는 지정된 MIME 유형을 기반으로 일치하는 이미지 인코더 정보를 찾고 반환하는 것을 지원합니다.
- JoinTiffImages() 메서드는 여러 이미지를 단일 TIFF 파일로 병합하는 데 사용됩니다. 이미지 배열의 각 이미지를 반복하고 지정된 인코더 매개변수에 따라 저장하는 방식으로 작동합니다.
단계
- PdfDocument 인스턴스를 만들고 LoadFromFile() 메서드를 사용하여 디스크에서 PDF 파일을 로드합니다.
- SaveAsImage() 메서드를 호출하여 PDF의 각 페이지를 이미지로 변환하고 이미지 배열에 저장합니다.
- 마지막으로 JoinTiffImages() 메서드를 호출하여 변환된 TIFF 이미지를 여러 페이지의 TIFF 파일로 병합합니다.
샘플 코드
- C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
namespace SavePdfAsTiff
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Load the PDF file
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");
// Convert PDF pages to images and save them as an image array
Image[] images = SaveAsImage(pdf);
// Merge the converted TIFF images into a multi-page TIFF file
JoinTiffImages(images, "C:\\Users\\Administrator\\Desktop\\Image\\ToImage.tiff", EncoderValue.CompressionLZW);
}
private static Image[] SaveAsImage(PdfDocument pdf)
{
// Create an image array with the size equal to the number of PDF pages
Image[] images = new Image[pdf.Pages.Count];
//Iterate through each page of PDF
for (int i = 0; i < pdf.Pages.Count; i++)
{
//Convert these pages to images
images[i] = pdf.SaveAsImage(i);
}
return images;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get all available image encoders
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
// Find the encoder that matches the specified mime type
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
// An exception is thrown if no matching encoder is found
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//Set the parameters of the image encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
// Initialize the first image as the base for merging
Image pages = images[0];
int frame = 0;
// Get the encoder information for TIFF format
ImageCodecInfo info = GetEncoderInfo("image/tiff");
// Iterate through each image
foreach (Image img in images)
{
if (frame == 0)
{
// If it's the first frame, set it as the current base image
pages = img;
// Save the first frame using the specified encoder parameters
pages.Save(outFile, info, ep);
}
else
{
// For intermediate frames, update the encoder parameter to indicate a new page
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
// Save the intermediate frame
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
// If it's the last frame, flush the encoder parameters to close the file
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
}
}
C# 라이브러리 무료 평가판
Spire.PDF for .NET는 사용자가 제한 없이 제품 기능을 더 잘 평가할 수 있도록 무료 사용을 제공합니다. 당신은 얻을 수 있습니다 무료 30일 임시 라이센스 다음 링크에서 PDF를 C#의 이미지로 변환하세요.
결론
이 게시물에서는 C#을 사용하여 PDF를 널리 사용되는 이미지 형식으로 변환하는 방법을 배웠습니다. Spire.PDF for .NET는 다음과 같은 다른 PDF 처리 작업에도 도움이 될 수 있습니다 PDF 생성, PDF 병합, PDF 비교 등, 한마디로 이 라이브러리는 프로세스를 단순화하고 개발자가 PDF 조작 작업과 관련된 강력한 응용 프로그램을 구축하는 데 집중할 수 있도록 합니다.