C#, VB.NET을 사용하여 PDF 문서 압축

블로그 카테고리: PDF.NET

2024/07/12

VintaSoft PDF .NET Plug-in은 PDF 문서의 압축 및 최적화에 사용할 수 있습니다. 또한 VintaSoft PDF .NET Plug-in을 사용하여 PDF 문서에서 메타데이터를 제거할 수 있습니다. PDF 파일 크기를 줄이면 PDF 파일 전송 시 네트워크 트래픽을 줄이고 PDF 파일이 차지하는 저장 공간을 줄일 수 있습니다. 이는 특히 아카이빙, 이메일 전송, 웹 애플리케이션에서 PDF 문서 사용과 같은 분야에서 유용합니다.

VintaSoft PDF .NET Plug-in은 PDF 문서를 최적화하기 위해 다음과 같은 작업을 수행할 수 있습니다.

PDF 문서 압축

PDF 문서에는 사용되지 않는 리소스가 포함될 수 있습니다. VintaSoft PDF .NET Plug-in은 PDF 문서에서 사용되지 않는 리소스를 찾아 제거할 수 있습니다.
또한 PDF 문서에는 수정 이력이 포함될 수 있습니다. VintaSoft PDF .NET Plug-in은 PDF 문서에서 수정 이력을 제거할 수 있습니다.
또한 PDF 문서의 리소스는 최적화되지 않은 압축 알고리즘으로 압축될 수 있습니다. VintaSoft PDF .NET Plug-in은 더 최적화된 압축 알고리즘을 사용하여 리소스를 압축할 수 있습니다.
또한 PDF 파일이 PDF 형식 1.4 이하를 사용하는 경우 PDF 문서에는 압축되지 않은 상호 참조 테이블이 포함될 수 있습니다. VintaSoft PDF .NET Plug-in은 PDF 문서를 PDF 1.5 이상 형식으로 저장하고 압축된 상호 참조 테이블을 사용할 수 있습니다.

다음은 기존 PDF 문서를 불러오고, PDF 문서에서 사용되지 않는 리소스를 제거하고, 사용되는 PDF 리소스를 최적의 압축 알고리즘으로 압축하고, 최적화된 PDF 형식으로 PDF 문서를 저장하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Packs the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void PackDocument(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
        Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must use maximum Flate compression level (best compression)
    compressor.FlateCompressionLevel = 9;
    // specify that compressor must recompress all resource that uses None, LZW, Flate compression using Flate compression
    compressor.RecompressFlateCompression = true;
    compressor.UseFlateInsteadLzwCompression = true;
    compressor.UseFlateInsteadNoneCompression = true;

    // specify that compressor must remove incremental update info and unused objects
    compressor.PackDocument = true;

    // if version of PDF document is lower than 1.7
    if (GetPdfDocumentVersion(inPdfFilename) < 17)
    {
        // set output format to PDF 1.7
        compressor.DocumentPackFormat = Vintasoft.Imaging.Pdf.PdfFormat.Pdf_17;
    }

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}

/// <summary>
/// Returns the PDF document version.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <returns>The version number in dual-digit format (10,11,12,13,14,15,16,17,20,...).</returns>
private static int GetPdfDocumentVersion(string pdfFilename)
{
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
        return document.Format.VersionNumber;
}


PDF 문서의 글꼴 최적화

일부 글꼴 글리프는 PDF 문서에서 텍스트를 렌더링하는 데 사용되지 않습니다. VintaSoft PDF .NET Plug-in을 사용하면 PDF 문서의 글꼴에서 사용되지 않는 글리프를 제거할 수 있습니다.

다음은 Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand 클래스를 사용하여 PDF 문서의 글꼴을 최적화하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Subsets fonts in PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void SubsetFonts(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must subset fonts in PDF document
    compressor.SubsetFonts = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF 문서의 이미지 압축

많은 PDF 문서에는 이미지가 포함되어 있습니다. VintaSoft PDF..NET 플러그인을 사용하면 PDF 문서의 이미지 해상도와 색상 비트 심도를 낮춰 PDF 파일 크기를 줄일 수 있습니다.

다음은 Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand 클래스를 사용하여 PDF 문서의 리소스 해상도와 색상 비트 심도를 낮추는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Detects "read color depth" of PDF image resources and compress PDF document with intent to view in 150DPI.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void CompressToViewIn150DPI(string inPdfFilename, string outPdfFilename)
{
    // create compressor that will compress PDF document using lossy compression algorithms
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateLossyCompressor(150, false, false, false);

    // specify that compressor must use JPEG compression for color images
    compressor.ColorImagesCompression = Vintasoft.Imaging.Pdf.PdfCompression.Jpeg;
    // specify that compressor must set JPEG quality to 70
    compressor.ColorImagesCompressionSettings.JpegQuality = 70;

    // specify that compressor must detect if image is bitonal image and use optimal compression for bitonal image
    compressor.DetectBitonalImageResources = true;
    // specify that compressor must detect if image is black-white image and use optimal compression for black-white image
    compressor.DetectBlackWhiteImageResources = true;
    // specify that compressor must detect if image is grayscale image and use optimal compression for grayscale image
    compressor.DetectGrayscaleImageResources = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF 문서의 내용 지우기

PDF 문서에는 사용되지 않는 객체(예: 리소스, 페이지, 글꼴, 이미지, 이름, 콘텐츠 연산자)가 포함될 수 있습니다. 또한 PDF 문서에는 이미지 복사본이나 글꼴 복사본과 같은 리소스 중복이 포함될 수 있습니다. VintaSoft PDF .NET Plug-in은 PDF 문서에서 사용되지 않는 객체와 리소스 중복을 찾아 제거할 수 있습니다.

다음은 Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand 클래스를 사용하여 PDF 문서에서 사용되지 않는 개체와 리소스 중복을 제거하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Removes unused and duplicated resources in the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void RemoveUnsusedResources(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must remove duplicate resources from PDF document
    compressor.RemoveDuplicateResources = true;
    // specify that compressor must remove unused names resources from PDF document
    compressor.RemoveUnusedNamedResources = true;
    // specify that compressor must remove unused names from PDF document
    compressor.RemoveUnusedNames = true;
    // specify that compressor must remove unused pages from PDF document
    compressor.RemoveUnusedPages = true;
    // specify that compressor must remove invalid bookmarks from PDF document
    compressor.RemoveInvalidBookmarks = true;
    // specify that compressor must remove invalid links from PDF document
    compressor.RemoveInvalidLinks = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF 문서에서 메타데이터 및 기타 요소 제거

PDF 문서에는 메타데이터, 책갈피, 내장 파일, 대화형 양식, 페이지 미리보기 이미지, 구조 트리, 문서 정보 등과 같이 PDF 페이지 표시에는 영향을 미치지 않는 객체가 포함될 수 있습니다. VintaSoft PDF .NET Plug-in을 사용하면 문서에서 불필요한 객체를 제거할 수 있습니다.

다음은 Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand 클래스를 사용하여 PDF 문서에서 객체를 제거하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Removes metadata, bookmarks, document information, embedded files, embedded thumbnails, interactive form and structure tree of the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void RemoveObjects(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must remove metadata from PDF document
    compressor.RemoveMetadata = true;
    // specify that compressor must remove bookmarks from PDF document
    compressor.RemoveBookmarks = true;
    // specify that compressor must remove document information from PDF document
    compressor.RemoveDocumentInformation = true;
    // specify that compressor must remove embedded files from PDF document
    compressor.RemoveEmbeddedFiles = true;
    // specify that compressor must remove embedded thumbnails from PDF document
    compressor.RemoveEmbeddedThumbnails = true;
    // specify that compressor must remove interactive form from PDF document
    compressor.RemoveInteractiveForm = true;
    // specify that compressor must remove structure tree from PDF document
    compressor.RemoveStructureTree = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}


PDF 문서에서 주석 제거

VintaSoft PDF .NET Plug-in을 사용하면 PDF 페이지에서 불필요한 주석을 제거할 수 있습니다. 또한 주석은 PDF 페이지에 표시되어야 하지만 사용자가 주석과 상호 작용할 수 없도록 하려면 주석을 그래픽으로 변환(주석 평면화)할 수 있습니다.

다음은 Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand 클래스를 사용하여 PDF 문서의 주석을 그래픽으로 변환(주석 평면화)하고 대화형 양식을 제거하는 방법을 보여주는 C# 코드입니다.
/// <summary>
/// Flatten an annotations and remove intractive form of the PDF document.
/// </summary>
/// <param name="inPdfFilename">The input PDF filename.</param>
/// <param name="outPdfFilename">The output PDF filename.</param>
public static void FlattenAnnotations(string inPdfFilename, string outPdfFilename)
{
    // create compressor with empty compression settings
    Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand compressor =
       Vintasoft.Imaging.Pdf.Processing.PdfDocumentCompressorCommand.CreateEmptyCompressor();

    // specify that compressor must remove interactive form from PDF document
    compressor.RemoveInteractiveForm = true;
    // specify that compressor must flatten annotations in PDF document
    compressor.FlattenAnnotations = true;

    // compress PDF document
    compressor.Compress(inPdfFilename, outPdfFilename);
}