当前位置: 首页 > 工具软件 > PDFsharp > 使用案例 >

c#使用pdfSharp制作pdf(条形码+图片+去除水印)

吕霍英
2023-12-01

 需要引用的主键:

1.using PdfSharp;

  try {
                string png1 = Application.StartupPath + @"\aramexConfig\Png\top.jpg";
                string png2 = Application.StartupPath+ @"\aramexConfig\Png\down.jpg";
                Dictionary<string, object> aramexShippmentMap = (Dictionary<string, object>)obj;
                AramexShippment aramexShippment =(AramexShippment)aramexShippmentMap["aramexShippment"];
                string labelPath = aramexShippmentMap["labelPath"].ToString();
                string labelName = aramexShippmentMap["labelName"].ToString();
                if (File.Exists(labelPath+ labelName+ ".JPG"))
                {//文件存在就删除
                    File.Delete(labelPath + labelName + ".JPG");
                }
                int margin_left_right = 50;//左右边距
                int margin_top_bottom = 80;//上下边距(这个高度尽量不动,可以去除水印)
                //1. 定义文档对象
                PdfDocument document = new PdfDocument();
                //2. 新增一页
                PdfPage page = document.AddPage();
                // 设置纸张大小
                page.Size = PageSize.A4;
                //3. 创建一个绘图对象
                XGraphics gfx = XGraphics.FromPdfPage(page);

                XFont font = new XFont("华文宋体", 10, XFontStyle.Bold);
                //定制化内容开始
                int cur_x = 0 + margin_left_right;
                int cur_y = 0 + margin_top_bottom - 30;
                int end_cur_x = cur_x + 250;//横向最大值
                double ContentWight = (page.Width - 2 * cur_x - 10);
                //自动换行参数
                int cnt = 0;
                int height = 0;
                string new_content = "";
                int total_lines = 0;

                cur_y = cur_y + 10;
                
                XImage im = XImage.FromFile(png1);
                gfx.DrawImage(im, cur_x, cur_y, cur_x + 70, 30);
                cur_y = cur_y + 30;
                gfx.DrawString("热线电话 020-83512000", font, XBrushes.Black, new XRect(cur_x, cur_y, cur_x + 70, 20), XStringFormats.CenterLeft);
                //一条横线
                cur_y = cur_y + 50;
                XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1);
                gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y);
                font = new XFont("华文宋体", 8, XFontStyle.Regular);
                cur_y = cur_y + 5;
                gfx.DrawString(" 件数 Packages", font, XBrushes.Black, new XRect(cur_x, cur_y, 50, 10), XStringFormats.CenterLeft);
                gfx.DrawString(aramexShippment.PCS, font, XBrushes.Black, new XRect(cur_x+5, cur_y + 15, 50, 10), XStringFormats.CenterLeft);
                gfx.DrawString(" 重量 Weight ", font, XBrushes.Black, new XRect(end_cur_x - 30, cur_y, 50, 10), XStringFormats.CenterLeft);
                gfx.DrawString(aramexShippment.Weight+" KG", font, XBrushes.Black, new XRect(end_cur_x - 26, cur_y + 13, 50, 10), XStringFormats.CenterLeft);
                gfx.DrawString(" 价值 Value ", font, XBrushes.Black, new XRect(page.Width - 2 * cur_x - 30, cur_y, 50, 10), XStringFormats.CenterLeft);
                gfx.DrawString(aramexShippment.DeclaredValue+" "+ aramexShippment.DeclaredCurrency, font, XBrushes.Black, new XRect(page.Width - 2 * cur_x - 26, cur_y + 13, 50, 10), XStringFormats.CenterLeft);
                //竖线
                gfx.DrawLine(pen, cur_x, cur_y - 5, cur_x, cur_y + 260);//左
                gfx.DrawLine(pen, page.Width - cur_x, cur_y - 5, page.Width - cur_x, cur_y + 260);//右
                //横线
                cur_y = cur_y + 40;
                gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y);
                //计算矩形
                XTextFormatter tf = new XTextFormatter(gfx);
                //发件人
                string shipperStr = aramexShippment.ShipperAdd1+","+ aramexShippment.OriginCity+","+ aramexShippment.OriginState+","+ aramexShippment.OriginCountry;
                gfx.DrawString(" 寄件人 From: "+ aramexShippment.ShipperEnglish+" /"+ aramexShippment.ShipperContact, font, XBrushes.Black, new XRect(cur_x, cur_y + 5, 50, 10), XStringFormats.CenterLeft);
                XRect rect = new XRect(cur_x, cur_y + 20, ContentWight, 40);
                tf.DrawString(GetNewContent(shipperStr, gfx, ContentWight, ref cnt, ref height, ref new_content, ref total_lines), font, XBrushes.Black, rect, XStringFormats.TopLeft);

                //gfx.DrawString(shipperStr, font, XBrushes.Black, new XRect(cur_x, cur_y + 5, page.Width - 2 * cur_x - 30, 50), XStringFormats.CenterLeft);
                cur_y = cur_y + 50;
                gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y);
                //收件人
                string consignstr = aramexShippment.CneeAdd1 + "," + aramexShippment.CneeCity + "," + aramexShippment.CneeState + "," + aramexShippment.DestCountry;
                gfx.DrawString(" 收件人 ShipTo: " + aramexShippment.Consignee + " /" + aramexShippment.CneeContact , font, XBrushes.Black, new XRect(cur_x, cur_y + 5, 50, 10), XStringFormats.CenterLeft);
                rect = new XRect(cur_x, cur_y + 20, ContentWight, 40);
                tf.DrawString(GetNewContent(consignstr, gfx, ContentWight, ref cnt, ref height, ref new_content, ref total_lines), font, XBrushes.Black, rect, XStringFormats.TopLeft);
                gfx.DrawString(" + "+ aramexShippment.CneeTel, font, XBrushes.Black, new XRect(cur_x, cur_y + 50, 30, 5), XStringFormats.CenterLeft);
                cur_y = cur_y + 60;
                gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y);
                //内件品名
                string descriptionItems = aramexShippment.DescriptionEN;
                gfx.DrawString(" 内物品名 Description ", font, XBrushes.Black, new XRect(cur_x, cur_y + 5, 50, 10), XStringFormats.CenterLeft);
                rect = new XRect(cur_x, cur_y + 20, ContentWight, 40);
                tf.DrawString(GetNewContent(descriptionItems, gfx, ContentWight, ref cnt, ref height, ref new_content, ref total_lines), font, XBrushes.Black, rect, XStringFormats.TopLeft);
                gfx.DrawString("参考号:«参考号»", font, XBrushes.Black, new XRect(page.Width - 2 * cur_x - 30, cur_y + 50, 30, 5), XStringFormats.CenterLeft);
                cur_y = cur_y + 60;
                gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y);
                //条形码
                font = new XFont("Free 3 of 9 Extended", 35, XFontStyle.Regular);
                gfx.DrawString("*"+ aramexShippment .HAWBHAWB+ "*", font, XBrushes.Black, new XRect(cur_x + 170, cur_y, page.Width - 2 * cur_x - 30, 40), XStringFormats.CenterLeft);
                //单号
                cur_y = cur_y + 30;
                font = new XFont("AdobeSongStd-Light", 20, XFontStyle.Regular);
                gfx.DrawString(aramexShippment.HAWBHAWB, font, XBrushes.Black, new XRect(cur_x + 190, cur_y, page.Width - 2 * cur_x - 30, 20), XStringFormats.CenterLeft);

                cur_y = cur_y + 20;
                gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y);
              
                XImage im2 = XImage.FromFile(png2);
                gfx.DrawImage(im2, page.Width - 2 * cur_x - 110, cur_y + 10, 140, 30);

                //document.Save(labelPath);//保存到本地的pdf
                MemoryStream outstream = new MemoryStream();
                document.Save(outstream);//保存到缓存流中
                outstream.Close();
                document.Close();
            } catch (Exception ex) {
               string error=ex.Message.ToString();
            }

 类似资料: