【问题标题】:Why I am unable to change the QR Code's size in iText7?为什么我无法在 iText7 中更改二维码的大小?
【发布时间】:2020-05-28 12:13:50
【问题描述】:

如何更改二维码大小?

            using (iText.Kernel.Pdf.PdfReader _pdf_reader = 
                new iText.Kernel.Pdf.PdfReader("tmp/example.pdf"))
            {
                using (iText.Kernel.Pdf.PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(_pdf_reader, new iText.Kernel.Pdf.PdfWriter("tmp/output.pdf").SetSmartMode(true)))
                {
                    BarcodeQRCode qrc = new BarcodeQRCode("https://google.com");

                    PdfFormXObject xObject = qrc.CreateFormXObject(ColorConstants.BLACK, pdfDoc);

                    float _w = pdfDoc.GetPage(1).GetPageSize().GetWidth();
                    float _h = pdfDoc.GetPage(1).GetPageSize().GetHeight();
                    PdfCanvas canvas = new PdfCanvas(pdfDoc.GetPage(1));
                    canvas.SaveState();
                    canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
                    //canvas.Rectangle(_w - 90, _h - 90, 100, 100);
                    canvas.Fill();
                    canvas.RestoreState();
                    canvas.AddXObject(xObject, _w - qrc.GetBarcodeSize().GetWidth(), _h - qrc.GetBarcodeSize().GetHeight());
                }
            }

我试试:

 qrc.GetBarcodeSize().GetHeight();
 qrc.GetBarcodeSize().GetWidth();

它返回 33

我尝试将高度和宽度设置为 100,如下所示:

 qrc.GetBarcodeSize().SetHeight(100);
 qrc.GetBarcodeSize().SetWidth(100);

然后再次检查大小,但它一直返回33,这是一个错误吗?还是我错过了什么?

请帮忙

谢谢 唐

【问题讨论】:

    标签: itext7


    【解决方案1】:

    我尝试将高度和宽度设置为 100,如下所示:

    实际上,您无法以这种方式更改 QrCode 端。 实际上,QRcode 是一个 n*n 网格,其中 n 取决于一些参数作为 QR 码版本和纠错级别。 生成时,iText 使用可以适合内容的最小版本。在您的情况下,这是版本 4 (33*33)。

    更改文档中 QrCode 大小的最简单方法是使用接受 moduleSide 参数的 createFormXObject 方法版本。

    float moduleSize = 100/qrc.GetBarcodeSize().GetHeight();    
    qrc.createFormXObject(foreground, moduleSize, document)
    

    这里的模块大小是条码网格单元的大小(默认为 1)。

    【讨论】:

      【解决方案2】:

      iTxt 7 二维码大小受三个参数提示的影响,示例供您参考。

      //C# code
      //Prepare all necessary properties to create the qrcode
      IDictionary<EncodeHintType, Object> hints = new Dictionary<EncodeHintType, object>();
      
      //default character set (ISO-8859-1)
      hints[EncodeHintType.CHARACTER_SET] = "UTF-8";
      
      //Qrcode Error correction level L,M,Q,H
      //default ErrorCorrectionLevel.L
      hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L;
      
      //Qrcode minimal version level
      //default 4
      hints[EncodeHintType.MIN_VERSION_NR] = 6;
      
      string code = "Qrcode content here";
      BarcodeQRCode qrcode = new BarcodeQRCode(code, hints);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 2017-07-05
        • 2012-11-10
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多