Exemplo n.º 1
0
        /// <summary>
        /// 整个任务书展示页面生成pdf,同时计算页码,生成书签(此方法不完善,书签定位不正确)
        /// </summary>
        /// <param name="docList"></param>
        /// <param name="url"></param>
        /// <param name="savePath"></param>
        /// <param name="customName"></param>
        /// <param name="tops"></param>
        /// <returns></returns>
        public static bool MergePdf3(List <PdfDoc> docList, string url, string savePath, string customName, Dictionary <int, int> tops)
        {
            Doc doc = new Doc();

            doc.HtmlOptions.Timeout          = 30 * 1000;
            doc.HtmlOptions.UseScript        = true;
            doc.HtmlOptions.UseNoCache       = true;
            doc.HtmlOptions.PageCacheEnabled = false;
            doc.HtmlOptions.PageCacheClear();

            int num = doc.AddImageUrl(url);

            while (true)
            {
                if (!doc.Chainable(num))
                {
                    break;
                }
                doc.Page = doc.AddPage();
                num      = doc.AddImageToChain(num);
            }
            for (int i = 0; i < tops.Count; i++)
            {
                int    minusOffset = tops.Values.ToList <int>()[0] * i;
                double offset      = 0.0;
                doc.PageNumber = CalculatePageNum(doc, tops.Values.ToList <int>()[i], out offset, minusOffset);
                int id = doc.AddBookmark(GetPath(docList, tops.Keys.ToList <int>()[i]).TrimEnd('\\'), true);
                doc.SetInfo(id, "/Dest:Del", "");
                doc.SetInfo(id, "/Dest[]:Ref", doc.Page.ToString());
                doc.SetInfo(id, "/Dest[]:Name", "XYZ");
                doc.SetInfo(id, "/Dest[]:Num", "0");
                doc.SetInfo(id, "/Dest[]:Num", offset.ToString());
                doc.SetInfo(id, "/Dest[]", "null");
            }

            for (int i = 1; i <= doc.PageCount; i++)
            {
                doc.PageNumber = i;
                doc.Flatten();
            }
            doc.Save(savePath);
            doc.Clear();

            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
        //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);
        }