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