We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace PDF text using Aspose.PDF for .NET - text overlapping and change in text position

I am attempting to replace some text within an existing PDF however i need to replace the textfragment with a paragraph so i can take advantage of the word wrapping of paragraphs. However creating a paragraph with the positioning and sizing of the found fragment produces an offset result (i expect in the example below the new paragraph to exactly over lap the existing text fragment) also if the paragraph rectangle is set to that of the fragment and wordwrap is enabled then the text is wrapped given the paragraph being the same size i would expect the tedt to not be wrapped given the size is the same as the textfragment and margins are zero .

Additionally i was under the impression that a documents origin is Lower left 0,0 but when i add text set to inlineparagraph to the page.paragraph collection the text is not appearing the position i have specified, instead it is appearing far right in the example and rotated

    Dim doc As New Document
    doc.PageInfo.Height = Pdf.PageSize.A1.Height
    doc.PageInfo.Width = Pdf.PageSize.A1.Width

    Dim page As Page = doc.Pages.Add
    page.Rotate = Rotation.on90

    Dim builder As New TextBuilder(page)

    Dim fragment1 As New TextFragment("Text Builder 1")
    fragment1.TextState.Font = FontRepository.FindFont("Times New Roman")
    fragment1.TextState.FontSize = 12
    fragment1.Position.XIndent = 100
    fragment1.Position.YIndent = 100
    builder.AppendText(fragment1)

    fragment1 = New TextFragment("Page Paragraph") ''this appears right hand side at top? put PDF origin should be 0,0 LLX,LLY so with a 90 Deg rotation this is still incorrect
    fragment1.TextState.Font = FontRepository.FindFont("Times New Roman")
    fragment1.TextState.FontSize = 12
    fragment1.Position.XIndent = 100
    fragment1.Position.YIndent = 200
    fragment1.IsInLineParagraph = True
    page.Paragraphs.Add(fragment1)

    fragment1 = New TextFragment("Text Builder 2")
    fragment1.TextState.Font = FontRepository.FindFont("Times New Roman")
    fragment1.TextState.FontSize = 12
    fragment1.Position.XIndent = 100
    fragment1.Position.YIndent = 300
    builder.AppendText(fragment1)

    ''find the text builder and add a paragraph over the top
    Dim absorber As New TextFragmentAbsorber("Text Builder 1")
    doc.Pages.Accept(absorber)

    For Each textFrag As TextFragment In absorber.TextFragments
        Dim newPar As New TextParagraph
        newPar.Rectangle = New Rectangle(textFrag.Rectangle.LLX, textFrag.Rectangle.LLY, textFrag.Rectangle.URX, textFrag.Rectangle.URY)
        newPar.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords
        Dim newFrag As New TextFragment("Text Builder 1")
        newFrag.TextState.ForegroundColor = Color.Green
        newFrag.TextState.Font = textFrag.TextState.Font
        newFrag.TextState.FontSize = textFrag.TextState.FontSize

        newPar.AppendLine(newFrag)
        builder.AppendParagraph(newPar)

    Next

    doc.Save("c:\temp\foo5.pdf")

    ''reopen the pdf in case when it renders to file the positions are corrected
    doc = New Document("c:\temp\foo5.pdf")
    page = doc.Pages(1)
    builder = New TextBuilder(page)
    absorber = New TextFragmentAbsorber("Text Builder 2")
    doc.Pages.Accept(absorber)

    For Each textFrag As TextFragment In absorber.TextFragments
        Dim newPar As New TextParagraph
        newPar.Rectangle = New Rectangle(textFrag.Rectangle.LLX, textFrag.Rectangle.LLY, textFrag.Rectangle.URX, textFrag.Rectangle.URY)
        newPar.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.NoWrap
        Dim newFrag As New TextFragment("Text Builder 2")
        newFrag.TextState.ForegroundColor = Color.Red
        newFrag.TextState.Font = textFrag.TextState.Font
        newFrag.TextState.FontSize = textFrag.TextState.FontSize

        newPar.AppendLine(newFrag)
        builder.AppendParagraph(newPar)

    Next

    doc.Save("c:\temp\foo5.pdf")

    ''reopen the pdf in case when it renders to file the positions are corrected
    doc = New Document("c:\temp\foo5.pdf")
    page = doc.Pages(1)
    builder = New TextBuilder(page)
    absorber = New TextFragmentAbsorber("Text Builder 2")
    doc.Pages.Accept(absorber)

    For Each textFrag As TextFragment In absorber.TextFragments
        Dim newPar As New TextParagraph
        newPar.Rectangle = New Rectangle(textFrag.Rectangle.LLX, textFrag.Rectangle.LLY, textFrag.Rectangle.URX, textFrag.Rectangle.URY)
        newPar.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.NoWrap
        Dim newFrag As New TextFragment("Text Builder 2")
        newFrag.TextState.ForegroundColor = Color.Red
        newFrag.TextState.Font = textFrag.TextState.Font
        newFrag.TextState.FontSize = textFrag.TextState.FontSize

        newPar.AppendLine(newFrag)
        builder.AppendParagraph(newPar)
    Next

    doc.Save("c:\temp\foo5.pdf")

@gushie

Thanks for contacting support.

Please note that there is no Setter for TextFragment.Position.XIndent and YIndent Properties. You can however set the position as following:

fragment1.Position = new Position(100, 200);

This way it will be placed as per the coordinating system followed by PDF i.e. (0,0) => lower-left.

We were able to notice this issue in our environment that new text was divided into two lines even when font size and text length was not changed. For the sake of further investigation and correction, we have logged an issue as PDFNET-47751 in our issue tracking system.

We will further look into details of the logged issue and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.