Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:49:15+00:00 2026-05-31T14:49:15+00:00

I’m trying to draw QR barcodes in a PDF file using iTextSharp. If I’m

  • 0

I’m trying to draw QR barcodes in a PDF file using iTextSharp. If I’m using English text the barcodes are fine, they are decoded properly, but if I’m using Chinese text, the barcode is decoded as question marks. For example this character ‘测’ (\u6D4B) is decoded as ‘?’. I tried all supported character sets, but none of them helped.
What combination of parameters should I use for the QR barcode in iTextSharp in order to encode correctly Chinese text?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:49:16+00:00Added an answer on May 31, 2026 at 2:49 pm

    iText and iTextSharp apparently don’t natively support this but you can write some code to handle this on your own. The trick is to get the QR code parser to work with just an arbitrary byte array instead of a string. What’s really nice is that the iTextSharp code is almost ready for this but doesn’t expose the functionality. Unfortunately many of the required classes are sealed so you can’t just subclass them, you’ll have to recreate them. You can either download the entire source and add these changes or just create separate classes with the same names. (Please check over the license to make sure you are allowed to do this.) My changes below don’t have any error correction so make sure you do that, too.

    The first class that you’ll need to recreate is iTextSharp.text.pdf.qrcode.BlockPair and the only change you’ll need to make is to make the constructor public instead of internal. (You only need to do this if you are creating your own code and not modifying the existing code.)

    The second class is iTextSharp.text.pdf.qrcode.Encoder. This is where we’ll make the most changes. Add an overload to Append8BitBytes that looks like this:

    static void Append8BitBytes(byte[] bytes, BitVector bits) {
        for (int i = 0; i < bytes.Length; ++i) {
            bits.AppendBits(bytes[i], 8);
        }
    }
    

    The string version of this method converts text to a byte array and then uses the above so we’re just cutting out the middle man. Next, add a new overload to the constructor that takes in a byte array instead of a string. We’ll then just cut out the string detection part and force the system to byte-mode, otherwise the code below is pretty much the same.

        public static void Encode(byte[] bytes, ErrorCorrectionLevel ecLevel, IDictionary<EncodeHintType, Object> hints, QRCode qrCode) {
            String encoding = DEFAULT_BYTE_MODE_ENCODING;
    
            // Step 1: Choose the mode (encoding).
            Mode mode = Mode.BYTE;
    
            // Step 2: Append "bytes" into "dataBits" in appropriate encoding.
            BitVector dataBits = new BitVector();
            Append8BitBytes(bytes, dataBits);
    
            // Step 3: Initialize QR code that can contain "dataBits".
            int numInputBytes = dataBits.SizeInBytes();
            InitQRCode(numInputBytes, ecLevel, mode, qrCode);
    
            // Step 4: Build another bit vector that contains header and data.
            BitVector headerAndDataBits = new BitVector();
    
            // Step 4.5: Append ECI message if applicable
            if (mode == Mode.BYTE && !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding)) {
                CharacterSetECI eci = CharacterSetECI.GetCharacterSetECIByName(encoding);
                if (eci != null) {
                    AppendECI(eci, headerAndDataBits);
                }
            }
    
            AppendModeInfo(mode, headerAndDataBits);
    
            int numLetters = dataBits.SizeInBytes();
            AppendLengthInfo(numLetters, qrCode.GetVersion(), mode, headerAndDataBits);
            headerAndDataBits.AppendBitVector(dataBits);
    
            // Step 5: Terminate the bits properly.
            TerminateBits(qrCode.GetNumDataBytes(), headerAndDataBits);
    
            // Step 6: Interleave data bits with error correction code.
            BitVector finalBits = new BitVector();
            InterleaveWithECBytes(headerAndDataBits, qrCode.GetNumTotalBytes(), qrCode.GetNumDataBytes(),
                qrCode.GetNumRSBlocks(), finalBits);
    
            // Step 7: Choose the mask pattern and set to "qrCode".
            ByteMatrix matrix = new ByteMatrix(qrCode.GetMatrixWidth(), qrCode.GetMatrixWidth());
            qrCode.SetMaskPattern(ChooseMaskPattern(finalBits, qrCode.GetECLevel(), qrCode.GetVersion(),
                matrix));
    
            // Step 8.  Build the matrix and set it to "qrCode".
            MatrixUtil.BuildMatrix(finalBits, qrCode.GetECLevel(), qrCode.GetVersion(),
                qrCode.GetMaskPattern(), matrix);
            qrCode.SetMatrix(matrix);
            // Step 9.  Make sure we have a valid QR Code.
            if (!qrCode.IsValid()) {
                throw new WriterException("Invalid QR code: " + qrCode.ToString());
            }
        }
    

    The third class is iTextSharp.text.pdf.qrcode.QRCodeWriter and once again we just need to add an overloaded Encode method supports a byte array and that calls are new constructor created above:

        public ByteMatrix Encode(byte[] bytes, int width, int height, IDictionary<EncodeHintType, Object> hints) {
            ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
            if (hints != null && hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
                errorCorrectionLevel = (ErrorCorrectionLevel)hints[EncodeHintType.ERROR_CORRECTION];
    
            QRCode code = new QRCode();
            Encoder.Encode(bytes, errorCorrectionLevel, hints, code);
            return RenderResult(code, width, height);
        }
    

    The last class is iTextSharp.text.pdf.BarcodeQRCode which we once again add our new constructor overload:

        public BarcodeQRCode(byte[] bytes, int width, int height, IDictionary<EncodeHintType, Object> hints) {
            newCode.QRCodeWriter qc = new newCode.QRCodeWriter();
            bm = qc.Encode(bytes, width, height, hints);
        }
    

    The last trick is to make sure when calling this that you include the byte order mark (BOM) so that decoders know to decode this properly, in this case UTF-8.

    //Create an encoder that supports outputting a BOM
    System.Text.Encoding enc = new System.Text.UTF8Encoding(true, true);
    
    //Get the BOM
    byte[] bom = enc.GetPreamble();
    
    //Get the raw bytes for the string
    byte[] bytes = enc.GetBytes("测");
    
    //Combine the byte arrays
    byte[] final = new byte[bom.Length + bytes.Length];
    System.Buffer.BlockCopy(bom, 0, final, 0, bom.Length);
    System.Buffer.BlockCopy(bytes, 0, final, bom.Length, bytes.Length);
    
    //Create are barcode using our new constructor
    var q = new BarcodeQRCode(final, 100, 100, null);
    
    //Add it to the document
    doc.Add(q.GetImage());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.