首先要安装ABCpdf.NET
using WebSupergoo.ABCpdf9;
- public void getPdf(string name,string html)
- {
- Random r = new Random();
- Doc theDoc = new Doc();
- theDoc.TopDown = true;
- theDoc.Rect.String = "22 15 820 580";//控制显示的大小205 300 632 895
- theDoc.MediaBox.String = "0 0 842 595";//控制页面的大小
- string reg = @"/<a id/=(.)+/</a/>";//这段正则主要是去掉页面中自己不需要显示的东西
- string reg1 = @"body/{(.)+/}";
- //string temp = Regex.Replace(Regex.Replace( GetHtmlData("~/硬件试产发布单/HPTrialRelease.aspx"), reg1, ""), reg, "");
- //temptemp = temp.Replace("{$name$}", name.Split(',')[0]).Replace("{$group$}", group);
- //string html = GetHtmlData("~/硬件试产发布单/HPTrialRelease.aspx");
-
- int theID = theDoc.AddImageHtml(html, true, 0, false);
- while (true)
- {
- if (!theDoc.Chainable(theID))
- {
- break;
- }
- theDoctheDoc.Page = theDoc.AddPage();
- theID = theDoc.AddImageToChain(theID);
- }
- XImage theImg = new XImage();
- theImg.SetFile(@"D:\项目源代码\DefaultCollection\代码库\保存PDF文件\SaveToPDF\Portal\WFRes\p_w_picpaths\checked.gif");
- theDoc.TopDown = true;
- theDoc.Rect.Left = 100;
- theDoc.Rect.Top = 100;
- theDoc.Rect.Width = theImg.Width;
- theDoc.Rect.Height = theImg.Height;
- theDoc.Rect.Magnify(1, 1); //把图像放大2倍
- theDoc.AddImageObject(theImg, false);
- byte[] theData = theDoc.GetData();
- FileCreate(name, theData);
- if (File.Exists(HttpContext.Current.Server.MapPath(name + ".pdf")))
- {
- HttpContext.Current.Response.Write(string.Format("<a target='_blank' href='{0}'>下载</a>", name + ".pdf"));
- }
- }
-
- public static void FileCreate(string name, byte[] datas)
- {
- FileInfo CreateFile = new FileInfo(HttpContext.Current.Server.MapPath(name + ".pdf")); //创建文件
- if (CreateFile.Exists)
- {
- CreateFile.Delete();
- }
- FileStream FS = CreateFile.Create();
- FS.Write(datas, 0, datas.Length);
- FS.Close();
- }
-
- 调用方法:
- string html = "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
- html += htmlcontent.Value;
- html += "</html>";
- getPdf("out",html);
这个方法有个缺点,对图片的支持不好
转载于:https://blog.51cto.com/1777783/1118920