Using aspose to change my code

Hi Team:

the attachment is my code snippet, and the txt sample data,I I try to transform from txt to pdf using vb.net, and would like to change font to specif text.
base on my code that using aspose to change it, but seems not apply to it, if there is any misunderstanding on how to using the function, please point it out for me, very thanks.
messageImage_1700794860615.jpg (128.9 KB)

@accesssoft

Are you sure that the font you are using to assign exists in the system and it supports the language characters? Can you please share your complete code snippet with us so that we can test the scenario in our environment and address it accordingly.

Dim line As String = String.Empty, character As String = String.Empty
Dim cw, rh, Xn, Yn As Single
Dim totalRow As Integer = 1
Dim rowCount As Single = CDec(para(6)), columnCount As Single = CDec(para(5))
Dim m As Match
Dim cFont As Font = Nothing, fontSize As Single, cUserFont As Font = Nothing
Dim TextData = File.ReadAllText(para(7), Encoding.Default)
Dim Memoy As New MemoryStream

    Using ms As System.IO.MemoryStream = New System.IO.MemoryStream
        '20230808-Lausen修改
        'Using writer As PdfWriter = PdfWriter.GetInstance(document, ms)
        Dim Pdf = New Aspose.Pdf.Document()

        With Pdf
            Dim Page = .Pages.Add()

            SetPageMarginAndFont(Pdf, cFont, cUserFont, fontSize, para(0), para(1), para(2))

            Xn = marginLeft + paddingLeft
            Yn = .Pages(1).Rect.URY - marginTop - paddingTop
            rh = (Pdf.Pages(1).Rect.URY - marginTop - marginBottom) / rowCount
            cw = (Pdf.Pages(1).Rect.URX - marginLeft - marginRight) / columnCount
            Dim p As Double
            '依字形跟字距比例設定字寬壓縮比
            If cw * 2 < para(1) Then
                p = 100 * cw * 2 / para(1)          '字寬壓縮比0~100,預設值為100\
                .Pages(1).PageInfo.DefaultTextState.HorizontalScaling = p
            End If

            Dim Text As New List(Of Aspose.Pdf.Text.TextFragment)
            .Pages(.Pages.Count).Paragraphs.Add(New Aspose.Pdf.Text.TextFragment(TextData))


            Pdf.Save(Memoy)
        End With
    End Using
    Dim Pdf2 = New Aspose.Pdf.Document(Memoy)
    Dim absorber = New TextFragmentAbsorber()
    For Each itemdata In TextData
        absorber.Phrase = itemdata

        Pdf2.Pages.Accept(absorber)
        For Each item In absorber.TextFragments


            Dim bIsExtB As Boolean = Char.IsHighSurrogate(item.Text)
            If bIsExtB Then
                '高代理字是4個byte,把後面的兩個byte也讀進來一起處理,才能show出字型

                item.TextState.Font = cFont
                item.TextState.FontSize = 12

            End If
            Dim bIsUserDefWord As Boolean = isUserDefWord(item.Text)    '是否為造字
            If bIsUserDefWord Then  '切換至造字字型
                item.TextState.Font = cUserFont
                item.TextState.FontSize = 64

                '判斷是否為全形字,但用字碼判斷會有風險,目前全形英數字會被判定為半形字,所以改取高位元組是否為0且參數用None來判斷應該比較適合
                'm = Regex.Match(s, "[\u3000-\u303F\u2190-\u2793\u4e00-\u9fff]+", RegexOptions.IgnoreCase)
                'Xn += IIf(m.Success, 2 * cw, cw)
                m = Regex.Match(item.ToString(), "[\u0000-\u00FF]+", RegexOptions.None)
                Xn += IIf(m.Success, cw, 2 * cw)
                '除了造字之外,如果是擴充字集,也要將預設字型切換回來
                'If bIsUserDefWord Then p.SetFontAndSize(cFont, fontSize) '若切換過造字,印完該字要切換回來

                '若切換過造字或擴充字集,印完該字要切換回來

            End If

        Next
    Next

    Pdf2.Save(filename & ".pdf")

@accesssoft

Please share the sample .txt file in .zip format as well so that we can test the scenario in our environment and address it accordingly.

@accesssoft

We apologize for the confusion but, we requested for the Text file from which you are reading content and adding it into the PDF. Also, there are many undefined objects and missing methods in your code snippet that we cannot simply run and execute it to test the case. You can share a simple and minimal code snippet that can be used to replicate the original issue i.e. font is not being assigned correctly. For example, below is the sample code to read text from .txt file and add it into the PDF:

Dim tr As TextReader = New StreamReader(dataDir & "input.txt", Encoding.UTF8, True)
Dim doc As New Aspose.Pdf.Document()
Dim page As Page = doc.Pages.Add()
Dim strLine As String
Dim builder As New TextBuilder(page)
Dim x As Double = 100
Dim y As Double = 100

While (InlineAssignHelper(strLine, tr.ReadLine())) IsNot Nothing
    Dim text As New Aspose.Pdf.Text.TextFragment(strLine)
    'page.Paragraphs.Add(text)
    text.Position = New Position(x, y)

    If y >= page.PageInfo.Height - 72 Then
        y = 100
        page.Dispose()
        page = doc.Pages.Add()
        builder = New TextBuilder(page)
    Else
        y += 15
    End If

    builder.AppendText(text)
End While

doc.Save(dataDir & "TexttoPDF_out.pdf")
tr.Close()

' Helper function for inline assignment
Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
    target = value
    Return value
End Function

@accesssoft

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56029

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@accesssoft

We still could not execute your code snippet in our environment because it is not complete and has missing objects. Therefore, we tried to use below code snippet just to read the data from text file (集保難字.txt) and assign SimSun font while adding text to the PDF:

System.IO.TextReader tr = new StreamReader(dataDir + "集保難字.txt", Encoding.UTF8, true);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
String strLine;
TextBuilder builder = new TextBuilder(page);
double x = 100; double y = 100;
while ((strLine = tr.ReadLine()) != null)
{
    Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment(strLine);
    text.TextState.Font = FontRepository.FindFont("SimSun");
    //page.Paragraphs.Add(text);
    text.Position = new Position(x, y);
    if (y >= page.PageInfo.Height - 72)
    {
        y = 100;
        page.Dispose();
        page = doc.Pages.Add();
        builder = new TextBuilder(page);
    }
    else
    {
        y += 15;
    }
    builder.AppendText(text);
}
doc.Save(dataDir + "TexttoPDF_out.pdf");
tr.Close();

We obtained a different type of PDF document with different formatting because of the changed logic off course. Other than the (?) characters which are rendering at customer’s end, we were able to see the blocks for the same text content that you showed in the screenshot.
TexttoPDF_out.pdf (311.0 KB)

We have logged the ticket shared in above message to further analyze this issue. We will further request for the information in case we need some for the analysis. Please spare us some time.

We are sorry for the inconvenience.

Hi
is it possible setup an online meeting?

Peggy

@accesssoft

We are afraid that an online meeting would not be possible through free support channel. However, you can share more material, screenshots, sample project or recordings to further explain the scenario in case our previous understandings are not clear. We will act accordingly and do our best to address the issue.