itextsharp merge resizes and unrotates pdf - vb.net

I'm trying to merge pdf files using itextsharp.
The problem 'm getting its that any cropping or rotating I've applied to the individual files before the merge is somehow ignored. All original files were cropped and rotated as TIFFs then converted to pdf and now finally I'm trying to merge them.
I'd like the page size to match the added content, and I any rotation I've applied to come through.'
Thank you for any help,
Corbin de Bruin
Public Function MergePDFFiles(FileList As Dictionary(Of String, String), DeleteOldFile As Boolean) As Byte()
' Public Function MergePDFFiles(FileList As Dictionary(Of String, String), DeleteOldFile As Boolean) As MemoryStream()
Dim document As New Document()
Dim output As New MemoryStream()
Try
Dim writer As PdfWriter = PdfWriter.GetInstance(document, output)
writer.PageEvent = New PdfPageEvents()
document.Open()
Dim content As PdfContentByte = writer.DirectContent
' foreach
For Each FilePath As KeyValuePair(Of String, String) In FileList
If File.Exists(FilePath.Value) Then
Dim reader As New PdfReader(FilePath.Value)
Dim numberOfPages As Integer = reader.NumberOfPages
For currentPageIndex As Integer = 1 To numberOfPages
document.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex))
document.NewPage()
' you can see iTextSharp.tutorial.01 - 0403sample
If currentPageIndex.Equals(1) Then
Dim par As New Paragraph(FilePath.Key)
Debug.Print("FilePath.Key = " & FilePath.Key)
Dim bookmark As New Chapter(par, 0) With {.NumberDepth = 0}
document.Add(bookmark)
End If
Dim importedPage As PdfImportedPage = writer.GetImportedPage(reader, currentPageIndex)
Dim pageOrientation As Integer = reader.GetPageRotation(currentPageIndex)
If (pageOrientation = 90) OrElse (pageOrientation = 270) Then
content.AddTemplate(importedPage, 0, 1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(currentPageIndex).Height)
Else
content.AddTemplate(importedPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Next
End If
Next
Catch exception As Exception
Debug.Print("Failure")
Finally
document.Close()
End Try
If DeleteOldFile Then
'Delete(FileList)
End If
Return output.GetBuffer()
End Function
End Try
If DeleteOldFile Then
'Delete(FileList)
End If
Return output.GetBuffer()

This question has been answered over and over again on StackOverflow. It's amazing that nobody voted to close it as a duplicate.
In any case: as I've answered many times before, it is bad practice to use PdfWriter/PdfImportedPage to merge document. Please read chapter 6 of the book I wrote about iText, and you'll discover that whoever provided you with the code sample you copied was all wrong. You should use PdfCopy to merge files, not PdfWriter!
For examples, read the following StackOverflow answers:
How to keep original rotate page in itextSharp (dll)
How to merge multiple pdf files (generated in run time)?
Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying
and so on...
If you're fast enough in accepting this answer, you'll probably be lucky enough not do get downvoted for not searching the archives before posting an already answered question.

Related

How to Add a signature form field visible on all pages using itext7

as showed on another post, using iText7 I can add a signature field on a pdf document so that it is visible on first and last page.
So, I need to add an empty signature field visible on all pages.
Is there any way to do my goal?
I tried to ad the dame field multiple times (but N field will be created):
Using reader As New PdfReader(sourceFile)
Using writer As New PdfWriter(destFile)
Dim document As PdfDocument = New PdfDocument(reader, writer, New StampingProperties)
Dim r As New iText.Kernel.Geom.Rectangle(400, 380, 120, 60)
Dim field As PdfFormField = PdfFormField.CreateSignature(document, r)
field.SetFieldName(fieldName)
Dim form As PdfAcroForm = PdfAcroForm.GetAcroForm(document, True)
For i As Integer = document.GetNumberOfPages To 1 Step -1
form.AddField(field, document.GetPage(i))
Next
field.UpdateDefaultAppearance()
document.Close()
writer.Close()
End Using
End Using
I tried also to add widget to the same signature (but it seems not correct):
Using reader As New PdfReader(sourceFile)
Using writer As New PdfWriter(destFile)
Dim document As PdfDocument = New PdfDocument(reader, writer, New StampingProperties)
Dim r As New iText.Kernel.Geom.Rectangle(400, 380, 120, 60)
Dim field As PdfFormField = PdfFormField.CreateSignature(document, r)
field.SetFieldName(fieldName)
Dim form As PdfAcroForm = PdfAcroForm.GetAcroForm(document, True)
form.AddField(field, document.GetPage(1))
For i As Integer = 2 To document.GetNumberOfPages
Dim w As New PdfWidgetAnnotation(r)
w.MakeIndirect(document)
field.AddKid(w)
w.SetPage(document.GetPage(i))
field.SetPage(i)
Next
field.UpdateDefaultAppearance()
document.Close()
writer.Close()
End Using
End Using

Create a new picture along the GraphicsPath

Is there some way to copy a GraphicsPath and the enclosed figure into a new picture?
I have the rectangle, the points of the GraphicsPath available. The path is definitely in the rectangle.
I already googled but the result is poor. So far, I can only copy a certain area (rectangle) into a new picture, see source code.
Using Extracted As Bitmap = New Bitmap(rect.Width, rect.Height, Imaging.PixelFormat.Format32bppArgb)
Using Grp As Graphics = Graphics.FromImage(Extracted)
Grp.DrawImage(Picture1, 0, 0, rect, GraphicsUnit.Pixel)
End Using
If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
Extracted.Save("C:\Users\xy\Desktop\1.png", Imaging.ImageFormat.Png)
End If
End Using
I have found something here:
This is the solution translated into VB.Net and with Option Strict On and Option Infer Off.
Using bmpSource As Bitmap = New Bitmap(Form1.Pfad_Bild)
Dim rectCutout As RectangleF = gp.GetBounds()
Using m As Matrix = New Matrix()
m.Translate(-rectCutout.Left, -rectCutout.Top)
gp.Transform(m)
End Using
Using bmpCutout As Bitmap = New Bitmap(CInt(rectCutout.Width), CInt(rectCutout.Height))
Using graphicsCutout As Graphics = Graphics.FromImage(bmpCutout)
graphicsCutout.Clip = New Region(gp)
graphicsCutout.DrawImage(bmpSource, CInt(-rectCutout.Left), CInt(-rectCutout.Top))
If System.IO.Directory.Exists("C:\Users\xy\Desktop") Then
bmpCutout.Save("C:\Users\xy\Desktop\1.png", Imaging.ImageFormat.Png)
End If
End Using
End Using
End Using

Screen sharing through sockets

I'm making my first screen sharing application in VB.NET using sockets to establish the connections.
This is the client side receiving screen images from the server (they are both running in a thread):
Private Sub startscreen()
Using imgstream As NetworkStream = imgclient.GetStream()
Using ms As New MemoryStream
Dim read As Double
Do
If (imgstream.DataAvailable) Then
read = 0
ms.Seek(0, SeekOrigin.Begin)
While imgclient.Available
Dim buffer(imgclient.Available - 1) As Byte
imgstream.Read(buffer, 0, buffer.Length)
ms.Write(buffer, 0, buffer.Length)
read += buffer.Length
End While
Me.Text = "Frame bytes read: " & read
PictureBox1.Image = Image.FromStream(ms)
ms.Flush()
End If
Thread.Sleep(34) 'about 30 FPS
Loop
End Using
End Using
End Sub
And this is the server side:
Private Sub screen()
Using imgstream As NetworkStream = imgclient.GetStream()
Using ms As New MemoryStream
Do
Thread.Sleep(34) 'about 30 FPS
ms.Seek(0, SeekOrigin.Begin)
Using img = ScreenCap()
img.Save(ms, Imaging.ImageFormat.Jpeg)
End Using
ms.WriteTo(imgstream)
Loop
End Using
End Using
End Sub
Public Function ScreenCap() As Image
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) ', Imaging.PixelFormat.Format16bppRgb555)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
g.Dispose()
Return screenGrab
End Function
The main problem is when I call the "Image.FromStream(ms)" function, it sometimes works and others doesn't depending on how many milliseconds I set the thread to wait. Tested on 2 different computers in my LAN, on around 1 second it seems OK but always with a high CPU-Network usage. If I set, as the example says around 34 milliseconds to get all more "LIVE", that function throw an exception because of the MemoryStream. How can I speed it up? Is there any smarter way I'm missing right now? I've also tried putting a delimiter byte (like a char = "*") at the and of the MemoryStream and then send it to the client who read one byte at a time until it found a char equal to the delimiter. But it turned out to be a bad solution because a single byte of the image could represent the delimiter if converted to char. Another question I have is: How can I change the image quality and the color depth? Is it a good approach using what the comment says: "Imaging.PixelFormat.Format16bppRgb555"
Thank you!

Extract Images with text from PDF and Edit it using iTextSharp

I am trying to do following things in Windows Forms
1) Read a PDF in Windows Forms
2) Get the Images with Text in it
3) Color / fill the Image
4) save everything to a new file
I have tried Problem with PdfTextExtractor in itext!
But It didn't help.
Here is the code I've tried:
Public Shared Sub ExtractImagesFromPDF(sourcePdf As String, outputPath As String)
'NOTE: This will only get the first image it finds per page.'
Dim pdf As New PdfReader(sourcePdf)
Dim raf As RandomAccessFileOrArray = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
Try
For pageNumber As Integer = 1 To pdf.NumberOfPages
Dim pg As PdfDictionary = pdf.GetPageN(pageNumber)
' recursively search pages, forms and groups for images.'
Dim obj As PdfObject = FindImageInPDFDictionary(pg)
If obj IsNot Nothing Then
Dim XrefIndex As Integer = Convert.ToInt32(DirectCast(obj, PRIndirectReference).Number.ToString(System.Globalization.CultureInfo.InvariantCulture))
Dim pdfObj As PdfObject = pdf.GetPdfObject(XrefIndex)
Dim pdfStrem As PdfStream = DirectCast(pdfObj, PdfStream)
Dim bytes As Byte() = PdfReader.GetStreamBytesRaw(DirectCast(pdfStrem, PRStream))
If (bytes IsNot Nothing) Then
Using memStream As New System.IO.MemoryStream(bytes)
memStream.Position = 0
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(memStream)
' must save the file while stream is open.'
If Not Directory.Exists(outputPath) Then
Directory.CreateDirectory(outputPath)
End If
Dim path__1 As String = Path.Combine(outputPath, [String].Format("{0}.jpg", pageNumber))
Dim parms As New System.Drawing.Imaging.EncoderParameters(1)
parms.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0)
'Dim jpegEncoder As System.Drawing.Imaging.ImageCodecInfo = iTextSharp.text.Utilities.GetImageEncoder("JPEG")'
img.Save(path__1) 'jpegEncoder, parms'
End Using
End If
End If
Next
Catch
Throw
Finally
pdf.Close()
raf.Close()
End Try
End Sub
Now, the actual purpose of this is to get something like this
If this is the actual PDF, I will have to check if there any any items in that bin(by Text in that box)
If there are items then I have to color it like below
Can someone help me with this
The PDF can be retrieved here.

No working append byte array to existing file using visual Basic

I have two strings, each string is a PDF etiquette, I must to write this two etiquette into a PDF file. To do this I convert a each string to a byte array (I don't know if this is the best way) and each I Write into a PDF file. When I write one etiquette into PDF file all is good, I see the etiquette, but then I try to appending the second, the result is the same, into the file is only the first etiquette. For example this code write first etiquette and all working good:
Dim fs As FileStream = New FileStream(fullFileName, FileMode.CreateNew)
fs.Close()
fs = New FileStream(fullFileName, FileMode.Append)
Dim str As String = GetPDFString(27)
Dim binaryData As Byte() = ConvertStringToByte(str)
fs.Write(binaryData, 0, binaryData.Length)
fs.Close()
but if I want to append the second etiquette in the same PDF file using this code ... this not appending.
Dim fs As FileStream = New FileStream(fullFileName, FileMode.CreateNew)
fs.Close()
fs = New FileStream(fullFileName, FileMode.Append)
Dim str As String = GetPDFString(25)
Dim str1 As String = GetPDFString(27)
Dim binaryData As Byte() = ConvertStringToByte(str)
Dim binaryData1 As Byte() = ConvertStringToByte(str1)
fs.Write(binaryData, 0, binaryData.Length)
fs.Write(binaryData1, 0, binaryData1.Length)
fs.Close()
both have the same result, and I don't understand why the second etiquette isn't appending? Thank you a lot.
Your question title suggests that you are asking about how to append a byte to a FileStream, not about PDF, and not about Base64 string conversion (which you are using in your code).
Before asking a question on StackOverflow, you need to ensure you are conveying only one problem at a time. Remove everything that is not relevant, and prepare a code sample we can use in a brand new VS project, in order to reproduce your problem and help you solve it.
Now, if your question is really about appending a byte (or a byte array) to a file, it's as simple as one line of code (or two, if you keep FileStream approach). See below link:
C# Append byte array to existing file
Also copy-pasted for your convenience here (and converted from C# to VB.NET):
Dim appendMe As Byte() = New Byte(999) {}
File.AppendAllBytes("C:\test.exe", appendMe)
Or, to avoid memory overflow, if your byte array is expected to be large enough:
Public Shared Sub AppendAllBytes(path As String, bytes As Byte())
'argument-checking here.
Using stream = New FileStream(path, FileMode.Append)
stream.Write(bytes, 0, bytes.Length)
End Using
End Sub
With this line:
fs.Write(binaryData1, binaryData.Length + 1, binaryData1.Length)
Specifically the second argument (binaryData.Length + 1), you're telling it to start appending from the wrong position of binaryData1. If it's 3 bytes long, for example, and so is binaryData, it won't append anything. It should be similar to the first .Write line:
fs.Write(binaryData1, 0, binaryData1.Length)
So it appends all of binaryData1. It will still append it after binaryData - you don't need to specify the length of the preceeding binaryData in this line.
Alternatively, bypassing the above entirely, concatenate your two strings before encoding/writing them to the file:
Dim fs As FileStream = New FileStream(fullFileName, FileMode.CreateNew)
fs.Close()
fs = New FileStream(fullFileName, FileMode.Append)
Dim str As String = GetPDFString(id, token, depot, 25)
Dim str1 As String = GetPDFString(id, token, depot, 27)
Dim binaryData As Byte() = Convert.FromBase64String(str & str1) 'concatenate strings
fs.Write(binaryData, 0, binaryData.Length)
fs.Close()