使用ImageMagick和C#将PDF转换为TIFF

时间:2019-08-13 01:47:47

标签: c# imagemagick imagemagick.net

我有一个现有程序,该程序会根据在页面上查找条形码的方式来处理.pdf文件并将其拆分为多个.pdf文件。

程序使用ImageMagick和C#。

我想将其从输出pdf更改为输出tif。在下面的代码中寻找我可能会进行更改的注释。

我添加了ImageMagick标记是因为有人可能会提供命令行选项,其他人可以帮助我将其转换为C#。

private void BurstPdf(string bigPdfName, string targetfolder)
{
    bool outputPdf = true;  // change to false to output tif.
    string outputExtension = "";

    var settings = new MagickReadSettings { Density = new Density(200) };

    string barcodePng = Path.Combine("C:\TEMP", "tmp.png");

    using (MagickImageCollection pdfPageCollection = new MagickImageCollection())
    {
        pdfPageCollection.Read(bigPdfName, settings);

        int inputPageCount = 0;
        int outputPageCount = 0;
        int outputFileCount = 0;
        MagickImageCollection resultCollection = new MagickImageCollection();
        string barcode = "";
        string resultName = "";
        IBarcodeReader reader = new BarcodeReader();
        reader.Options.PossibleFormats = new List<BarcodeFormat>();
        reader.Options.PossibleFormats.Add(BarcodeFormat.CODE_39);
        reader.Options.TryHarder = false;

        foreach (MagickImage pdfPage in pdfPageCollection)
        {
            MagickGeometry barcodeArea = getBarCodeArea(pdfPage);
            IMagickImage barcodeImg = pdfPage.Clone();

            barcodeImg.ColorType = ColorType.Bilevel;
            barcodeImg.Depth = 1;
            barcodeImg.Alpha(AlphaOption.Off);

            barcodeImg.Crop(barcodeArea);
            barcodeImg.Write(barcodePng);

            inputPageCount++;
            using (var barcodeBitmap = new Bitmap(barcodePng))
            {
                var result = reader.Decode(barcodeBitmap);
                if (result != null)
                {
                    // found a first page because it has bar code.
                    if (result.BarcodeFormat.ToString() == "CODE_39")
                    {

                        if (outputFileCount != 0)
                        {
                            // write out previous pages.
                            if (outputPdf) {
                                outputExtension = ".pdf";
                            } else {
                                // What do I put here to output a g4 compressed tif?
                                outputExtension = ".tif";
                            }
                            resultName = string.Format("{0:D4}", outputFileCount) + "-" + outputPageCount.ToString() + "-" + barcode + outputExtension;
                            resultCollection.Write(Path.Combine(targetfolder, resultName));
                            resultCollection = new MagickImageCollection();
                        }
                        barcode = standardizePhysicalBarCode(result.Text);

                        outputFileCount++;
                        resultCollection.Add(pdfPage);
                        outputPageCount = 1;

                    }
                    else
                    {
                        Console.WriteLine("WARNING barcode is not of type CODE_39 so something is wrong. check page " + inputPageCount + " of " + bigPdfName);
                        if (inputPageCount == 1)
                        {
                            throw new Exception("barcode not found on page 1.  see " + barcodePng);
                        }
                        resultCollection.Add(pdfPage);
                        outputPageCount++;
                    }
                }
                else
                {
                    if (inputPageCount == 1)
                    {
                        throw new Exception("barcode not found on page 1.  see " + barcodePng);
                    }
                    resultCollection.Add(pdfPage);
                    outputPageCount++;
                }
            }

            if (File.Exists(barcodePng))
            {
                File.Delete(barcodePng);
            }

        }

        if (resultCollection.Count > 0)
        {
            if (outputPdf) {
                outputExtension = ".pdf";
            } else {
                // What do I put here to output a g4 compressed tif?
                outputExtension = ".tif";
            }
            resultName = string.Format("{0:D4}", outputFileCount) + "-" + outputPageCount.ToString() + "-" + barcode + outputExtension;
            resultCollection.Write(Path.Combine(targetfolder, resultName));
            outputFileCount++;
        }
    }
}

[编辑]上面的代码是我使用的(未经测试的修改)将.pdf拆分为其他.pdf。我想知道如何修改此代码以输出tiff。我在代码中添加了一条我认为会进行更改的注释。

[EDIT]因此,在@ fmw42的鼓励下,我只运行了启用了.tif扩展名的代码。看起来它确实转换为.tif,但是tif未被压缩。 IM只是根据文件的扩展名来配置输出,这让我感到惊讶。我想很方便,但似乎有点松动。

[编辑]我想通了。尽管违反直觉的设置会在读取文件时设置压缩率。我正在阅读.pdf,但我将压缩设置为Group,如下所示:

var settings = new MagickReadSettings { Density = new Density(200), Compression = CompressionMethod.Group4 };

我了解到的是,简单地命名输出文件.tif告诉IM输出一个tif。这是一种方便的方法,但是看起来很草率。

0 个答案:

没有答案
相关问题