为什么我无法在 iText7 中更改二维码的大小?
Why I am unable to change the QR Code's size in iText7?
如何修改二维码大小?
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();
它returns33
我尝试将高度和宽度设置为 100,如下所示:
qrc.GetBarcodeSize().SetHeight(100);
qrc.GetBarcodeSize().SetWidth(100);
然后再检查size,还是一直返回33,是不是bug?还是我错过了什么?
请帮忙
谢谢
唐
I try to set Height & Width to 100 like below:
实际上,您不能通过这种方式更改二维码的一面。
事实上,QRcode 是一个 n*n 网格,其中 n 取决于一些参数,如 QR 码版本和纠错级别。
生成时,iText 使用可以适合内容的最小版本。这是您的版本 4 (33*33)。
更改文档中 QrCode 大小的最简单方法是使用接受 moduleSide
参数的 createFormXObject
方法版本。
float moduleSize = 100/qrc.GetBarcodeSize().GetHeight();
qrc.createFormXObject(foreground, moduleSize, document)
此处的模块大小是条形码网格单元格的大小(默认为 1)。
iText7二维码大小受hints中三个参数的影响,example范例供大家参考。
//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);
如何修改二维码大小?
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();
它returns33
我尝试将高度和宽度设置为 100,如下所示:
qrc.GetBarcodeSize().SetHeight(100);
qrc.GetBarcodeSize().SetWidth(100);
然后再检查size,还是一直返回33,是不是bug?还是我错过了什么?
请帮忙
谢谢 唐
I try to set Height & Width to 100 like below:
实际上,您不能通过这种方式更改二维码的一面。 事实上,QRcode 是一个 n*n 网格,其中 n 取决于一些参数,如 QR 码版本和纠错级别。 生成时,iText 使用可以适合内容的最小版本。这是您的版本 4 (33*33)。
更改文档中 QrCode 大小的最简单方法是使用接受 moduleSide
参数的 createFormXObject
方法版本。
float moduleSize = 100/qrc.GetBarcodeSize().GetHeight();
qrc.createFormXObject(foreground, moduleSize, document)
此处的模块大小是条形码网格单元格的大小(默认为 1)。
iText7二维码大小受hints中三个参数的影响,example范例供大家参考。
//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);