본문 바로가기
C#

BitMiracle.Docotic.Pdf를 이용한 Tiff To Pdf 변환 예제

by Hwoarang757 2024. 6. 16.

BitMiracle.Docotic.Pdf를 이용한 Tiff To Pdf 변환 예제입니다.

 

BitMiracle.Docotic.Pdf 는 유료입니다. , 31일 동안의 무료 라이선스를 받을 수 있더라구요  , 라이선스를 등록 하지 않으면 
변환 시마다 왼쪽 상단에 메시지가 표시 됩니다.

 

출처 : https://bitmiracle.com/pdf-library/

 

C# PDF library | Docotic.Pdf for .NET

PDF library for C# and VB.NET Docotic.Pdf is a high-performance C# PDF library for .NET. You can use it to create, read, and edit PDF documents in .NET Core, ASP.NET, Windows Forms, WPF, Xamarin, Blazor, Unity, and HoloLense applications. The library suppo

bitmiracle.com

// See https://aka.ms/new-console-template for more information
global using BitMiracle.Docotic.Pdf;
using System.Drawing;
using System.Drawing.Imaging;

using (Image image = Image.FromFile(@"D:\document\팩스이미지1.tif"))
using (MemoryStream ms = new MemoryStream()) 
{
    int pageCount = image.GetFrameCount(FrameDimension.Page);

    for(int i = 0; i < pageCount; i++)
    {
        image.SelectActiveFrame(FrameDimension.Page , i);

        if(i == 0) 
            image.Save(ms, GetCodecForstring("TIFF"), GetFaxTifEncoderParamsForFirstpage());
        else
            image.SaveAdd(GetFaxTifEncoderParamsForAdditionalpage());
    }

    image.Dispose();

    using (PdfDocument pdf = new PdfDocument())
    {
        PdfImageFrames frames = pdf.OpenImage(ms.ToArray());

        int imagesAdded = 0;
        frames.ToList().ForEach(a =>
        {
            if (imagesAdded != 0) pdf.AddPage();

            PdfImage image = pdf.AddImage(a);
            PdfPage pdfPage = pdf.Pages[pdf.PageCount - 1];

            pdfPage.Height = image.Height;
            pdfPage.Width = image.Width;
            pdfPage.Canvas.DrawImage(image, 0, 0, pdfPage.Width, pdfPage.Height, 0);
            imagesAdded++;
        });

        pdf.Save(@"D:\document\팩스이미지1.pdf");
    }

}

ImageCodecInfo GetCodecForstring(string type)
{
    ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();

    for (int i = 0; i < info.Length; i++)
    {
        string EnumName = type.ToString();
        if (info[i].FormatDescription.Equals(EnumName))
        {
            return info[i];
        }
    }
    return null;
}

EncoderParameters GetFaxTifEncoderParamsForFirstpage()
{
    Encoder saveEncoder = Encoder.SaveFlag;
    Encoder compressionEncoder = Encoder.Compression;

    // Save the first page (frame).
    EncoderParameter SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame);
    EncoderParameter CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);

    EncoderParameters EncoderParams = new EncoderParameters(2);
    EncoderParams.Param[0] = CompressionEncodeParam;
    EncoderParams.Param[1] = SaveEncodeParam;

    return EncoderParams;

}

EncoderParameters GetFaxTifEncoderParamsForAdditionalpage()
{
    Encoder saveEncoder = Encoder.SaveFlag;
    Encoder compressionEncoder = Encoder.Compression;

    EncoderParameter SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
    EncoderParameter CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionCCITT4);

    EncoderParameters EncoderParams = new EncoderParameters(2);
    EncoderParams.Param[0] = CompressionEncodeParam;
    EncoderParams.Param[1] = SaveEncodeParam;

    return EncoderParams;

}

 

PDF로 변환 한 결과 입니다.