PDFsharp: How to find the Size dimensions of all the pages in a PDF file? - vb.net

I am using PDFsharp, a great tool for working with PDFs.
I am writing an application in VB.net to work with PDFs for the printing industry. I need to know how to find out the dimensions of each page of the PDF.

Loop through all pages in the PDF and query the page size for each page.
Use the Pages property of the PdfDocument object.

Function GetPDFMetaData(ByRef pSourceFile As OpenFileDialog)
Dim lpdfDocument As PdfDocument = PdfReader.Open(pSourceFile.FileName, PdfDocumentOpenMode.Import)
Dim lpdfpage As PdfPage
Dim Text As String = ""
Dim Width As Integer
Dim Height As Integer
For idx As Integer = 0 To lpdfDocument.PageCount - 1
lpdfpage = lpdfDocument.Pages(idx)
Width = lpdfpage.Width.Millimeter
Height = lpdfpage.Height.Millimeter
Text = Text & vbCrLf & "Page: (" & idx + 1 & "); Size =(" & Width & " X " & Height & ")"
Next
Return Text
End Function

Related

Using iText7.Net, why dimensions returned by iText.Kernel.Geom.Point are 10 times greater that TextRenderInfo.LineSegment.GetStartPoint()?

I written a VB.Net program that intercept RENDER_PATH and RENDER_TEXT events generated by iText7 module.
I have written a little code to find location of TEXT.
Dim ascent As LineSegment = t.GetAscentLine()
Dim descent As LineSegment = t.GetDescentLine()
Dim initX As Single = descent.GetStartPoint().Get(0)
Dim initY As Single = descent.GetStartPoint().Get(1)
Dim endX As Single = ascent.GetEndPoint().Get(0)
Dim endY As Single = ascent.GetEndPoint().Get(1)
For specific PDF page, all values returned by GetStartPoint() and GetEndPoint() are between 20 and 600.
To find PATH values, I have writte following code
Private Sub RenderPath(render As PathRenderInfo)
For Each sp As Subpath In render.GetPath().GetSubpaths()
Console.WriteLine(render.GetPath().ToString())
For Each segment In sp.GetSegments()
Console.WriteLine(" " & segment.ToString())
Select Case segment.GetType().FullName
Case "iText.Kernel.Geom.Line"
Dim oLine As iText.Kernel.Geom.Line = segment
Dim oList As List(Of Point) = oLine.GetBasePoints()
Dim n = 0
For Each p In oList
Console.WriteLine(" p" & CStr(n) & ".x: " & CStr(oList(n).GetX()))
Console.WriteLine(" p" & CStr(n) & ".y: " & CStr(oList(n).GetY()))
n += 1
Next
Console.WriteLine(" width: " & CStr(oList(0).GetX() - oList(1).GetX()))
Console.WriteLine(" height: " & CStr(oList(0).GetY() - oList(1).GetY()))
Case "iText.Kernel.Geom.BezierCurve"
Case Else
Dim i0 = 0
End Select
Next
Next
End Sub
All location's values returned by GetX() and GetY() functions are now between ... 200 and 6000 !
Why PATH location's values seems to be 10 times greater that TEXT location's values ?
Is that normal or is that a BUG ?
In iText7, what are dimensions of TEXT locations and dimensions of PATH segments ?
In iText7, what are dimensions of TEXT locations and dimensions of PATH segments ?
Indeed, the coordinates returned by TextRenderInfo and those returned by PathRenderInfo differ:
Coordinates returned by TextRenderInfo are given in the default user space coordinates of the given page, i.e. all active transformations are already accounted for.
Coordinates returned by PathRenderInfo, on the other hand, are given in the current user space coordinates - current when the path is constructed and drawn. To transform these coordinates into default user space coordinates, you have to apply the CTM (current transformation matrix) to the path. You can retrieve the CTM using the GetCTM method of the path render info object.
That different render info classes return coordinates in conceptually different coordinate system probably isn't intuitive and should be made clearer.
In case of your document page the CTM appears to be a scaling transformation by a factor of 0.1.

Bitmap read, write & compare

A) When I write a BMP file & then read it back into another bitmap object the objects appear different. Looks like Horizontal & Vertical Resolutions are changed.
B) When I make the bitmap object resolutions the same the bitmap objects still appear different despite all attributes appearing the same.
C) If I read the same BMP file twice into two different bitmap objects the objects appear different.
Eventually I want to clone small sections of a larger bitmap & ascertain whether I already have a file for the smaller section by comparing against the contents of each known previously generated file in turn. That is, by reading each file as a bitmap I want to be able to compare against the small section bitmap.
I will eventually be placing a checksum in each file's name or attributes for quicker exclusion but will still need to compare each file contents of those who "survive the exclusion" to prevent different files with the same checksum issue.
When run, the code below shows the issues.
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim sFileName As String = "c:\Temp\Red.bmp"
' Create red coloured bitmap
Dim oBitMap1 As New Bitmap(200, 100)
Dim oGraphics = Graphics.FromImage(oBitMap1)
oGraphics.FillRectangle(Brushes.Red, 0, 0, 200, 100)
Dim oBitMapA As New Bitmap(200, 100)
oBitMapA = oBitMap1
Debug.Print("oBitMap1 Is oBitMapA = " & CStr(oBitMap1 Is oBitMapA))
' Save the bitmap
oBitMap1.Save(sFileName, Imaging.ImageFormat.Bmp)
' Read the "same" details back into oBitMap2 & BitMap3
Dim oBitMap2 = New Bitmap(sFileName)
Dim oBitMap3 = New Bitmap(sFileName)
'Dim oBitMap2 As Bitmap = Image.FromFile(sFileName)
'Dim oBitMap3 As Bitmap = Image.FromFile(sFileName)
' Show what's what
Debug.Print("Show whether BitMaps are the same (pre changes)")
Debug.Print("oBitMap1 Is oBitMap2 = " & CStr(oBitMap1 Is oBitMap2))
Debug.Print("oBitMap2 Is oBitMap3 = " & CStr(oBitMap2 Is oBitMap3))
Debug.Print("Show resolutions (pre any changes)")
Debug.Print("oBitMap1.Resolution = " & Format(oBitMap1.HorizontalResolution, "##0.###") & "," & Format(oBitMap1.VerticalResolution, "##0.###"))
Debug.Print("oBitMap2.Resolution = " & Format(oBitMap2.HorizontalResolution, "##0.###") & "," & Format(oBitMap2.VerticalResolution, "##0.###"))
Debug.Print("oBitMap3.Resolution = " & Format(oBitMap3.HorizontalResolution, "##0.###") & "," & Format(oBitMap3.VerticalResolution, "##0.###"))
oBitMap2.SetResolution(96, 96)
oBitMap3.SetResolution(96, 96)
Debug.Print("Show resolutions (post changes)")
Debug.Print("oBitMap1.Resolution = " & Format(oBitMap1.HorizontalResolution, "##0.###") & "," & Format(oBitMap1.VerticalResolution, "##0.###"))
Debug.Print("oBitMap2.Resolution = " & Format(oBitMap2.HorizontalResolution, "##0.###") & "," & Format(oBitMap2.VerticalResolution, "##0.###"))
Debug.Print("oBitMap3.Resolution = " & Format(oBitMap3.HorizontalResolution, "##0.###") & "," & Format(oBitMap3.VerticalResolution, "##0.###"))
Debug.Print("Show whether BitMaps are the same (post changes)")
Debug.Print("oBitMap1 Is oBitMap1 = " & CStr(oBitMap1 Is oBitMap1))
Debug.Print("oBitMap1 Is oBitMap2 = " & CStr(oBitMap1 Is oBitMap2))
Debug.Print("oBitMap2 Is oBitMap3 = " & CStr(oBitMap2 Is oBitMap3))
End Sub
Using descriptions above
A) I'm expecting oBitMap2 read from the file created from oBitMap1 to be the same as oBitMap1 but the resolutions are different.
B) When I set the oBitMap2 resolutions to be the same as oBitMap1 I'm expecting oBitMap1 & oBitmap2 to be the same but they're different.
C) When I read the same file twice I'm expecting the two bitmaps created (oBitMap2 & oBitMap3) to be the same but they're different.
I am wondering whether I've misunderstood the "Is" compare operator or the bitmaps have "hidden" attributes that don't show when I "print" them in the debugger as all the shown attributes appear to be the same.

word vba - remove manually typed list number

I have lot of documents with list number typed manually, so my intent is to remove those manual list number and the tab or space following it.
e.g
1. Text 1
1.1 Text 2
1.1.1 Text 3
1.1.1.1 Text 4
become
Text 1
Text 2
Text 3
Text 4
I'm not sure how to do this with vba and I'd really appreciate your help.
I have figured it out by using below code:
Sub RemoveManualListNumber()
Dim strInitialPar As String
Dim intSpace As Integer, intTab As Integer
Dim strFinalPar As String
Dim iPar
For iPar = 1 To ActiveDocument.Paragraphs.Count
strInitialPar = ActiveDocument.Paragraphs(iPar).Range.Text
intSpace = InStr(1, strInitialPar, " ")
intTab = InStr(1, strInitialPar, Chr(9))
Debug.Print "Paragraph " & iPar & ": " & "Index space = " & intSpace & ", Index tab = " & intTab
If intTab > 0 And intTab < intSpace Then
strFinalPar = Right(strInitialPar, Len(strInitialPar) - intTab)
Else
strFinalPar = Right(strInitialPar, Len(strInitialPar) - intSpace)
End If
ActiveDocument.Paragraphs(iPar).Range.Text = strFinalPar
Next iPar
End sub

Word VBA: how to split paragraph into two styles?

I made a program that goes through the document and if there is a paragraph with tab in it, it splits it in two:
sSPlit = Split(aPara.Range.Text, vbTab)
aPara.Range.Text = sSPlit(0) & vbCrLf & sSPlit(1)
That works great. The problem is, I'd like the first splitted paragraph to have "Style1" and the second "Style2".
aPara.Style = "Style1"
adds this style to the next, yet unsplitted paragraph. Please help.
One possible solution is to calculate the ranges to apply the style to and then simply retrieve the range of calculated characters, e.g.:
Dim par1Start As Integer
Dim par2Start As Integer
par1Start = aPara.Range.Start
par2Start = par1Start + Len(sSplit(0)) + 1
aPara.Range.Text = sSplit(0) & vbCrLf & sSplit(1)
ActiveDocument.Range(par1Start, par2Start).Style = "Style1"
ActiveDocument.Range(par2Start, par2Start + Len(sSplit(1))).Style = "Style2"

Find height and width of pdf using iTextSharp

In my window application i am using Quick pdf to find height,width of pdf.please refer the below code.But sometimes quickpdf can't able to find correct value.so i want to do the task using iTextSharp. How to find pdf height,width using iTextSharp?
Try
Dim qp1 As New iSED.QuickPDF
Dim sPDFHei, sPDFWid As Double
Dim iPgCnt As Integer
qp1.UnlockKey("6510E9D5C3938A920B3A8D7293C6DF00")
qp1.LoadFromFile(sFilePath & "\" & cmbArticles.Text)
qp1.SetMeasurementUnits(0)
iPgCnt = qp1.PageCount
For i As Integer = 1 To iPgCnt
'MsgBox("pagecount=" & iPgCnt)
qp1.SelectPage(i)
'MsgBox("PageNumber=" & i)
sPDFHei = qp1.PageHeight : sPDFWid = qp1.PageWidth
sPDFHei = Math.Round(sPDFHei, 2) : sPDFWid = Math.Round(sPDFWid, 2)
''If sPDFWid <> 8.26 And sPDFHei <> 11.69 Then
If sPDFWid > 520 Or sPDFHei > 600 Then
MsgBox("Article PDF size should not exit 7.22 x 8.33" & vbCrLf & "Problem in template:" & i)
Exit Function
End If
Next
qp1.clear()
Checkpdfsize = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "CheckPdfSize")
End Try
Thanks in Advance
Youll find what you need in chapter 6 of their documentation here. Its under Action by the way.