首页 > 解决方案 > Image' is an ambiguous reference between 'System.Drawing.Image' and 'iText.Layout.Element.Image'

问题描述

With that code I can split a multi tiff and save the images to files.

    public void SplitImage(string file)
    {
        Bitmap bitmap = (Bitmap)Image.FromFile(file);
        int count = bitmap.GetFrameCount(FrameDimension.Page);
        var new_files = file.Split("_");
        String new_file = new_files[new_files.Length - 1];
        for (int idx = 0; idx < count; idx++)
        {
            bitmap.SelectActiveFrame(FrameDimension.Page, idx);
            
            bitmap.Save($"C:\\temp\\{idx}-{new_file}", ImageFormat.Tiff);
        }
    }

here the code for the Pdf creation

    public void CreatePDFFromImages(string path_multi_tiff)
    {
        Image img = new Image(ImageDataFactory.Create(path_multi_tiff));
        var p = new Paragraph("Image").Add(img);

        var writer = new PdfWriter("C:\\temp\\test.pdf");
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf);
        document.Add(new Paragraph("Images"));
        document.Add(p);
        document.Close();
        Console.WriteLine("Done !");
    }

now I would like to save the images to pdf pages and tried it with iText7. But this fails as

using System.Drawing.Imaging;
using Image = iText.Layout.Element.Image;

are to close to have them both in the same class. How could I save the splitted images to PDF pages ? I would like to avoid saving first to files and reloading all the images.

标签: c#itext7

解决方案


The line

using Image = iText.Layout.Element.Image;

is a so-called using alias directive. It creates the alias Image for the namespace or type iText.Layout.Element.Image. If this poses a problem, you can simply create a different alias. For example

using A = iText.Layout.Element.Image;

will create the alias A for the namespace or type iText.Layout.Element.Image.


推荐阅读