使用iTextSharp添加新页面

时间:2013-03-06 13:59:18

标签: c# xml pdf itextsharp

我有一个类为我的目录构建内容,并且工作正常,花花公子。 这是代码:

public void Build(string center,IDictionary<string,iTextSharp.text.Image> images)
    {
        iTextSharp.text.Image image = null;
        XPathDocument rapportTekst = new XPathDocument(Application.StartupPath + @"..\..\..\RapportTemplates\RapportTekst.xml");
        XPathNavigator nav = rapportTekst.CreateNavigator();
        XPathNodeIterator iter;

        iter = nav.Select("//dokument/brevhoved");
        iter.MoveNext();
        var logo = iTextSharp.text.Image.GetInstance(iter.Current.GetAttribute("url", ""));
        iter = nav.Select("//dokument/gem_som");
        iter.MoveNext();
        string outputPath = iter.Current.GetAttribute("url", "")+center+".pdf";
        iter = nav.Select("//dokument/titel");
        iter.MoveNext();
        this.titel = center;

        Document document = new Document(PageSize.A4, 30, 30, 100, 30);
        var outputStream = new FileStream(outputPath, FileMode.Create);
        var pdfWriter = PdfWriter.GetInstance(document, outputStream);
        pdfWriter.SetLinearPageMode();

        var pageEventHandler = new PageEventHandler();
        pageEventHandler.ImageHeader = logo;
        pdfWriter.PageEvent = pageEventHandler;

        DateTime timeOfReport = DateTime.Now.AddMonths(-1);

        pageWidth = document.PageSize.Width - (document.LeftMargin + document.RightMargin);
        pageHight = document.PageSize.Height - (document.TopMargin + document.BottomMargin);

        document.Open();
        var title = new Paragraph(titel, titleFont);
        title.Alignment = Element.ALIGN_CENTER;
        document.Add(title);

        List<TableOfContentsEntry> _contentsTable = new List<TableOfContentsEntry>();
        nav.MoveToRoot();
        iter = nav.Select("//dokument/indhold/*");
        Chapter chapter = null;
        int chapterCount = 1;
        while (iter.MoveNext())
        {
            _contentsTable.Add(new TableOfContentsEntry("Test", pdfWriter.CurrentPageNumber.ToString()));
            XPathNodeIterator innerIter = iter.Current.SelectChildren(XPathNodeType.All);
            chapter = new Chapter("test", chapterCount);
            while(innerIter.MoveNext())
            {                  
                if (innerIter.Current.Name.ToString().ToLower().Equals("billede"))
                {
                    image = images[innerIter.Current.GetAttribute("navn", "")];
                    image.Alignment = Image.ALIGN_CENTER;
                    image.ScaleToFit(pageWidth, pageHight);
                    chapter.Add(image);
                }
                if (innerIter.Current.Name.ToString().ToLower().Equals("sektion"))
                {
                    string line = "";
                    var afsnit = new Paragraph();
                    line += (innerIter.Current.GetAttribute("id", "") + " ");
                    innerIter.Current.MoveToFirstChild();
                    line += innerIter.Current.Value;
                    afsnit.Add(line);
                    innerIter.Current.MoveToNext();
                    afsnit.Add(innerIter.Current.Value);
                    chapter.Add(afsnit);
                }
            }
            chapterCount++;
            document.Add(chapter);
        }
        document = CreateTableOfContents(document, pdfWriter, _contentsTable);
        document.Close();           
    }

然后我调用方法CreateTableOfContents(),因此它正在做它应该做的事情。以下是该方法的代码:

public Document CreateTableOfContents(Document _doc, PdfWriter _pdfWriter, List<TableOfContentsEntry> _contentsTable)
    {
        _doc.NewPage();
        _doc.Add(new Paragraph("Table of Contents", FontFactory.GetFont("Arial", 18, Font.BOLD)));
        _doc.Add(new Chunk(Environment.NewLine));

        PdfPTable _pdfContentsTable = new PdfPTable(2);

        foreach (TableOfContentsEntry content in _contentsTable)
        {
            PdfPCell nameCell = new PdfPCell(_pdfContentsTable);
            nameCell.Border = Rectangle.NO_BORDER;
            nameCell.Padding = 6f;
            nameCell.Phrase = new Phrase(content.Title);
            _pdfContentsTable.AddCell(nameCell);

            PdfPCell pageCell = new PdfPCell(_pdfContentsTable);
            pageCell.Border = Rectangle.NO_BORDER;
            pageCell.Padding = 6f;
            pageCell.Phrase = new Phrase(content.Page);
            _pdfContentsTable.AddCell(pageCell);
        }
        _doc.Add(_pdfContentsTable);
        _doc.Add(new Chunk(Environment.NewLine));

        /** Reorder pages so that TOC will will be the second page in the doc
        * right after the title page**/
        int toc = _pdfWriter.PageNumber - 1;
        int total = _pdfWriter.ReorderPages(null);
        int[] order = new int[total];

        for (int i = 0; i < total; i++)
        {
            if (i == 0)
            {
                order[i] = 1;
            }
            else if (i == 1)
            {
                order[i] = toc;
            }
            else
            {
                order[i] = i;
            }
        }

        _pdfWriter.ReorderPages(order);
        return _doc;
    }

但问题是。我想在目录之前插入分页符,以便重新排序页面,以便目录自然是第一页。但pdf文件的输出不正确。

这是一张它的样子: Picture

似乎_doc.NewPage()方法中的CreateTableOfContents()无法正确执行。这意味着当方法开始重新排序页面时,图像和目录仍然在同一页面上。

编辑:为了澄清上述内容,_doc.NewPage()被执行,但空白页面被添加到图片和目录之后。

我读过几个地方,这可能是因为有人试图在已经空白的页面后插入新页面。但事实并非如此。

我只是链接到pdf文件,以更好地说明问题。

包含目录的pdf:with table of contents

没有目录的pdf:without table of contents

提前感谢您的帮助:)

0 个答案:

没有答案