2

我当前的代码写了一个二维码,但它只用二维码覆盖了我的文件。我不知道如何调整二维码的大小以放置在文档的一个角落而不是占据整个页面。也不确定这是否RasterImage.Create意味着它只使用 qr 创建一个新文件并丢弃我的原始文件?

代码: - 将 PDF 转换为 Bmp 以添加 QR 然后保存回 PDF

 public void PDFFileExample()
    {
        RasterCodecs codecs1 = new RasterCodecs();

        codecs1.Options.Pdf.InitialPath = @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32";
        codecs1.Dispose();
        RasterCodecs codecs2 = new RasterCodecs();
        codecs2.ThrowExceptionsOnInvalidImages = true;
        System.Diagnostics.Debug.Assert(codecs2.Options.Pdf.InitialPath == @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32");

        string pdfFile = @"C:\QRCodeTesting\bottomRight.pdf";
        string destFileName1 = @"C:\QRCodeTesting\bottomRightOutputTemp.pdf";
        string destFileName2 = @"C:\QRCodeTesting\bottomRightOutput.bmp";

        RasterCodecs codecs = new RasterCodecs();

        if (codecs.Options.Pdf.IsEngineInstalled)
        {
            // Resulting image pixel depth.
            codecs.Options.Pdf.Load.DisplayDepth = 24;
            codecs.Options.Pdf.Load.GraphicsAlpha = 4;
            codecs.Options.Pdf.Load.Password = "";

            // Type of font anti-aliasing to use.
            codecs.Options.Pdf.Load.TextAlpha = 1;
            codecs.Options.Pdf.Load.UseLibFonts = true;

            // Horizontal,vertical  display resolution in dots per inch.
            codecs.Options.RasterizeDocument.Load.XResolution = 150;
            codecs.Options.RasterizeDocument.Load.YResolution = 150;

            using (RasterImage image = codecs.Load(pdfFile, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
            {

                // Set the PDF version to be v1.4
                codecs.Options.Pdf.Save.Version = CodecsRasterPdfVersion.V14;

                try
                {
                    // Save the image back as PDF
                    codecs.Save(image, destFileName1, RasterImageFormat.RasPdf, 24);
                }
                catch (RasterException ex)
                {
                    if (ex.Code == RasterExceptionCode.FileFormat)
                        MessageBox.Show(string.Format("Image in file {0} is loaded", destFileName1));
                    else
                    {
                        MessageBox.Show(string.Format("Could not load the file {0}.{1}{2}", destFileName1, Environment.NewLine, ex.Message));
                    }
                }
            }

            // And load it back before saving it as BMP
            using (RasterImage image = codecs.Load(destFileName1))
            {
                codecs.Save(image, destFileName2, RasterImageFormat.Bmp, image.BitsPerPixel);
                writeQRTag(destFileName2);
            }
        }
        else
        {
            MessageBox.Show("PDF Engine is not found!");
        }

        // Clean up
        codecs.Dispose();

    }

二维码写法

private void writeQRTag(string imageFileName)
    {
        BarcodeEngine engine = new BarcodeEngine();

        // Create the image to write the barcodes to
        int resolution = 300;
        using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.Red)))
        {
            // Write two QR barcodes
            WriteQRCode(engine.Writer, image, QRBarcodeSymbolModel.Model1AutoSize, "QR Data 1", true);

            // Save the image
            using (RasterCodecs codecs = new RasterCodecs())
            {
                codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1);
            }
        }
    }
4

1 回答 1

2

这是来自 LEADTOOLS 支持的 Maen。

我检查了您的代码并注意到以下内容:

1) 当您调用 RasterImage.Create() 方法时,它将创建一个新的 RasterImage 对象,其中包含一个空的红色图像,您随后将其传递给 writeQRTag() 函数,然后使用给定的文件名保存。当你保存它时,红色被黑色代替,因为你使用的文件格式只支持黑白。由于您使用的是新图像,因此旧图像会丢失(覆盖)。

如果您想在原始文件中的图像上写入条形码,则不应创建新图像。相反,您需要使用已使用 codecs.Load() 加载的图像并在其上写入条形码。

2) 代码执行多次加载和保存操作。通常,您不需要这样做,除非您的应用程序需要不同的文件格式(PDF、BMP 和 TIFF)。

3) 您创建了 RasterCodecs 对象的不同实例,但实际上只使用了其中一个。代码中不需要 4 个 RasterCodecs 对象中的 3 个。

如果您仍然遇到使用我们工具包的代码的问题,您可以通过电子邮件将详细信息发送至support@leadtools.com,我们将尽力为您提供帮助。

于 2013-05-20T14:11:24.023 回答