0

I'm using IText 7 to convert my HTML file into PDF and auto download the file when users click a button.

Currently I'm planning to insert a barcode using IText7 into the PDF file, however I encountered some error. Btw, it works fine without the 'barcode code'.

This is the error : 'iText.Kernel.PdfException: 'Pdf indirect object belongs to other PDF document. Copy object to current pdf document.''

How can I add a barcode at the end of my pdf file?

public MemoryStream GetCovidFormPdfByAccessionNumber(string htmlFile, string accessionNumber)
    {
                var workStream = new MemoryStream();
                using (var pdfWriter = new PdfWriter(workStream))
                {
                    pdfWriter.SetCloseStream(false);
    
                    var pdfDoc = new PdfDocument(pdfWriter);
    
                    using (var document = HtmlConverter.ConvertToDocument(htmlFile, pdfWriter))
                    {
                        document.Add(CreateBarcode(accessionNumber, pdfDoc));
            
                    }
                }
    
                workStream.Position = 0;
    
                return workStream;
    }
    
    private static Image CreateBarcode(string code, PdfDocument pdfDoc)
    {
                
    
    
                Barcode39 barcode = new Barcode39(pdfDoc);
                barcode.SetCode(code);
    
                //Create barcode object to put it to the cell as image
                PdfFormXObject barcodeObject = barcode.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);
                var image = new Image(barcodeObject);
                image.SetWidth(250);
                return image;
    }
sicKo
  • 1,241
  • 1
  • 12
  • 35
  • See following : https://stackoverflow.com/questions/34508058/how-to-insert-values-into-an-existing-pdf-on-the-fly – jdweng Nov 09 '20 at 16:11
  • @jdweng That's not it. I'm trying to insert the IText7 barcode into my Pdf which is created using the Itext7.pdfhtml library – sicKo Nov 09 '20 at 16:20
  • ItextSharp is older version of Itext7. The old code may still work with newer version. – jdweng Nov 09 '20 at 16:23
  • @jdweng that solution is to insert values from user into existing pdf – sicKo Nov 09 '20 at 16:30

0 Answers0