문제

I want to do something very simple but I can't find out how. This is getting very annoying, here's my problem.

I have the following text that I want to add inside a aspose.pdf section

 "<h1>What a nice <i>italic</i> freaking title</h1>"

Since the PDF generation of apsose does not correctly support css styles, it seams, my team decided that we would take a different approach and that is : support basic html elements and add them directly using the aspose object library with predefined style in the code. Now, I made the following function to add a title:

    public static void AddTitle(XElement xElement, Section section, TitleType type)
    {
        Text title;
        title = xElement.HasElements ? new Text(section, GetFullValue(xElement)) : new Text(section, xElement.Value);
        var info = new TextInfo();
        switch(type)
        {
            case TitleType.H1:
                info.FontSize = 22;
                break;
            case TitleType.H2:
                info.FontSize = 20;
                break;
            case TitleType.H3:
                info.FontSize = 18;
                break;
            case TitleType.H4:
                info.FontSize = 16;
                break;
            case TitleType.H5:
                info.FontSize = 14;
                break;
        }

        info.IsTrueTypeFontBold = true;
        title.IsKeptTogether = true;
        //title.IsHtmlTagSupported = true;
        title.TextInfo = info;
        section.Paragraphs.Add(title);
    }

BTW: The content passed to the text object in this example is "This is a nice italic freaking title"

Currently, the italic is not working. If I uncomment the .IsHtmlTagSupported, italic will work but I'm losing my TextInfo(bold, font size etc). Is there a way to make this work? Or else, is there a way to append text in a single paragraph with different styles. (Like can I add another Text object to my paragraph that contains the italic text)

도움이 되었습니까?

해결책

When using the following simple code with Aspose.Pdf for .NET 7.7.0, I can see Italic text and all string in H1. When using IsHtmlTagSupported, its recommended to specify the text formatting (Bold, FontSize etc) using HTML Tags.

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Text title = new Text("<h1>What a nice <i>italic</i> freaking title</h1>");
var info = new TextInfo();
info.FontSize = 12;
//info.IsTrueTypeFontBold = true;
title.TextInfo = info;

title.IsHtmlTagSupported = true;

sec1.Paragraphs.Add(title);
pdf1.Save("c:/pdftest/PDF_From_HTML.pdf");

Furthermore, a Text paragraph can have one or more Segment object and you can specify different formatting for each segment. In order to fulfill your requirement, you may try using the following code snippet

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Text title = new Text("What a nice");

var info = new TextInfo();
info.FontSize = 12;
info.IsTrueTypeFontBold = true;
title.TextInfo = info;

Segment segment2 = new Segment(" italic ");
// set the formatting of segment2 as Italic
segment2.TextInfo.IsTrueTypeFontItalic = true;
// add segment2 to segments collection of text object
title.Segments.Add(segment2);

Segment segment3 = new Segment(" freaking title ");
// add segment3 to segments collection of text object
segment3.TextInfo.IsTrueTypeFontBold = true;
title.Segments.Add(segment3);

sec1.Paragraphs.Add(title);
// save the output document
pdf1.Save("c:/pdftest/PDF_with_Three_Segments.pdf");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top