天天看點

向PDF檔案添加QRcode二維碼

向PDF檔案添加QRcode二維碼

   首先在VS2013建立C#的Windows Form應用程式,并向窗體添加兩個button控件、一個Picturebox控件。

1 實作生成QRcode二維碼圖像。

1)添加引用zxing.dll檔案,并且添加相應2條using語句。

2)添加代碼如下。

using System;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

usingcom.google.zxing.qrcode;  //QRcode操作庫

usingcom.google.zxing.common;  //QRcode操作庫

namespacecsdnPDFQRcode20150408

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

pictureBox1.Image= GenQRBarcodeImg("StringstrQRBarContent",200, 200, "BarCodeType");

        }

        public System.Drawing.Image GenQRBarcodeImg(String strQRBarContent, int SizeWidth, int SizeHeight, String BarCodeType)

        {//以下為生成二維碼示例 by xuaimin

            System.Drawing.Image img = null;

            QRCodeWriter QRCWriter = new QRCodeWriter();

            com.google.zxing.common.ByteMatrix myByteMatrix = null;

            myByteMatrix =QRCWriter.encode(strQRBarContent, com.google.zxing.BarcodeFormat.QR_CODE, SizeWidth,SizeHeight);

            img = myByteMatrix.ToBitmap();

            return img;

        }

    }

}

2 實作将圖像和文字添加到PDF檔案上。

1)在本項目中繼續,添加引用檔案itextsharp.dll 和itextsharp.xtra.dll檔案。添加using如下:

        usingiTextSharp.text;         //PDF檔案操作

usingiTextSharp.text.pdf;     //PDF檔案操作

using System.IO;               //檔案讀寫操作

2)在C槽根目錄準備PDF檔案,名為"c:\\CheckList004.pdf"。

3)實作向每一頁右上角添加圖像和相應文字内容。

4)添加如下代碼

        private void button2_Click(object sender, EventArgs e)

        {

            String pdfS = ("c:\\CheckList004.pdf");//待加圖像的PDF檔案

            String pdfD = ("c:\\CheckList004added.pdf");//已加圖像的PDF檔案

            String strQRcodeContent="StringQRBarContentTest";

System.Drawing.Image qrImage =GenQRBarcodeImg(strQRcodeContent, 120, 120, "BarCodeType");

            AddBarcodeIMGtoPDF(pdfS, pdfD,qrImage, strQRcodeContent);

        }

        public void AddBarcodeIMGtoPDF(String PDFFileFullName, StringPDFFileFullNameAddedIMG, System.Drawing.Image qrImage, String strQRcodeContent)

        {

            try

            {

                // 建立一個PdfReader對象

                PdfReader reader = new PdfReader(PDFFileFullName);

                // 獲得文檔頁數

                int n =reader.NumberOfPages;

                // 獲得第一頁的大小

                iTextSharp.text.Rectangle psize =reader.GetPageSize(1);

                float width =psize.Width;

                float height =psize.Height;

                // 建立一個文檔變量

                Document document = new Document(psize, 50, 50, 50,50);//50頁面空白邊距

                // 建立該文檔

                PdfWriter writer = PdfWriter.GetInstance(document,new FileStream(PDFFileFullNameAddedIMG,FileMode.Create));

                document.Open(); // 打開文檔

                for (int iPageIndex = 1;iPageIndex <= n; iPageIndex++)

                {   // 添加内容

                    PdfContentByte cb =writer.DirectContent;

                    PdfImportedPage page11 =writer.GetImportedPage(reader, iPageIndex);

                    BaseFont bfbf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

                    iTextSharp.text.Image imglift =iTextSharp.text.Image.GetInstance((System.Drawing.Image)qrImage,System.Drawing.Imaging.ImageFormat.Bmp);

                    cb.AddTemplate(page11, 1.0f, 0, 0,1.0f, 0, 0);

                    cb.AddImage(imglift,imglift.Width * 2 / 3, 0, 0, imglift.Height * 2 / 3, width - 120, height - 95);//所添加圖像的比例,位置20140626

                    cb.BeginText();//添加文字

                    cb.SetFontAndSize(bfbf, 10);

cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,strQRcodeContent, width / 2, 30, 0);

cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,strQRcodeContent, width-80, height-100,0);

                    cb.EndText();

                    document.NewPage();

                    Application.DoEvents();

                }

                document.Close();//關閉文檔                               

            }

            catch (Exception de)

            {

                Console.Error.WriteLine(de.Message);

                Console.Error.WriteLine(de.StackTrace);

            }

        }

程式界面如下:

向PDF檔案添加QRcode二維碼