Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jun 23, 2022 9:27 am

Hello,

I've been looking at Spire.PDF (.NET) for a project where I need to produce product datasheet downloads in PDF format, from product data stored in a database.

On the whole, I've managed to get a nice result so far - but have come across a problem with interpreting text with HTML tags.

We need to organise the product description in to three columns, which I've achieved using PdfStringLayouter - I break up the product description in to paragraphs, which I feed in to PdfStringLayouter.Layout(). Each line of the result is then output using the DrawString function, and I maintain/check the vertical position on the page after each line, to decide when a column break is required.

With plain text, this works absolutely fine.

However, our product description contains some basic HTML tags (e.g. <strong>, <b>, <u>) which are used to highlight certain key words or phrases.

It seems that PdfStringLayouter is not able to interpret HTML tags, even though DrawString is able to (htmlTags boolean parameter).

We found that DrawString correctly interprets basic HTML tags (and makes the necessary adjustments in the text - e.g. setting chunks of content to bold type). However, because PdfStringLayouter doesn't recognise HTML tags (and includes them verbatim when creating the layout) it breaks the layout.

Just wondered if there was a work around for this? Is there some way of using PdfStringLayouter to get the line breaks in the right place by passing in a plain text version of a paragraph, and somehow using that result to calculate the correct portion of the string with the HTML tags to pass to DrawString?

Or, is there a way of telling PdfStringLayouter to ignore HTML tags when calculating the line breaks? (whilst still maintaining them in the output)?

Any tips greatly appreciated!

Thanks,

Steve.

stevesuk
 
Posts: 2
Joined: Wed Jun 22, 2022 11:06 am

Fri Jun 24, 2022 8:07 am

Hello Steve,

Thanks for your inquiry.

I did notice the problem you mentioned and did a lot of tests with many different texts containing HTML tags. Unfortunately, after finally communicating with the development team, I confirmed that PdfStringLayouter does not currently support HTML tags. But we think this feature is useful, so we will try to implement it or achieve this need with other solutions. The ticket for this issue is SPIREPDF-5292. We will keep you informed when there is any update for this issue.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed Jul 27, 2022 10:26 am

Hi Steve,

Hope you are doing well.

Glad to inform you that we just released Spire.Pdf hotfix Version:8.7.9 which fixes your issue SPIREPDF-5292. Please download the new version from the links below.

Please download the fix version from the following links to test.
Website link:
https://www.e-iceblue.com/Download/down ... t-now.html
Nuget:
https://www.nuget.org/packages/Spire.PDF/8.7.9

Besides, In this case, we made adjustments to the DrawString method. It will ignore HTML tags when calculating for splitting string and preserve the effect of HTML tags when drawing strings. Below is the test code for reference.
Code: Select all
PdfDocument pdf = new PdfDocument();

PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial",10f,FontStyle.Regular));
PdfStringFormat format4 = new PdfStringFormat();

String text = "aAAA AA<strong>SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS</strong>AAA<b>BBBBBBBBBBBBBBB</b>";
format4.LineSpacing = font5.Size * 1.5f;

Rectangle rect = new Rectangle(50, 0, 200, 400);
page.Canvas.DrawString(text, font5, PdfBrush.Red, rect, format4, true);

pdf.SaveToFile("output.pdf",FileFormat.PDF);
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Thu Nov 24, 2022 3:29 pm

Thank you - and sorry, I missed this post.

I've just tried out the newer version in my solution, and found a problem.

I have an HTML string, which I pass into the Layout method of PdfStringLayouter - this splits the result into a series of lines, ready for output (as mentioned, we have a column layout, so sometimes a paragraph of text needs splitting between columns).

I output each line one by one into the document using the DrawString method (with htmlTags set to true).

This is all fine, unless the opening/closing HTML tags span multiple lines.

e.g.

Line 1 = This is <strong>a test
Line 2 = of Spire.PDF</strong>.

In this sort of scenario, DrawString seems to be throwing an exception, usually something like "System.ArgumentOutOfRangeException: 'Length cannot be less than zero. '

I guess what I need to do to fix this, is to parse each line to add the missing opening/closing HTML tags. e.g. so the above would become:

Line 1 = This is <strong>a test</strong>
Line 2 = <strong>of Spire.PDF</strong>.

Another question if that's OK? You mentioned that this change was part of hotfix 8.7.9.

So far, we've been using Free Spire PDF (although I downloaded "normal" Spire PDF to test out this fix). Free Spire PDF is still on 8.6. Will this be updated to match Spire PDF at some point?

Thank you.

stevesuk
 
Posts: 2
Joined: Wed Jun 22, 2022 11:06 am

Fri Nov 25, 2022 9:51 am

Hello,

Thanks for your feedback.
Please kindly note if you pass HTML string into PdfStringLayouter, HTML string will be treated as plain text. Therefore, there will appear the issue you mentioned when you call the DrawString method. However, you can refer to the following test code my colleague Andy provided last time to achieve your requirement.
In addition, this fix will be updated to our FreeSpire.Pdf in the future, however, due to the FreeSpire.Pdf is maintained irregularly, we can’t give you exactly ETA about updating this fix to FreeSpire.Pdf.

Code: Select all
PdfDocument pdf = new PdfDocument();

PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial",10f,FontStyle.Regular));
PdfStringFormat format4 = new PdfStringFormat();

String text = "aAAA AA<strong>SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS</strong>AAA<b>BBBBBBBBBBBBBBB</b>";
format4.LineSpacing = font5.Size * 1.5f;

Rectangle rect = new Rectangle(50, 0, 200, 400);
page.Canvas.DrawString(text, font5, PdfBrush.Red, rect, format4, true);

pdf.SaveToFile("output.pdf",FileFormat.PDF);


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF