I'm using PDFsharp library to transform a control into a PDF file. The problem is that the bitmap image, even if I set height and width and the format is A4, does not fill the view in Adobe Reader.
I also tried to set the parameters manually from DPI but it does not work. The script is:
Dim docum As New PdfDocument()
For i As Integer = 0 To FoglioFattura1.listaPagIndex.Count - 1
Dim memImage = New Bitmap(FoglioFattura1.Width, FoglioFattura1.Height) 'A4Size
FoglioFattura1.DrawToBitmap(memImage, New Rectangle(0, 0, FoglioFattura1.Width, FoglioFattura1.Height))
Dim oPage As New PdfPage()
oPage.Orientation = PageOrientation.Portrait
oPage.Size = PageSize.A4
'oPage.TrimMargins.Left = 30
'oPage.TrimMargins.Top = 50
docum.Pages.Add(oPage)
Dim xgr As XGraphics = XGraphics.FromPdfPage(oPage)
Dim img As XImage = XImage.FromGdiPlusImage(memImage)
xgr.DrawImage(img, 0, 0)
Next
These are the dimensions of the document, I have hidden the content for privacy reasons. I want the document fill the view.
The problem is that you call DrawImage without specifying the destination size of the image in the PDF. Add Width and Height to the DrawImage call and the image will be drawn in the size you specify.
PDFs have no DPI. The size if the PDF page is given in Points - and Points are not pixels.
See also:
https://en.wikipedia.org/wiki/Point_(typography)
Related
I have added an image to my iTextSharp PDF document like this:
Public Sub CreatePDFFromBitmap(ByVal uPath As String, ByVal uBitmap As Bitmap)
Dim nFs As System.IO.FileStream = New FileStream(uPath, FileMode.Create)
Dim nDocument As iTextSharp.text.Document
Dim nWriter As iTextSharp.text.pdf.PdfWriter
Dim nCb As iTextSharp.text.pdf.PdfContentByte
Dim nImgFromBitmap As System.Drawing.Image = DirectCast(uBitmap, System.Drawing.Image)
Dim nImg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(nImgFromBitmap, Imaging.ImageFormat.Png)
Dim bLandscape As Boolean = (nImg.Width > nImg.Height)
'rotation needs to be set before document is being opened
If bLandscape Then
nDocument = New iTextSharp.text.Document(PageSize.A4.Rotate, 0, 0, 0, 0)
Else
nDocument = New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
End If
'if an exception is raised here, the following will help: https://stackoverflow.com/questions/15833285/pdfwriter-getinstance-throws-system-nullreferenceexception
nWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(nDocument, nFs)
nDocument.Open()
nCb = nWriter.DirectContent
nImg.ScaleToFit(nDocument.PageSize.Width, nDocument.PageSize.Height) 'raises dpi size :-)))
'X-Y-Koordinatensystem 0,0 startet also unten links, nicht oben-links
nImg.SetAbsolutePosition(0, nDocument.PageSize.Height - nImg.ScaledHeight)
nCb.AddImage(nImg)
nDocument.Close()
nWriter.Close()
nFs.Close()
End Sub
It works fine.
However, when I click the image in the PDF, it gets selected.
This is not what I want.
If I click the image in the PDF, it should not be selected.
This is what it looks like: The image becomes blue:
I want to add editable fields to the PDF, so I need to make the image not selectable, else it would confuse the user.
As Abdel-Rahman Al-Qawasmi mentions in his answer, it is completely up to the PDF viewer which entities it makes selectable and which not. Thus, there is no guaranteed way to get what you want.
Nonetheless, there are ways to put an image into a PDF which dissuade current versions of most PDF viewers from making it selectable. These ways either transform the bitmap image into a non-bitmap entity (e.g. by iterating over the pixels of the bitmap and drawing a little rectangle per pixel using vector graphics) or wrap the bitmap image into something that usually is not selectable.
Let's take the latter approach and wrap the image into a page-size PDF pattern with which we then fill the actual page. You can do that by replacing your
nCb.AddImage(nImg)
by
Dim painter As iTextSharp.text.pdf.PdfPatternPainter = nCb.CreatePattern(nDocument.PageSize.Width, nDocument.PageSize.Height)
painter.AddImage(nImg)
nCb.SetColorFill(New iTextSharp.text.pdf.PatternColor(painter))
nCb.Rectangle(0, 0, nDocument.PageSize.Width, nDocument.PageSize.Height)
nCb.Fill()
(This essentially is the VB/iTextSharp pendant of the Java/iText code from this answer.)
This is a pdf program specifications and not related to asp.net or vb.net programming. you need to have control of the pdf reader settings. Or try to use another format.
I have a panel which draws a diagram based on user input. Unfortunately the diagrams can get really really big, and when I print them it doesn't entirely fit on a single page without losing readability.
I'm using PDFsharp within VB.net to create the PDF. The current method is the turn the panel in which the diagram is drawn on to a bitmap image, and then place the image in the pdf document.
1 diagram can easily make the panel 1500,3000 in size.
Example of Diagram
Example of Diagram in PDF
Any help is greatly appreciated
Dim pdfdoc As New PdfDocument
Dim page As PdfPage = pdfdoc.AddPage
Dim Bitmap As Bitmap = New Bitmap(pnl_Draw.Width, pnl_Draw.Height)
Dim BXImage As XImage
Dim GFX As XGraphics
Me.pnl_Draw.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Height))
Dim pbx As New PictureBox
pbx.Image = Bitmap
BXImage = XImage.FromGdiPlusImage(pbx.Image)
GFX = XGraphics.FromPdfPage(page)
GFX.ScaleTransform(0.82)
GFX.DrawImage(BXImage, 0, 0)
GFX.Dispose()
pdfdoc.Save("G:\test.pdf")
pdfdoc.Close()
Try
Process.Start("G:\test.pdf")
Catch ex As Exception
End Try
Simple solution: in the Adobe Reader Print dialog click on "Poster" to print the image on more than 1 page. Increase the Zoom factor to distribute the poster on as many pages as you need.
You can also increase the page size proportionally to the bitmap size.
BTW: You don't need GFX.ScaleTransform(0.82) if you specify the destination size in the call to GFX.DrawImage.
I used the following code in a VB.Net forms application:
Dim btnShowHideSize As New Size(30, 30)
btnShowHide.Size = btnShowHideSize
Dim img As New Bitmap(Resources.change2)
Dim img2 As New Bitmap(img, btnShowHideSize)
btnShowHide.BackgroundImage = img2
btnShowHide.BackgroundImageLayout = ImageLayout.Stretch
The image will expand, but not shrink. When I step through, I see that img size is 80,80 and img2 size is 30,30. Howewver, the image is not shrinking - it is getting cut off. If I go above 80,80 e.g. 200,200, the image does expand.
Is shrinking an image not possible?
I am facing a strange issue while dealing with big images ( size approx 48 mb, a Tiff file having 175 pages.). Now when I am trying to convert the Imagefile into the bitmap and doing some operation, randomly system will throw "Out of memory exception"
Below is my code snippet. I am getting error (randomly) on line " pages = New Bitmap(lorigionalFile) " where LoriginalFile is of ImageType and Page is of Bitmap.
So Please guide me what to do to remove this type of error or stoping the memory leakage.
Dim lorigionalFile As System.Drawing.Image
Dim SaveEncodeParam As EncoderParameter 'Encoder parameter to create multi page image
Dim EncoderParams As EncoderParameters = New EncoderParameters(1) 'Encoder parameter Array
Dim pages As Bitmap 'Used to save image page
Dim NextPage As Bitmap 'Used to save next image page
Dim PageNumber As Integer
Lfr = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite)
lorigionalFile = System.Drawing.Image.FromStream(Lfr)
''Get number of page count in image
PageNumber = getPageNumber(lorigionalFile)
'set first page as active frame
cintPagenumber = PageNumber
'loop to every page of attached document
For i As Integer = 0 To PageNumber - 1
'set active from as per loop variable
lorigionalFile.SelectActiveFrame(FrameDimension.Page, i)
'Will get errow in below line(randomly), when there are other programs running in background
pages = New Bitmap(lorigionalFile)
'image store in Image
CalImageContainer.Add(pages)
'make a copy on image container
CalImageContainerCopy.Add(pages)
Next
Lfr.Close()
End Try
Thanks
Pratik vohera
I don't know VB, but it looks like you create a new bitmap memory in your for-loop above without freeing the memory in between.
Use Imagemagick instead to extract images from tif and then operate on each of those instead.
Just a thought.
I have created a PNG image that is 200 DPI, and perfectly sized for a landscape A4 page size. I needed to convert this to a PDF document, so I've used the iTextSharp library with the code below.
This all works, however the image quality has degraded. Any suggestions as to how I might improve this?
Public Sub ConvertPNGtoPDF(ByVal inputFile As String, ByVal outputFile As String)
Using fs As New FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
Dim document As New Document(PageSize.A4.Rotate, 0, 0, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
document.Open()
Dim cb As PdfContentByte = writer.DirectContent
Using bm As New Bitmap(inputFile)
Dim total As Integer = bm.GetFrameCount(FrameDimension.Page)
For k As Integer = 0 To total - 1
bm.SelectActiveFrame(FrameDimension.Page, k)
Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bm, Nothing, False)
img.SetDpi(200, 200)
img.ScalePercent(72.0F / 200.0F * 100)
img.SetAbsolutePosition(0, 0)
cb.AddImage(img)
document.NewPage()
Next
End Using
document.Close()
writer.Close()
End Using
End Sub
This all works, however the image quality has degraded. Any suggestions as to how I might improve this?
Looking at the code in PngImage, it looks like iText doesn't support PNG compression as a PDF-native filter, so it has to be decompressed and recompressed as Something Else. Is this because the PDF spec doesn't support it:
Just checked, sure looks that way.
Best fix? JPEG and JPEG2000 are supported as "native" compression types within PDF (and this is echoed in iText[sharp]). So use JPEG[2k] instead, and run your images through your image conversion library of choice if need be.