を追加、削除、抽出し、画像をPDFでC


PDFは、最も人気のある広く使用されるファイル形式のいずれかになっている.画像はPDFファイルの重要な部分です.PDFファイル内の画像の追加、削除、抽出、置換は、通常、Adobe Acrobat Pro DC . この記事では、私たちはどのようにPDFファイルのC Chorzを使用して我々のsyncfusion PDFライブラリの助けを借りてこれを行う方法を学びます.
統合PDF Library は.Adobe Dependencyへの参照なしでPDFファイルでイメージを加えて、操作することができるネットPDF図書館.
以下にこの記事をご覧ください.
  • Getting started with app creation
  • Add an image in a PDF using C#
  • Extract images from a PDF using C#
  • Remove images from a PDF using C#
  • Replace image in a PDF using C#
  • アプリの作成から始める

  • クリエイトアConsole application using Visual Studio .
  • パッケージマネージャーコンソールを開きます.
  • 以下のコマンドを実行して、Syncfusion PDF .NET Core NuGet package .
  • パッケージのsyncfusionをインストールします.PDFファイル.ネットコア

    を使用してPDFファイルに画像を追加する


    これらの手順に従って、SyncFusion PDFライブラリを使用してPDFファイルにイメージを追加します.
  • PDFileDedDocumentクラスを使用して既存のPDFドキュメントを読み込みます.
  • 次に、ページを参照してください.
  • さて、PDFGraphicsオブジェクトをページから取得します.
  • 画像を* PDFBitmap *オブジェクトに読み込みます.
  • DrawImageメソッドを使用してページに画像を描画します.
  • 最後に、ファイルを保存します.
  • 以下のコード例は、Cのファイルを使用してPDFファイルに画像を追加する方法を示します.
    using System.IO;
    using Syncfusion.Drawing;
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    using Syncfusion.Pdf.Parsing;
    
    namespace Add_image
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Load a PDF document.
                PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../Data/input.pdf",FileMode.Open));
                //Get first page from document.
                PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
                //Create PDF graphics for the page.
                PdfGraphics graphics = page.Graphics;
                //Load the image from the disk.
                PdfBitmap image = new PdfBitmap(new FileStream("../../../../../Data/Sample.jpg",FileMode.Open));
                //Draw the image.
                graphics.DrawImage(image, new RectangleF(50, 150, 400, 250));
                //Create stream object to save file.
                FileStream stream = new FileStream("Output.pdf", FileMode.Create);
                //Save the document.
                doc.Save(stream);
                //Close the document.
                doc.Close(true);
                //Close stream.
                stream.Close();
            }
        }
    }
    
    このコード例を実行すると、次のスクリーンショットに似たPDFドキュメントが表示されます.
    イメージをPDFでC

    CからPDFを抽出する


    以下の手順でPDFファイルからすべての画像を抽出する方法を見てみましょう.
  • PDFileDedDocumentクラスを使用して既存のPDFドキュメントを読み込みます.
  • PDFileAddPageオブジェクト内のページ(画像を含む)の参照を取得します.
  • 次に、メソッドの抽出を呼び出して、特定のページからすべての画像を展開します.
  • 最後に、ディスク上の抽出画像を保存します.
  • 注意:インストールSyncfusion.Pdf.Imaging.Net.Core 画像を抽出するNuGetパッケージ.NETコアアプリケーション.
    既存のPDFドキュメントからCを使用して画像を抽出する方法を次のコード例に示します.
    using System.Drawing;
    using System.IO;
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Exporting;
    using Syncfusion.Pdf.Parsing;
    
    namespace Extract_images
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Load the template document.
                PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../Data/ImageInput.pdf",FileMode.Open));
                //Load the first page.
                PdfPageBase pageBase = doc.Pages[0];
                //Extract images from first page.
                Image[] extractedImages = pageBase.ExtractImages();
                //Save images to file.
                for (int i = 0; i < extractedImages.Length; i++)
                {
                    extractedImages[i].Save("Image" + i + ".jpg");
                }
    
                //Close the document.
                doc.Close(true);
    
            }
        }
    }
    

    CからPDFを削除する


    簡単に私たちのsyncfusion PDFライブラリの助けを借りてPDF文書から不要な画像を削除することができます.これはファイルサイズをかなり減らすでしょう.
    PDFドキュメントからイメージを削除するには、次の手順に従います
  • PDFileDedDocumentクラスを使用して既存のPDFドキュメントを読み込みます.
  • ページのリファレンスを取得します.
  • 次に、RemoveImageメソッドをPDFImageInfoオブジェクトで呼び出し、イメージを削除します.
  • 最後に、変更されたPDFドキュメントを保存します.
  • 注意:インストールSyncfusion.Pdf.Imaging.Net.Core Nugetパッケージでイメージを削除します.NETコアアプリケーション.
    既存のPDFドキュメントからCを使用してイメージを削除する方法を次のコード例に示します.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Exporting;
    using Syncfusion.Pdf.Parsing;
    using System.IO;
    
    namespace Remove_images
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Load the template document.
                PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../Data/ImageInput.pdf",FileMode.Open));
                //Get first page of the document.
                PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
                //Remove first image in the page.
                page.RemoveImage(page.GetImagesInfo()[0]);
                FileStream stream = new FileStream("output.pdf", FileMode.Create);
                //Save the modified document to file.
                doc.Save(stream);
                //Close the PDF document.
                doc.Close(true);
                stream.Close();
    
            }
        }
    }
    
    このコード例を実行すると、次のスクリーンショットに似た出力が得られます.
    Cからのイメージを削除する

    画像をPDFで置き換える


    既存のイメージを新しいイメージに置き換えるか、既存の画像をPDFドキュメントで変更できます.
    そのためには、
  • まず、置換する必要のある画像を削除します.
  • その後、削除された画像がどこに新しいイメージを描画します.
  • 注:インストールしてくださいSyncfusion.Pdf.Imaging.Net.Core NuGetパッケージで画像を置換する.NETコアアプリケーション.
    次の例では、既存の画像をPixelated画像で置換しました.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Exporting;
    using Syncfusion.Pdf.Graphics;
    using Syncfusion.Pdf.Parsing;
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace Replace_image
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Load the template document.
                PdfLoadedDocument doc = new PdfLoadedDocument(new FileStream("../../../../../../Data/ImageInput.pdf",FileMode.Open));
                //Get first page of the document.
                PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;
                //Get image info of the first image.
                PdfImageInfo pdfImageInfo = page.GetImagesInfo()[0];
                RectangleF bounds = pdfImageInfo.Bounds;
                //Remove first image in the page.
                page.RemoveImage(pdfImageInfo);
                //Pixlate the existing image.
                Stream pixlated = Pixelate((Bitmap)pdfImageInfo.Image, new Rectangle(0, 0, pdfImageInfo.Image.Width, pdfImageInfo.Image.Height), 15);
                //Draw the pixlated image in the existing image bounds.
                page.Graphics.DrawImage(new PdfBitmap(pixlated), bounds.X,bounds.Y,bounds.Width,bounds.Height);
    
                FileStream stream = new FileStream("output.pdf", FileMode.Create);
                //Save the modified document to file.
                doc.Save(stream);
                //Close the PDF document.
                doc.Close(true);
    
            }
    
            private static Stream Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize)
            {
                Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height);
    
                // Make an exact copy of the bitmap provided.
                using (Graphics graphics = System.Drawing.Graphics.FromImage(pixelated))
                    graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
                        new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
    
                // look at every pixel in the rectangle while making sure we're within the image bounds.
                for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize)
                {
                    for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize)
                    {
                        Int32 offsetX = pixelateSize / 2;
                        Int32 offsetY = pixelateSize / 2;
    
                        // Make sure that the offset is within the boundry of the image.
                        while (xx + offsetX >= image.Width) offsetX--;
                        while (yy + offsetY >= image.Height) offsetY--;
    
                        // Get the pixel color in the center.
                        Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY);
    
                        // For each pixel in the pixelate size, set it to the center color.
                        for (Int32 x = xx; x < xx + pixelateSize && x < image.Width; x++)
                            for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++)
                                pixelated.SetPixel(x, y, pixel);
                    }
                }
    
                MemoryStream stream = new MemoryStream();
                pixelated.Save(stream, ImageFormat.Jpeg);
                stream.Position = 0;
                pixelated.Dispose();
                return stream;
            }
        }
    }
    
    このコード例を実行すると、次のスクリーンショットに似たイメージが得られます.
    Cでの画像の置換

    リソース


    また、Githubの完全な例を得ることができます.Add, Extract, Remove, and Replace Images from PDF documents using C# .

    結論


    読書ありがとう!このブログの投稿では、C Countを使用してPDFドキュメント内の画像を追加、抽出、削除、置換する方法を学びました.核融合PDF Library PDFビューアコントロールを表示、レビュー、およびPDFファイルを印刷を提供します.その強力な変換APIを簡単にHTML、Word、Excel、PowerPoint、およびPDF形式に画像を変換することができます.一時停止するdocumentation , 他のオプションと機能、すべての付随するコード例を見つけます.
    一つのイメージは、1000の語を言うことができます.そこで、簡単にPDFファイルを使用して画像を処理するために我々のsyncfusion PDFライブラリを使用しましょう!
    あなたがこれらの機能についての質問をするならば、我々にこのブログ柱のコメント部で知らせてください.また、我々を介してお問い合わせすることができますsupport forum , Direct-Trac , or feedback portal . 私たちはあなたを支援して満足している!

    関連ブログ




  • 7 Ways to Compress PDF Files in C#, VB.NET