0

I want to know if is possible to duplicate/copy the same image in the same PDF with the libraries iTextSharp.

I already have the image to duplicate and generate its not a problem but when I copy it (with a for cicle) the image doesn't copy but only moves to the new position.

Thanks a lot, Giovanni

Dim documento As New Document(PageSize.A4, MillimetersToPoints(10), MillimetersToPoints(10), MillimetersToPoints(10), MillimetersToPoints(10))
Dim filestream As New FileStream("C:\test.pdf", FileMode.Create, FileAccess.Write, FileShare.None)
Dim Scrittura As PdfWriter = PdfWriter.GetInstance(documento, FileStream)
Dim baseFont As BaseFont = baseFont.CreateFont(baseFont.HELVETICA_BOLD, baseFont.CP1252, baseFont.EMBEDDED)

Dim img As iTextSharp.text.Image
Dim DataEncoder As DataMatrix.net.DmtxImageEncoder
Dim DataEncodeOption As DataMatrix.net.DmtxImageEncoderOptions
Dim DataImg As System.Drawing.Image
Dim Databitmap As Bitmap
Dim BarcodeImage As Bitmap

DataEncoder = New DataMatrix.net.DmtxImageEncoder
DataEncodeOption = New DataMatrix.net.DmtxImageEncoderOptions
DataEncodeOption.SizeIdx = DataMatrix.net.DmtxSymbolSize.DmtxSymbol18x18
DataEncodeOption.Scheme = DataMatrix.net.DmtxScheme.DmtxSchemeAscii
DataEncodeOption.ModuleSize = 1
DataEncodeOption.MarginSize = 1
DataImg = DataEncoder.EncodeImage(My.Forms.AvvioProgramma.MyInfo.Progressivo, DataEncodeOption)
Databitmap = New Bitmap(DataImg)
BarcodeImage = Databitmap
BarcodeImage.Save("C:\prova.jpg")
img = Image.GetInstance("C:\prova.jpg")

documento.Open()
Dim contentByte As PdfContentByte = Scrittura.DirectContent
 contentByte.BeginText()
    contentByte.SetFontAndSize(baseFont, 12)
    contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Tubotec", posXTitolo, posYTitolo, 0)
    img.SetAbsolutePosition(posXDM, posYDM)
    contentByte.AddImage(img)
    contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, My.Forms.AvvioProgramma.MyInfo.Progressivo, posXPar, posYPar, 0)
    contentByte.EndText()
documento.Close()
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
Bill712
  • 1
  • 2
  • ok, added the code – Bill712 Dec 11 '17 at 12:24
  • What is `posXDM` and `posYDM`? And why are you adding content with `PdfContentByte`? You don't expect the image to be between the two lines of text, do you? (If so, your code is wrong.) What do you mean when you write *the image doesn't copy but only moves to the new position*? Because I see you adding the image only once. And why are you not using the latest iText version? Your question is too unclear to answer. – Bruno Lowagie Dec 11 '17 at 13:49
  • posXDM and posYDM are the position of the lines in the pages. yes i will expect the image between the two lines. I put a link to an image example: [link](https://drive.google.com/file/d/1N7ZbQFBQlcrN7SFhB8xrwhc8TpRwVopA/view?usp=sharing). all the code is inside a for cicle, if you look at the image you can see that the DataMatrix is only in the third "label" but i want it in all the label i generate. thanks – Bill712 Dec 11 '17 at 13:58
  • In your code, you only add one image, so why are you surprised that only one image is added? Your question doesn't make sense. Why aren't you adding the image three times if you want to see it three times? And also: why are you doing this the hard way with `PdfContentByte`? And why are you still using an old iText version? I could be lazy, and just apply the company policy: there is no longer voluntary support for all iText versions prior to iText 7; if you want support for an older version a support contract with iText Software is required. – Bruno Lowagie Dec 11 '17 at 14:54
  • I use the older version simply because it's free to download. However I say that all the code above is all inside a FOR loop so (according to my experience with iText) I think that for every cicle and changing the value of .SetAbsolutePosition the image was copy and move to the new position but this doesn't work. if you can help me what method can I use instead of PdfContentByte? can you give me some help? thank you. I don't want to insert three/four/five... images because the images are 65 and not 3, the image on the link above is only an example – Bill712 Dec 11 '17 at 15:52
  • You write: "It's free to *download*" but so is iText 7 (iText 5 and iText 7 have the same open source license, the AGPL). That doesn't mean the old version is free to *use*. The version you use can only be distributed in the context of an open source project. For instance: if you are offering a SaaS application, then you have to distribute *all of your own* source code to all the users of your application. You aren't creating any goodwill with the sole person who is looking to help you by telling things that are blatantly untrue! – Bruno Lowagie Dec 11 '17 at 17:03
  • As for an alternative to using `PdfContentByte`, why don't you just use a table? See [How to display barcodes in a matrix-like structure? ](https://stackoverflow.com/questions/21981602) and [Adding more text to a cell in table in itext](https://stackoverflow.com/questions/25325408). (And stop telling me non sense! There is no reason why you shouldn't upgrade!) – Bruno Lowagie Dec 11 '17 at 17:03

0 Answers0