This is a migrated thread and some comments may be shown as answers.

Scalining issues on textfragment

5 Answers 230 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Steven
Top achievements
Rank 1
Steven asked on 29 Aug 2019, 12:08 AM

I am having trouble positioning text using text fragment.  I'm not sure if its a scaling issue during import or what

Below is an extract that failes

I created a PDF completely gray with a black box starting at 100x100 pixels from the top left corner (https://www.pastefile.com/sjKGiJ)

The Original PDF was created by printing a blank page from work through adobe acrobat (for sizing) and then edited in illustrator to add the image.

Via the processor

The file is the imported using PdfStreamWriter and result written to a memory stream

The memorystream is the imported using RadFixedDocument  and then text is added at 100x100 position.

Result is written to a file

Resulting file (https://www.pastefile.com/4HZwvR)

The text does not appear at 100x100 but at at ~ 74x70ish

the text font is not Helvetica but Arial

the font size is not 10pt but 5.63 pt

Code below (result is test4.pdf)

 

Imports Telerik.Windows.Documents.Fixed.FormatProviders.Pdf
Imports Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming
Imports Telerik.Windows.Documents.Fixed.Model
 
Public Class Form1
    Public pageLetter As New System.Windows.Size(816, 1056)
 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 
        'Load file into byte array
        Dim page1 As Byte() = ReadFile("d:\test2.pdf")
 
        Dim originalsize As String = ""
        '  Load source document
        Dim sourceDocument As New IO.MemoryStream(page1)
        Dim sourceDocumentPDF As New PdfFileSource(sourceDocument)
 
        Dim resultIO As New IO.MemoryStream
        Using fileWriter As PdfStreamWriter = New PdfStreamWriter(resultIO)
 
            ' Create file same size as source
            Using targetPDF As PdfPageStreamWriter = fileWriter.BeginPage(pageLetter)
 
 
                ' Store original size to write it out later
                originalsize = sourceDocumentPDF.Pages(0).Size.Width & "x" & sourceDocumentPDF.Pages(0).Size.Height
                ' Write file
                targetPDF.ContentPosition.Clear()
                targetPDF.ContentPosition.Translate(0, 0)
                targetPDF.ContentPosition.Scale(1, 1)
                targetPDF.WriteContent(sourceDocumentPDF.Pages(0))
 
            End Using
 
        End Using
 
 
        Dim provider As New PdfFormatProvider()
        Dim doc1 As RadFixedDocument = provider.Import(resultIO.ToArray)
        Dim pg As RadFixedPage = doc1.Pages(0)
 
        Dim t As Telerik.Windows.Documents.Fixed.Model.Text.TextFragment = New Telerik.Windows.Documents.Fixed.Model.Text.TextFragment()
        t.Font = Fonts.FontsRepository.Helvetica
        t.FontSize = Telerik.Windows.Documents.Media.Unit.DipToPoint(10)
        t.Text = "TEST TEXT PDF SIZE" & originalsize
        pg.Content.Add(t)
 
        t.Position.Translate(100, 100)
        Dim b() As Byte = provider.Export(doc1)
        Dim io2 As New IO.FileInfo("d:\test4.pdf")
        Dim sw As IO.FileStream = io2.OpenWrite()
        sw.Write(b, 0, b.Length)
        sw.Close()
 
 
 
    End Sub
 
    Shared Function ReadFile(filename As String) As Byte()
 
        Dim io As New IO.FileInfo(filename)
 
        Dim fs As IO.FileStream = io.OpenRead
        Dim b() As Byte
        ReDim b(io.Length)
        fs.Read(b, 0, io.Length)
        fs.Close()
        Return b
 
    End Function
 
End Class

5 Answers, 1 is accepted

Sort by
0
Steven
Top achievements
Rank 1
answered on 29 Aug 2019, 12:12 AM

Issue continues without the PdfStreamWriter step

 

Private Sub Button2_Click2(sender As Object, e As EventArgs) Handles Button2.Click
 
    'Load file into byte array
    Dim page1 As Byte() = ReadFile("d:\test2.pdf")
 
 
    Dim provider As New PdfFormatProvider()
    Dim doc1 As RadFixedDocument = provider.Import(page1) 'resultIO.ToArray)
    Dim pg As RadFixedPage = doc1.Pages(0)
 
    Dim t As Telerik.Windows.Documents.Fixed.Model.Text.TextFragment = New Telerik.Windows.Documents.Fixed.Model.Text.TextFragment()
    t.Font = Fonts.FontsRepository.Helvetica
    t.FontSize = Telerik.Windows.Documents.Media.Unit.DipToPoint(10)
    t.Text = "TEST TEXT PDF SIZE" '& originalsize
    pg.Content.Add(t)
 
    t.Position.Translate(100, 100)
    Dim b() As Byte = provider.Export(doc1)
    Dim io2 As New IO.FileInfo("d:\test4.pdf")
    Dim sw As IO.FileStream = io2.OpenWrite()
    sw.Write(b, 0, b.Length)
    sw.Close()
 
End Sub
0
Steven
Top achievements
Rank 1
answered on 29 Aug 2019, 02:33 PM

I have also seen the same positioning issue with placing text fields

 

Dim textbox As New InteractiveForms.TextBoxField("field" & counter)
textbox.DefaultValue = field.text
Dim widget As Annotations.VariableContentWidget = textbox.Widgets.AddWidget()
widget.Rect = New Rect(New System.Windows.Point(100,100), New System.Windows.Size(130, 20))
widget.TextProperties.FontSize = 10
widget.RecalculateContent()
forms.FormFields.Add(textbox)
pg.Annotations.Add(widget)
0
Steven
Top achievements
Rank 1
answered on 29 Aug 2019, 04:28 PM

I think my issue is because seems the size is in DIP not PX.

 

I cant seem to find a means of converting DIP to PX or vice versa. 

 

Any suggestions?

0
Steven
Top achievements
Rank 1
answered on 29 Aug 2019, 05:13 PM

 

Maybe there is a better way of doing this but i have had success with the following

then mulitplying all PX values by pixelw  to get DIP

 

         Dim w As Double = Telerik.Windows.Documents.Media.Unit.InchToDip(8.5)
       Dim h As Double = Telerik.Windows.Documents.Media.Unit.InchToDip(11)
       pageLetter = New System.Windows.Size(w, h)
       pixelw = w / 612
0
Tanya
Telerik team
answered on 02 Sep 2019, 07:56 AM

Hi Steven,

I am happy to see that you have found the solution for this case.

Indeed, the measurement unit used in PdfProcessing is DIPs and you can use the Unit class to obtain their values in the unit you would need.

Regards,
Tanya
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PdfProcessing
Asked by
Steven
Top achievements
Rank 1
Answers by
Steven
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or