使用 Spire.Pdf 生成书签但是有 Evaluation Warning : The document was created with Spire.PDF for .NET.

使用 Spire.Pdf 生成书签但是有 Evaluation Warning : The document was created with Spire.PDF for .NET.字样

这里用到了两个插件  Spire.Pdf 与 iTextSharp 请在 NuGet 下载安装

由于使用  Spire.Pdf 生成的书签带有 Evaluation Warning : The document was created with Spire.PDF for .NET. 字样

但是它只在第一页头部有显示,所以我就有了个点子

就是生成第一页空白让它独占,接着我把第一页删除就好了,对没错,想法是很好的,

可是继续使用Spire.Pdf删除第一页的时候那该死的字样还是会在第一页,所以就想到了使用别的插件

申明文件

public void BuildMaterialMark()
        {
            List<string> fileNames = new List<string>();
            fileNames.Add("D:\\test\\one\\1.pdf");
            fileNames.Add("D:\\test\\two\\2.pdf");
            fileNames.Add("D:\\test\\three\\3.pdf");
            fileNames.Add("D:\\test\\four\\4.pdf");
            fileNames.Add("D:\\test\\four\\5.pdf");
            fileNames.Add("D:\\test\\four\\6.pdf");
            fileNames.Add("D:\\test\\four\\7.pdf");
            fileNames.Add("D:\\test\\four\\8.pdf");
            fileNames.Add("D:\\test\\five\\9.jpg");
            fileNames.Add("D:\\test\\five\\10.jpg");
            fileNames.Add("D:\\test\\five\\11.jpg");
            fileNames.Add("D:\\test\\five\\12.jpg");
            fileNames.Add("D:\\test\\five\\13.pdf");
            fileNames.Add("D:\\test\\five\\14.jpg");
            fileNames.Add("D:\\test\\six\\15.pdf");
            fileNames.Add("D:\\test\\seven\\16.pdf");
            fileNames.Add("D:\\test\\seven\\17.pdf");
            fileNames.Add("D:\\test\\seven\\18.pdf");
            fileNames.Add("D:\\test\\seven\\19.jpg");
            fileNames.Add("D:\\test\\seven\\20.pdf");
            fileNames.Add("D:\\test\\eight\\21.pdf");
            fileNames.Add("D:\\test\\eight\\22.pdf");
            fileNames.Add("D:\\test\\eight\\23.pdf");
            fileNames.Add("D:\\test\\eight\\24.pdf");
            fileNames.Add("D:\\test\\eight\\25.pdf");
            fileNames.Add("D:\\test\\eight\\26.pdf");

            List<Hashtable> datas = new List<Hashtable>();
            for (int i = 0; i < fileNames.Count; i++)
            {
                string title = "";
                if (fileNames[i].IndexOf("\\one\\")>-1)
                    title = "1";
                if (fileNames[i].IndexOf("\\two\\") > -1)
                    title = "2";
                if (fileNames[i].IndexOf("\\three\\") > -1)
                    title = "3";
                if (fileNames[i].IndexOf("\\four\\") > -1)
                    title = "4";
                if (fileNames[i].IndexOf("\\five\\") > -1)
                    title = "5";
                if (fileNames[i].IndexOf("\\six\\") > -1)
                    title = "6";
                if (fileNames[i].IndexOf("\\seven\\") > -1)
                    title = "7";
                if (fileNames[i].IndexOf("\\eight\\") > -1)
                    title = "8";
                Hashtable ht = new Hashtable();
                ht.Add("title", title);
                ht.Add("paths", fileNames[i]);
                datas.Add(ht);
            }
            MergePdfFilesWithBookMark(datas, "D:\\test\\mark\\mark5.pdf");
        }

生成书签

        /// <summary>
        /// 
        /// </summary>
        /// <param name="datas">文件</param>
        /// <param name="outputPdf">新生成的pdf地址  这个文件带有 Evaluation Warning : The document was created with Spire.PDF for .NET.</param>
        private void MergePdfFilesWithBookMark(List<Hashtable> datas, string outputPdf)
        {
            try
            {
                Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();
                pdf.Pages.Add();

                int index = 0;
                int totalPages = 0;
                for (int i = 0; i < datas.Count; i++)
                {
                    Hashtable ht = datas[i];

                    Spire.Pdf.PdfDocument pdfs = new Spire.Pdf.PdfDocument();

                    index = pdf.Pages.Count;
                    int b = ht["paths"].ToString().LastIndexOf("\\") + 1;
                    int e = ht["paths"].ToString().LastIndexOf(".");
                    string marks = ht["paths"].ToString().Substring(b, e - b);
                    string types = ht["paths"].ToString().Substring(e+1);
                    
                    if (types!="jpg")
                    {
                        pdfs.LoadFromFile(ht["paths"].ToString());
                        pdfs.Bookmarks.Clear();
                        pdf.AppendPage(pdfs);
                    }
                    else
                    {
                        PdfPageBase page = pdf.Pages.Add();
                        Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromFile(ht["paths"].ToString());
                        //Image im = Image.FromFile(ht["paths"].ToString());
                        Spire.Pdf.Graphics.PdfTemplate template = page.CreateTemplate();
                        template.Graphics.DrawImage(image, 0, 0);
                        template.Draw(page, new PointF(0, 0));
                    }
                    totalPages = pdf.Pages.Count;
                    PdfBookmarkCollection markcoll = pdf.Bookmarks;
                    int markCount = markcoll.Count;
                    bool ishavetitle = false;
                    for (int k = 0; k < markCount; k++)
                    {
                        PdfBookmark pdfmark = markcoll[k];
                        if (pdfmark.Title == ht["title"].ToString())
                        {
                            PdfBookmark childBookmark = pdfmark.Insert(pdfmark.Count, marks);
                            childBookmark.Destination = new Spire.Pdf.General.PdfDestination(pdf.Pages[index]);
                            childBookmark.Destination.Location = new PointF(0, 0);
                            ishavetitle = true;
                            break;
                        }
                    }
                    if (!ishavetitle)
                    {
                        PdfBookmark bookmark = pdf.Bookmarks.Add(ht["title"].ToString());
                        bookmark.Destination = new Spire.Pdf.General.PdfDestination(pdf.Pages[index]);
                        bookmark.Destination.Location = new PointF(0, 0);

                        PdfBookmark childBookmark = bookmark.Insert(bookmark.Count, marks);
                        childBookmark.Destination = new Spire.Pdf.General.PdfDestination(pdf.Pages[index]);
                        childBookmark.Destination.Location = new PointF(0, 0);
                    }
                }
                pdf.SaveToFile(outputPdf);

                MergePdfFilesWithBookMark(new string[1] { outputPdf }, "D:\\test\\mark\\mark5NEW.pdf");
            }
            catch (Exception err)
            {
                throw;
            }
            
        }

去掉 Evaluation Warning : The document was created with Spire.PDF for .NET.字样

这个方法借鉴了 https://blog.csdn.net/zuoyefeng1990/article/details/72515089 文章

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourcePdfs">原文件地址   </param>
        /// <param name="outputPdf">生成后的文件地址  去除  Evaluation Warning : The document was created with Spire.PDF for .NET.</param>
        private void MergePdfFilesWithBookMark(string[] sourcePdfs, string outputPdf)
        {
            PdfReader reader = null;
            Document document = new Document();
            PdfImportedPage page = null;
            PdfCopy pdfCpy = null;
            int n = 0;
            int totalPages = 0;
            int page_offset = 0;
            List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
            IList<Dictionary<string, object>> tempBookmarks;
            for (int i = 0; i <= sourcePdfs.GetUpperBound(0); i++)
            {
                reader = new PdfReader(sourcePdfs[i]);
                reader.ConsolidateNamedDestinations();
                n = reader.NumberOfPages;
                tempBookmarks = SimpleBookmark.GetBookmark(reader);

                if (i == 0)
                {
                    document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
                    pdfCpy = new PdfCopy(document, new FileStream(outputPdf, FileMode.Create));
                    document.Open();
                    SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset-1, null);
                    page_offset += n;
                    if (tempBookmarks != null)
                        bookmarks.AddRange(tempBookmarks);
                    totalPages = n;
                }
                else
                {
                    SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset - 1, null);
                    if (tempBookmarks != null)
                        bookmarks.AddRange(tempBookmarks);
                    page_offset += n;
                    totalPages += n;
                }

                for (int j = 2; j <= n; j++)
                {
                    page = pdfCpy.GetImportedPage(reader, j);
                    pdfCpy.AddPage(page);

                }
                reader.Close();

            }
            pdfCpy.Outlines = bookmarks;
            document.Close();
        }

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值