FieldPageRef to prior page (or page 1) not correct

Simple code, goal is to add PAGEREF to internal hyperlinks to get page numbers displayed. Issue originates from SSRS rendering to PDF so now rendering to Word which retains the bookmarks but since SSRS report definition does not have the concept of a page reference resorting to post processing.

Any help?

class AddXRefToHyperlinks
{
    public static void Run()
    {
        // The path to the documents directory.
        string dataDir = RunExamples.GetDataDir_WorkingWithHyperlink();

        Document doc = new Document(dataDir + "BookmarkTest.docx");
        DocumentBuilder builder = new DocumentBuilder(doc);

        // For each Hyperlink the document
        foreach (Field field in doc.Range.Fields)
        {
            if (field.Type == FieldType.FieldHyperlink)
            {
                FieldHyperlink hyperlink = (FieldHyperlink)field;

                // Hyperlink must be to internal bookmark
                if (hyperlink.SubAddress != null)
                {
                    builder.MoveToField(hyperlink, true);
                    FieldPageRef pageRef = (FieldPageRef)builder.InsertField(FieldType.FieldPageRef, false);
                    pageRef.BookmarkName = hyperlink.SubAddress;
                    pageRef.Update();
                }
            }
        }

        dataDir = dataDir + "BookmarkTest_out.pdf";
        doc.Save(dataDir);

        Console.WriteLine("\nHyperlinks have cross ref added\nFile saved at " + dataDir);
    }
}

Use of LayoutCollector shows that all page numbers are correctly known. I would like these to be PAGEREF objects so wondering if the page 1 / prior page issue is a known bug or a problem in my simple code pasted in above.

private static void AddPageNumbers(Document doc)
{
    // Create and attach collector before the document before page layout is built.
    LayoutCollector layoutCollector = new LayoutCollector(doc);

    // This will build layout model and collect necessary information.
    doc.UpdatePageLayout();

    // For each Hyperlink the document
    foreach (Field field in doc.Range.Fields)
    {
        if (field.Type == FieldType.FieldHyperlink)
        {
            FieldHyperlink hyperlink = (FieldHyperlink)field;

            // Hyperlink must be to internal bookmark
            if (hyperlink.SubAddress != null)
            {
                Bookmark reference = doc.Range.Bookmarks[hyperlink.SubAddress];
                if (reference != null)
                {
                    int pageNumber = layoutCollector.GetStartPageIndex(reference.BookmarkStart);
                    hyperlink.Result = hyperlink.Result + " Page: " + pageNumber;
                    hyperlink.Update();
                }
            }
        }
    }

    // Detatch the collector from the document.
    layoutCollector.Document = null;
}

Hi there,

Thanks for your inquiry. Could you please share some more detail about your query along with input and expected output documents? We will then provide you more information about your query along with code.

Input, Word document generated by SSRS ‘BookmarkTest.docx’. Ultimate need is to deliver a PDF to customer but SSRS rendering to PDF does not preserve bookmarks. Visual display of page number the internal hyperlink takes you to a requirement for printed review of PDF document. Together these two issues are the reason for doing this post-processing on a Word document to add the page numbers in the exported PDF.

Cross Reference example where page number for page #1 is not correct 'BookmarkTest_xref.pdf’

Example where page numbers for bookmarks are added to hyperlink text 'BookmarkTest_pageNum.pdf’

I would like to use the PAGEREF approach and am wondering if I am the cause of the problem where the reference to the bookmark on page 1 is not getting resolved or if it is a bug.

Hi there,

Thanks for sharing the detail. Please note that Aspose.Words mimics the same behavior as MS Word does.

As per my understanding, you want the output like “internal hyperlink” + “Page number” and you want to use PAGEREF approach. If this is the case, please use FieldHyperlink.Result to update the hyperlink’s text value.

If you still face problem, please manually create Word document using Microsoft Word and attach it here for our reference. We will investigate how you want PAGEREF field in document and provide you more information on this along with code.

This is a non-answer. The one code example that uses the layout object does just what you describe, adds the page number to the hyperlink text. This was done to show that the page numbers are accurately known. The first example that inserts the FieldPageRef works on page one to reference a bookmark on page 2 BUT the FieldPageRef on page 2 back to a bookmark on page 1 does not work. The question is why doesn’t this first example work?

Hi there,

Thanks for your inquiry. In the first example, you are inserting the PageRef field after Hyperlink field. The PageRef field is not part of hyperlink.

We noticed that PageRef field is converted to hyperlink in output Pdf. This is incorrect behavior. We have logged this issue as WORDSNET-14853 in our issue tracking system.

Field.Update does not update the FieldPageRef field on second output page. This PageRef field is on second page. It is after the text "Back to Start of Report". We have logged this issue as WORDSNET-14854 in our issue tracking system.

You will be notified via this forum thread once these issues are resolved. We apologize for your inconvenience.

You want the page number in hyperlink’s text. We suggest you please add the page number in hyperlink’s text instead of adding PageRef field.

This is not what I see in my tests. I insert the PageRef field after the hyperlink to the bookmark. All that eventually goes into the PDF is the ‘result’ in the PageRef with is the page number. In one case this page number is not set, it is blank and in all other cases the page number is correct. This means that only the second half of WORDSNET-14853 accurately describes what I’m seeing.

Hi there,

Thanks for sharing the detail. You are using old version of Aspose.Words. Please upgrade to the latest version of Aspose.Words for .NET 17.2.0.

Please manually insert PAGEREF field using Microsoft Word after the hyperlinks in your input document and check the MS Word’s behavior.

Please create your expected output document using Microsoft Word. First create the document using MS Word and then save it to Pdf. Please share the Word and Pdf documents here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

I’ve updated to 17.2

Added two CrossReferences to the source Word document to verify behavior, attached as ‘BookmarkTest_xref.docx’ This works as expected in Word or when exported as PDF, attached as ‘BookmarkTest_xref2.pdf’.

No change to source code, attached as ‘AddXRefToHyperlinks.txt’ (extension changed to permit add).

Result is unchanged, attached resulting PDF as ‘BookmarkTest_xref.pdf’ where the cross reference to the bookmark on page 1 does not get a page number.

Hi there,

Thanks for sharing the detail. We logged this issue as WORDSNET-14854 in our issue tracking system. You will be notified via this forum thread once these issues are resolved.

We apologize for your inconvenience.

Hi there,

Further to my previous post, your code performs following steps:

  1. adds PAGEREF field 1
  2. updates PAGEREF field 1
  3. adds PAGEREF field 2
  4. updates PAGEREF field 2

During step #2 Aspose.Words builds layout model. So, on step #4 layout model already exists, but does not contain span field for the second PAGEREF field. That is why it does not have result.

Please use one of the following workaround for this issue.

  1. Call Document.UpdatePageLayout before Field.Update.
  2. Call field.Update or Document.UpdateFields after all PAGEREF field are inserted
private static void AddCrossReferences(string inputDoc, string outputDoc)
{
    Document document = new Document(inputDoc);
    DocumentBuilder builder = new DocumentBuilder(document);
    var fields = document.Range.Fields
    .OfType<FieldHyperlink>()
    .Where(p => p.SubAddress != null)
    .Select(p =>
    {
        builder.MoveToField(p, true);
        var pageRef = (FieldPageRef)builder.InsertField(FieldType.FieldPageRef, false);
        pageRef.BookmarkName = p.SubAddress;
        return pageRef;
    })
    .ToList();
    foreach (var field in fields)
        field.Update();
    document.Save(outputDoc, Aspose.Words.SaveFormat.Pdf);
    Console.WriteLine("\nHyperlinks have cross ref added\nFile saved as PDF at " + outputDoc);
}

Thanks.

I had tried calling UpdatePageLayout once before adding and updating fields but all page references are blank if you do that. Calling UpdatePageLayout repeatedly before Update did not occur to me.

Hi there,

Thanks for your inquiry. The shared workarounds work. Please upgrade to the latest version of Aspose.Words for .NET 17.2.0. We have attached the output Pdf with this post for your kind reference.

Please only call Document.UpdatePageLayout before Field.Update as shown below. You may use the code shared in my previous post.

private static void AddCrossReferences(string inputDoc, string outputDoc)
{
    Document doc = new Document(inputDoc);
    DocumentBuilder builder = new DocumentBuilder(doc);
    // For each Hyperlink the document
    foreach (Field field in doc.Range.Fields)
    {
        if (field.Type == FieldType.FieldHyperlink)
        {
            FieldHyperlink hyperlink = (FieldHyperlink)field;
            // Hyperlink must be to internal bookmark
            if (hyperlink.SubAddress != null)
            {
                builder.MoveToField(hyperlink, true);
                FieldPageRef pageRef = (FieldPageRef)builder.InsertField(FieldType.FieldPageRef, false);
                pageRef.BookmarkName = hyperlink.SubAddress;
                doc.UpdatePageLayout();
                pageRef.Update();
            }
        }
    }
    doc.Save(outputDoc, Aspose.Words.SaveFormat.Pdf);
    Console.WriteLine("\nHyperlinks have cross ref added\nFile saved as PDF at " + outputDoc);
}

Yes, I verified both of your suggestions from your last post. Thank you for your patience in this.

Hi there,

Thanks for your patience. It is to update you that we have closed the issue (WORDSNET-14854) with “Won’t Fix” resolution.

Please use the suggested code examples in my previous posts.

The issues you have found earlier (filed as WORDSNET-14853) have been fixed in this Aspose.Words for .NET 17.4 update and this Aspose.Words for Java 17.4 update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.