Exemplo n.º 1
0
        //public static  bool PageToPdfByteArray(string url, string path, Encoding encoe)
        //{
        //    byte[] pdfBuf;
        //    bool ret = false;
        //    try
        //    {
        //        GlobalConfig gc = new GlobalConfig();

        //        // set it up using fluent notation
        //        gc.SetMargins(new Margins(0, 0, 0, 0))
        //          .SetDocumentTitle("Test document")
        //          .SetPaperSize(PaperKind.A4);
        //        //... etc

        //        // create converter
        //        IPechkin pechkin = new SynchronizedPechkin(gc);

        //        // subscribe to events
        //        //pechkin.Begin += OnBegin;
        //        //pechkin.Error += OnError;
        //        //pechkin.Warning += OnWarning;
        //        //pechkin.PhaseChanged += OnPhase;
        //        //pechkin.ProgressChanged += OnProgress;
        //        //pechkin.Finished += OnFinished;

        //        // create document configuration object
        //        ObjectConfig oc = new ObjectConfig();

        //        // and set it up using fluent notation too
        //        oc.SetCreateExternalLinks(false)
        //        .SetFallbackEncoding(encoe)
        //        .SetLoadImages(true)
        //        .SetPageUri(url);
        //        //... etc

        //        // convert document
        //        pdfBuf = pechkin.Convert(oc);

        //        FileStream fs = new FileStream(path, FileMode.Create);
        //        fs.Write(pdfBuf, 0, pdfBuf.Length);
        //        fs.Close();
        //        ret = true;
        //    }
        //    catch (Exception ex)
        //    {

        //    }

        //    return ret;
        //}

        /// <summary>
        /// 根据页面url,按节点目录生成pdf和对应书签(空页面不生成)
        /// </summary>
        /// <param name="docList"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        public static bool MergePdf(List <PdfDoc> docList, string savePath)
        {
            if (docList.Count == 0)
            {
                return(false);
            }
            Doc doc = new Doc();

            doc.HtmlOptions.Timeout   = 30 * 1000;
            doc.HtmlOptions.UseScript = true;
            doc.Rect.Inset(36.0, 72.0);//Rect默认是文档整个页面大小, 这里的Inset表示将Rect左右留出36的空白,上下留出72的空白

            string emptyMarkPath = null;
            bool   emptyMarkExp  = false;

            try
            {
                foreach (PdfDoc pd in docList)
                {
                    if (pd.NodePid != 0) //0为根节点,没有页面
                    {
                        if (string.IsNullOrEmpty(pd.Url))
                        {
                            /**
                             * 有些目录可能没有页面内容,这里则先将目录的bookmark路径保存;
                             * **/
                            emptyMarkPath = GetPath(docList, pd).TrimEnd('\\');
                            emptyMarkExp  = pd.Expended;
                        }
                        else
                        {
                            doc.Page = doc.AddPage();
                            if (emptyMarkPath != null)
                            {
                                doc.AddBookmark(emptyMarkPath, emptyMarkExp); //让空目录指定到其第一个子页面
                                emptyMarkPath = null;                         //添加之后置空
                            }
                            doc.AddBookmark(GetPath(docList, pd).TrimEnd('\\'), pd.Expended);

                            int num = doc.AddImageUrl(pd.Url);
                            while (true)
                            {
                                //doc.FrameRect();//给内容区域添加黑色边框
                                if (!doc.Chainable(num))
                                {
                                    break;
                                }
                                doc.Page = doc.AddPage();
                                num      = doc.AddImageToChain(num);
                            }
                        }
                    }
                }
                for (int i = 1; i <= doc.PageCount; i++)
                {
                    doc.PageNumber = i;
                    doc.Flatten();
                }
                if (!savePath.ToLower().EndsWith(".pdf"))
                {
                    savePath += ".pdf";
                }
                doc.Save(savePath);
            }
            catch (Exception ex)
            {
                LogError.ReportErrors(ex.Message);
                return(false);
            }
            finally
            {
                doc.Clear();
                doc.Dispose();
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 按一级目录请求页面,二级和以后的目录全部不生成,书签只能定位到一级目录
        /// </summary>
        /// <param name="docList"></param>
        /// <param name="savePath"></param>
        /// <param name="customerName"></param>
        /// <param name="bookTaskUrl">为a标签包含的链接</param>
        /// <returns></returns>
        public static bool MergePdf4(List <PdfDoc> docList, string savePath, string customerName, string bookTaskUrl)
        {
            if (docList.Count == 0)
            {
                return(false);
            }
            Doc doc = new Doc();

            doc.HtmlOptions.Timeout          = 30 * 1000;
            doc.HtmlOptions.UseScript        = true;
            doc.HtmlOptions.UseNoCache       = true;
            doc.HtmlOptions.PageCacheEnabled = false;
            doc.HtmlOptions.PageCacheClear();
            doc.Rect.Inset(52.0, 100.0);

            try
            {
                Dictionary <int, string> titleDic = new Dictionary <int, string>();
                foreach (PdfDoc pd in docList)
                {
                    if (pd.NodePid == 0)//0为根节点,可能为封面,单独处理
                    {
                        continue;
                    }

                    doc.Page = doc.AddPage();
                    doc.AddBookmark(GetPath(docList, pd).TrimEnd('\\'), pd.Expended);
                    titleDic.Add(doc.PageCount, pd.Name);

                    if (pd.Url == null)
                    {
                        continue;
                    }
                    int num = doc.AddImageUrl(pd.Url);

                    while (true)
                    {
                        if (!doc.Chainable(num))
                        {
                            break;
                        }
                        doc.Page = doc.AddPage();
                        num      = doc.AddImageToChain(num);
                        titleDic.Add(doc.PageCount, pd.Name);
                    }
                }
                #region 添加页眉和页脚

                AddHeader(ref doc, titleDic, customerName, bookTaskUrl);
                for (int i = 1; i <= doc.PageCount; i++)
                {
                    doc.PageNumber = i;
                    //压缩输出
                    doc.Flatten();
                }
                #endregion
                if (!savePath.ToLower().EndsWith(".pdf"))
                {
                    savePath += ".pdf";
                }
                doc.Save(savePath);
            }
            catch (Exception ex)
            {
                LogError.ReportErrors(ex.Message);
                return(false);
            }
            finally
            {
                doc.Clear();
                doc.Dispose();
            }

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 将html页面生成pdf文档,同步方式
        /// </summary>
        /// <param name="url"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        public static bool Html2PDFSynch(string url, string savePath, string customName)
        {
            Doc doc = new Doc();

            try
            {
                //url = "http://localhost:8018/PDF?returnUrl=http://localhost:8018/Material/MatList/exporttopdf?matListId=167";
                doc.HtmlOptions.UseNoCache       = true;
                doc.HtmlOptions.PageCacheEnabled = false;
                doc.HtmlOptions.PageCacheClear();
                doc.HtmlOptions.Timeout    = 30 * 1000;
                doc.HtmlOptions.UseScript  = true;
                doc.HtmlOptions.UseActiveX = true;
                doc.Rect.Inset(36.0, 72.0);
                doc.Page = doc.AddPage();
                int num = doc.AddImageUrl(url);
                while (true)
                {
                    //doc.FrameRect();//添加黑色边框
                    if (!doc.Chainable(num))
                    {
                        break;
                    }
                    doc.Page = doc.AddPage();
                    num      = doc.AddImageToChain(num);
                }
                for (int i = 1; i <= doc.PageCount; i++)
                {
                    doc.PageNumber = i;

                    doc.Color.String = "0 0 0";     //黑色
                    doc.AddLine(24, 750, 588, 750); //画一条分隔线

                    doc.Rect.String      = "24 12 588 40";
                    doc.HPos             = 0;
                    doc.VPos             = 0.5;
                    doc.Font             = doc.AddFont("宋体", "ChineseS");
                    doc.TextStyle.Italic = true;
                    doc.AddHtml(" <font color=\"#cccccc\">" + customName + "</font>");
                    doc.TextStyle.Italic = false;

                    doc.Rect.String  = "24 12 588 40";
                    doc.HPos         = 1.0;
                    doc.VPos         = 0.5;
                    doc.Color.String = "0 0 0"; //黑色
                    doc.AddHtml("page " + i.ToString() + "/" + doc.PageCount.ToString());
                    doc.AddLine(24, 40, 588, 40);

                    doc.Flatten();
                }
                if (!savePath.ToLower().EndsWith(".pdf"))
                {
                    savePath += ".pdf";
                }
                doc.Save(savePath);
                doc.Clear();
                return(true);
            }
            catch (Exception ex)
            {
                LogError.ReportErrors(ex.Message);

                return(false);
            }
            finally
            {
                doc.Clear();
            }

            return(true);
        }