向PDF文件添加QRcode二维码

来源:互联网 发布:莎布 尼古拉斯trpg数据 编辑:程序博客网 时间:2024/06/16 11:34

向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,newFileStream(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);

            }

        }

程序界面如下:


0 0