I made some simple functions for easy writing on top of an existing PDF.
Thanks to DonManfred post: PDFBox post#4
I need to calculate stringwidth for center align and right align.
On StackOverflow I found the stringwidth could by calculated with a function at font object.
But in the for B4J parsed PDFBox lib the font is set by FontAndColorHelper. That object doesn't support the width calculation.
What to do?
Thanks in advance,
Marc
Thanks to DonManfred post: PDFBox post#4
I need to calculate stringwidth for center align and right align.
On StackOverflow I found the stringwidth could by calculated with a function at font object.
But in the for B4J parsed PDFBox lib the font is set by FontAndColorHelper. That object doesn't support the width calculation.
What to do?
Simple one line functions for PDFbox:
Sub mmToPDFpixel(intmm As Double)As Int
Dim intResult As Int
'1/72 inch = 0,0352777777777778 cm = 1 pixel
'1cm Is dus: 1cm / 0,0352777777777778cm = 28,34pixels --> 28 pixels
'10mm Is dus: 10mm / 0,352777777777778mm = 28,34pixels --> 28 pixels
' 24mm Is dus: 24mm / 0,352777777777778mm = 68,03pixels --> 68 pixels
If intmm = 0 Then
intResult = 0
Else
intResult = Ceil(intmm / 0.352777777777778)
End If
Return intResult
End Sub
Sub SetPDFImage(cs As PDPageContentStream,doc As Object ,intXmmStart As Int, intYmmStart As Int, intHeightmm As Int, intWidthmm As Int,strFilePath As String)
Dim img As PDImageXObject
img.Initialize(doc,strFilePath) 'img
cs.drawImage2(img,mmToPDFpixel(intXmmStart), mmToPDFpixel(intYmmStart), mmToPDFpixel(intHeightmm), mmToPDFpixel(intWidthmm))
End Sub
Sub SetPDFHorzLine(cs As PDPageContentStream ,intXmmStart As Int, intYmmStart As Int, intmmlen As Int)
cs.addLine(mmToPDFpixel(intXmmStart),mmToPDFpixel(intYmmStart) ,mmToPDFpixel(intXmmStart+intmmlen),mmToPDFpixel(intYmmStart))
cs.closeAndStroke
End Sub
Sub SetPDFVertLine(cs As PDPageContentStream ,intXmmStart As Int, intYmmStart As Int, intmmlen As Int)
cs.addLine(mmToPDFpixel(intXmmStart),mmToPDFpixel(intYmmStart) ,mmToPDFpixel(intXmmStart),mmToPDFpixel(intYmmStart-intmmlen))
cs.closeAndStroke
End Sub
Sub SetPDFText(cs As PDPageContentStream, Fontname As Object, intFontSize As Int, intRcolor As Int, intGColor As Int, intBcolor As Int, intAlign As Int, blnLandscape As Boolean, intX As Int, intY As Int, strText As String)
Dim intPageWidth As Int
Dim intTextWidth As Int
'
If blnLandscape = True Then
intPageWidth = 297 'A4 in mm
Else
intPageWidth = 210 'A4 in mm
End If
cs.beginText()
cs.setFont(Fontname,intFontSize) 'Font, Font size
cs.setNonStrokingColor(intRcolor,intGColor,intBcolor) 'Color
cs.setStrokingColor(intRcolor,intGColor,intBcolor) 'color in my case the same color
intTextWidth = 90 'mm for test purpose a fixed value because string width calculation is not working yet.
'intTextWidth = Fontname.getStringWidth(strText) / 1000 * intFontSize
Select intAlign
Case 2
intX = mmToPDFpixel(intPageWidth-(intX+intTextWidth))
Case 3
intX = mmToPDFpixel(Ceil((intPageWidth-intTextWidth)/2))
Case Else
intX = mmToPDFpixel(intX)
End Select
intY = mmToPDFpixel(intY)
cs.newLineAtOffset(intX,intY) ' text position
cs.showText(strText) 'text
cs.endText()
End Sub
Thanks in advance,
Marc