Hi,
We are generating a word document on our server. As of now, we are generating the entire document in one step.
This document consists of multiple group of pages and a cover page. (cover, page 1-1, page 1-2, page 2-1, page 2-2, page 2-3, page 3-1, etc.)
We are converting this document as a PDF.
Now, we have to insert specific PDF after each group of page, so I will certainly import the pdf then rearrange the page. But for this, I need to know at which page each group starts.
From my previous example we want to have : cover, page 1-1, page 1-2,
I am struggling to find a way to locate each group first page in the pdf.
I have tried using PDF bookmarks (either using word bookmarks or heading style) without success : there are no information in the PdfBookmark about its location (which page could be enough)
Do you have any suggestion about how to do so?
Nevermind,
I was able to obtain the Page Index using
I based my first though on the Destination.PageIndex value, which was always 0. But the page is really the correct one, and I can base my code on this page index to rearrange the document.
I found strange that PdfBookmark has a Destination property of type PdfDestination, where the PageIndex is always 0, despite the Page property refers to a page which is not at Index 0.
That is the reason why I was convinced that the Page was referring to the first page of the document (instead of the good one).
After discovering I was wrong, I had to use the PdfDocument.Pages.IndexOf(destination.Page) to find the correct page index.
I don't know what is the meaning of the PageIndex property, since it does not have any documentation and it does not contains the actual index of the Page property.
Perhaps there is a bug there, perhaps the documentation should be more clear about the intend of this property.
Regards.
Here is a simple code which reproduces the problem:
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using System;
namespace WindowsFormsApp1
{
static class Program
{
static void Main()
{
using (var wordDoc = new WordDocument())
{
IWSection section;
IWParagraph paragramh;
section = wordDoc.AddSection();
section.BreakCode = SectionBreakCode.NewPage;
paragramh = section.AddParagraph();
paragramh.AppendText("First Page");
section = wordDoc.AddSection();
paragramh = section.AddParagraph();
paragramh.AppendBookmarkStart("Second Page");
paragramh.AppendText("Second Page");
paragramh.AppendBookmarkEnd("Second Page");
using (var converter = new DocToPDFConverter())
{
var pdfDoc = converter.ConvertToPDF(wordDoc);
foreach (PdfBookmark bookmark in pdfDoc.Bookmarks)
{
Console.WriteLine(bookmark.Destination.PageIndex); // output 0
Console.WriteLine(pdfDoc.Pages.IndexOf((PdfPage)bookmark.Destination.Page)); // output 1
}
}
Console.ReadKey();
}
}
}
}
|
Console.WriteLine(pdfDoc.Pages.IndexOf((PdfPage)bookmark.Destination.Page)); |
|
PdfLoadedDocument ldoc = new PdfLoadedDocument("Output.pdf");
PdfLoadedBookmark lbook = (ldoc.Bookmarks[0] as PdfLoadedBookmark);
var index = lbook.Destination.PageIndex; |
Thank for the explanation.
Perhaps you could document this behavior on the official documentation, by explaining that the PageIndex property is only set when the destination is owned by a PdfLoadedDocument?
Regards,