-3

I'm using iTextsharp library for generating pdf and merging pdf with document. But could not write barcode in pdf. I'm getting an error

access to the path is denied

But i've checked permission for the folder in server, all permission is given including write permission, this same code works perfectly previously, but not recently. But while checking in locally its working perfectly. Is this because using Itextsharp. Help me out guys.

code for Generating Barcode

iTextSharp.text.pdf.BarcodePDF417 pdf417 = new iTextSharp.text.pdf.BarcodePDF417();
        pdf417.CodeRows = 60;
        pdf417.CodeColumns = 60;
        pdf417.YHeight = 1;

        pdf417.SetText(tmpSourceString);
        System.Drawing.Image im = pdf417.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);

        MemoryStream ms = new MemoryStream();
        im.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();

         System.Drawing.Bitmap newbitmap = new System.Drawing.Bitmap(returnImage);

            newbitmap.Save(fileUploadLoc + file_name, ImageFormat.Jpeg);
            file_name_new = officer + "_" + "digital_barcode1.Jpeg";
            var bmp = Bitmap.FromFile(fileUploadLoc + file_name);
            var newImage = new Bitmap(bmp.Width, bmp.Height + 15);

            var gr = Graphics.FromImage(newImage);
            gr.DrawImageUnscaled(bmp, 0, 0);
            gr.DrawString(signed, SystemFonts.CaptionFont, Brushes.White,
                new RectangleF(0, bmp.Height, bmp.Width, 0));

Code for merging barcode with pdf

using (Stream inputPdfStream = new FileStream(file_path + file_name_doc, FileMode.Open, FileAccess.Read, FileShare.Read))
                                      using (Stream inputImageStream = new FileStream(fileUploadLoc + file_name_new, FileMode.Open, FileAccess.Read, FileShare.Read))
                                      using (Stream outputPdfStream = new FileStream(fileUploadLoc + file_temp, FileMode.Append, FileAccess.Write, FileShare.None))
                                      {
                                          var reader = new PdfReader(inputPdfStream);
                                          PdfReader.unethicalreading = true;
                                          var stamper = new PdfStamper(reader, outputPdfStream);

                                          int numberOfPages = reader.NumberOfPages;
                                         
                                          iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
                                          int pages = get_pageCcount(file_path + file_name_doc);


                                          
                                          for (int i = 1; i <= pages; i++)
                                          {

                                              var pdfContentByte = stamper.GetOverContent(i);
                                              image.SetAbsolutePosition(1, 0);

                                              pdfContentByte.AddImage(image);

                                          }
                                          stamper.Close();
                                          inputImageStream.Close();

                                          File.Delete(file_path + file_name_doc);
                                          File.Move(fileUploadLoc + file_temp, file_path + file_name_doc);
krickx
  • 31
  • 4
  • If using this on a web server, the following may be helpful: https://learn.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.mappath?view=netframework-4.8 and https://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath – Tu deschizi eu inchid Oct 27 '21 at 21:36

1 Answers1

5

Yes, it is called programming. LOTS of programming - basically you need to use the .NET system libraries to generate the graphics for the barcode. Then you manipulate a file to put it in - like your library does now.

At the end, you WRITE a library for this if you do not use one. There is nothing in .NET that does it out of the box. Most would consider it NOT to be worth their time - because it takes a LOT of work for something you can get for a relatively small amount.

Note that I answer the ONE question you ask - for which your code is irrelevant. You ask whether it can be done without a library, and yes.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • Yeah Question I asked and description I entered seems not relevant but thats wat issue im facing , any way thanks – krickx Oct 27 '21 at 17:56
  • Yeah, but you STILL have to write a library to manipulate the PDF, though possibly - depending on use case - in a degenerate edge case (i.e. more a simple templating. – TomTom Oct 27 '21 at 17:57
  • I was going to suggest just [using a font](https://fonts.google.com/specimen/Libre+Barcode+128), which works for oldschool barcode formats like Code39 and Code128, but I just noticed OP tagged this [tag:PDF417], which is a rather more complex encoding than traditional barcodes. There's no easy option for that. The [specification document](https://www.iso.org/standard/65502.html) is 200 francs... looks like a big job. – J... Oct 27 '21 at 17:59
  • Yeah, PDF417 takes the whole font idea out and garbages it. For anyone now aware - PDF417 is multiple lines stacked. Back to PDF generation. – TomTom Oct 27 '21 at 18:02