代码之家  ›  专栏  ›  技术社区  ›  Be Kind To New Users

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

  •  0
  • Be Kind To New Users  · 技术社区  · 5 年前

    我有一个现有的程序,它可以进行一些处理。pdf文件,并将其拆分为多个文件。基于在页面上查找条形码的pdf文件。

    该程序使用ImageMagick和C#。

    我想把它从输出PDF改为输出tifs。请在下面的代码中查找注释,我猜在哪里会做出更改。

    我加入了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++;
            }
        }
    }
    

    [EDIT]上面的代码就是我用来拆分一个。pdf格式转换成其他格式。PDF。我想知道如何修改此代码以输出TIFF。我在代码中添加了一条注释,我认为更改会发生在哪里。

    [EDIT]受到@fmw42的鼓励,我用。tif扩展已启用。看起来它确实变成了一个。tif,但tif不会被压缩。我很惊讶IM只是根据文件的扩展名配置输出。我想是Handy,不过看起来有点松。

    [编辑]我想出来了。尽管有一些与直觉相反的方法会在读取文件时设置压缩。我正在读一本书。pdf,但我将压缩设置为Group,如下所示:

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

    我学到的是简单地命名输出文件。tif告诉IM输出一个tif。这是一个方便的方法,但它似乎太草率了。

    0 回复  |  直到 5 年前