Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Mar 22, 2023 8:46 am

Hello.
I found that when trying to appendHtml text that contains hyperlink anchor tag <a> into existing document, the style of hyperlink is not applied on the hyperlink anchor text inside tag, but it is applied to text following the hyperlink.
So the hyperlink text is generated with standard black color and the following text is blue color and underlined. Only style is applied incorrectly, hyperlink is clickable only inside <a> tags not on the style basic text.
This happens until first use of <p> tag inside appended htmlText. Then the is everything correct.

Bad_URL_style.png


Used spire.doc for Java version: 11.3
I have to mention that I am saving document to PDF (saving to DOCX works correctly)


Appended html text:
Code: Select all
First use of hyperlink: <a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a> Bad style applied to basic text and not applied to hyperlink until second hyperlink start: <a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a>
<p>Use of paragraph tag inside appended htmlText and the hyperlink and text style is correct.<a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a></p>
Text and hyperlink style after usage of paragraph tag inside htmlText is correct:<a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a>


Code used to append htmlString to existing document is:
Code: Select all
    Section tempSection = document.addSection();
    tempSection.addParagraph().appendHTML(htmlString);
    TextBodySelection selection = new TextBodySelection(tempSection.getBody(), 0, tempSection.getBody().getChildObjects().getCount() - 1, 0, 0);

    TextBodyPart bodyPart = new TextBodyPart(selection);

    BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
    bookmarkNavigator.moveToBookmark(bookmarkName);
    bookmarkNavigator.replaceBookmarkContent(bodyPart);

matus.vandak
 
Posts: 18
Joined: Fri Sep 02, 2022 10:00 pm

Thu Mar 23, 2023 4:00 am

Hi,

Thanks for your feedback.
We have tested the functionality of hyperlink anchor tags <a> and we did not experience any problems with their styling or functionality. I have attached the code and my output files for your reference.
Code: Select all
String htmls = "First use of hyperlink: <a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a> Bad style applied to basic text and not applied to hyperlink until second hyperlink start: <a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a>
<p>Use of paragraph tag inside appended htmlText and the hyperlink and text style is correct.<a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a></p>
Text and hyperlink style after usage of paragraph tag inside htmlText is correct:<a href="https://www.e-iceblue.com" target="_blank" rel="noopener">www.e-iceblue.com</a>";

        Document document = new Document();
        Section tempSection = document.addSection();
        Paragraph paragraph = tempSection.addParagraph();
        paragraph.appendHTML(htmls);

        document.saveToFile("htmls-2.pdf", FileFormat.PDF);

Could you please provide us with the following messages to help us do an accurate investigation? You can send them to us via email ([email protected]) or attach them here. Thanks for your assistance.
1) your full code and test documents.
2) your test environment, such as OS info (E.g., Windows 10 64bit) and region setting (E.g., China, Chinese).
3) the JDK version.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Thu Mar 23, 2023 8:37 am

Hello Triste,
thank you for your answer.

Yes, you are right. I can confirm that your code is working so in my setup. It seems that, when I added paragraph, it works fine.

But when I added paragraph and use replaceBookmarkContent with appended temp section, in that case it is wrong.

This is my code:
Code: Select all
      String htmlString = "First use of hyperlink: <a href=\"https://www.e-iceblue.com\" target=\"_blank\" rel=\"noopener\">www.e-iceblue.com</a> Bad style applied to basic text and not applied to hyperlink until second hyperlink start: <a href=\"https://www.e-iceblue.com\" target=\"_blank\" rel=\"noopener\">www.e-iceblue.com</a>\n" +
            "<p>Use of paragraph tag inside appended htmlText and the hyperlink and text style is correct.<a href=\"https://www.e-iceblue.com\" target=\"_blank\" rel=\"noopener\">www.e-iceblue.com</a></p>\n" +
            "Text and hyperlink style after usage of paragraph tag inside htmlText is correct:<a href=\"https://www.e-iceblue.com\" target=\"_blank\" rel=\"noopener\">www.e-iceblue.com</a>";

      Document document = new Document();
      document.loadFromFile("test.docx");

      Section tempSection = document.addSection();
      tempSection.addParagraph().appendHTML(htmlString);

          TextBodySelection selection = new TextBodySelection(tempSection.getBody(), 0, tempSection.getBody().getChildObjects().getCount() - 1, 0, 0);

      TextBodyPart bodyPart = new TextBodyPart(selection);

      BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
      bookmarkNavigator.moveToBookmark("test");
      bookmarkNavigator.replaceBookmarkContent(bodyPart);

      document.getSections().remove(tempSection);
      document.saveToFile("htmls-2.pdf", FileFormat.PDF);

matus.vandak
 
Posts: 18
Joined: Fri Sep 02, 2022 10:00 pm

Fri Mar 24, 2023 6:37 am

Hi,

Thanks for your feedback.
After testing, I reproduced your issue and logged it into our issue tracking system with the ticket number SPIREDOC-9205, our developers will investigate and fix it, sorry for the inconvenience caused, once the issue is fixed, I will inform you asap.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc