【问题标题】:How to get Page Count per section in a PDF如何获取 PDF 中每个部分的页数
【发布时间】:2013-10-22 06:39:40
【问题描述】:

我正在使用 MigraDoc 呈现 PDF 文档。 每个部分都有一个或多个段落文本。

目前这是我创建文档的方式;

var document = new Document();
var pdfRenderer = new PdfDocumentRenderer(true);
pdfRenderer.Document = document; 

for(int i=0;i<10;i++){
    Section section = document.AddSection();
    section.PageSetup.PageFormat = PageFormat.A4;

    for(int j=0;j<5;j++) {
    var paragraphText = GetParaText(i,j); // some large text can span multiple pages
    section.AddParagraph(paragraphText);
    //Want page count per section? 
     // Section 1 -> 5 , Section 2 ->3 etc.
    // int count = CalculateCurrentPageCount(); //*EDIT*
   }
}
// Create the PDF document
pdfRenderer.RenderDocument();
pdfRenderer.Save(filename);

编辑:目前我使用以下代码来获取页数。
但这需要很多时间,可能每个页面都渲染两次。

 public int CalculateCurrentPageCount()
        {
            var tempDocument = document.Clone();
            tempDocument.BindToRenderer(null);     
            var pdfRenderer = new PdfDocumentRenderer(true);
            pdfRenderer.Document = tempDocument;
            pdfRenderer.RenderDocument();
            int count = pdfRenderer.PdfDocument.PageCount;
            Console.WriteLine("-- Count :" + count);
            return count;
        }

根据添加的内容,某些部分可以跨越多个页面。

是否可以获取/查找渲染一个部分需要多少页(PDF 格式)?

编辑2:是否可以标记一个部分并找到它从哪个页面开始?

【问题讨论】:

    标签: c# pdfsharp migradoc


    【解决方案1】:

    感谢您的帮助。我是这样计算的(即获取代码中的计数......):

    首先我用该部分的创建计数标记了该部分

    newsection.Tag = num_sections_in_doc; //count changes every time i add a section
    

    然后我使用 GetDocumentObjectsFromPage :

    var x = new Dictionary<int, int>();
                    int numpages = pdfRenderer.PdfDocument.PageCount;
                    for (int idx = 0; idx < numpages; idx++)
                    {
                        DocumentObject[] docObjects = pdfRenderer.DocumentRenderer.GetDocumentObjectsFromPage(idx + 1);
                        if (docObjects != null && docObjects.Length > 0)
                        {
                            Section section = docObjects[0].Section;
                            int sectionTag = -1;
                            if (section != null)
                                sectionTag = (int)section.Tag;
                            if (sectionTag >= 0)
                            {
                                // count a section only once
                                if (!x.ContainsKey(sectionTag))
                                    x.Add(sectionTag, idx + 1);
                            }
                        }
                    }
    

    x.Keys 是部分。
    和 x.values 是每个部分的开始。

    【讨论】:

    • 您可以使用节对象本身来比较相等性,而不是使用标记。然后使用 null 而不是 -1
    【解决方案2】:

    如果要在 PDF 中显示页数,请使用 paragraph.AddSectionPagesField()

    另请参阅:
    https://stackoverflow.com/a/19499231/162529

    要在代码中获取计数:您可以将标签添加到任何文档对象(例如,添加到任何段落),然后使用 docRenderer.GetDocumentObjectsFromPage(...) 查询特定页面的对象。这使您可以找出此页面上的对象属于哪个部分。

    或者在单独的文档中创建每个部分,然后使用docRenderer.RenderPage(...) 将它们合并为一个 PDF,如下所示:
    http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx
    该示例将页面缩小为缩略图大小 - 您可以 1:1 绘制它们,每个都在一个新页面上。

    【讨论】:

    • 内容是动态添加的。所以我不能使用前两个选项。至于第三个选项我必须使用 PDFSharp 的对象模型,我已经实现了 migradoc 对象模型的代码。我还添加了一个编辑,有没有更好/更快的方法?
    • 您可以动态添加内容。立即添加标签或在呈现文档之前添加标签(向每个部分添加单个标签就足够了)。做对了,文档只准备一次,可以查询页数,可以逐页创建单个PDF,同时统计每个节的页数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多