0

I have a function that converts a ZPL(Zebra Label) into a PDF format and saves the file. What I'm trying to do is instead of overwriting the file each time, I would like to append the filestream to the file (if it exists), write new (if not exists).

I've tried setting a new filestream with filemode.append, that did not seem to make a difference.

private static void SaveLabel(string label, string labelDir, string caseNumber)
    {
        var zpl = Encoding.UTF8.GetBytes(label);
        var fileName = $@"{labelDir}\{caseNumber}.pdf";

        // adjust print density (8dpm), label width (4 inches), label height (6 inches), and label index (0) as necessary
        var request = (HttpWebRequest)WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
        request.Method = "POST";
        request.Accept = "application/pdf"; // omit this line to get PNG images back
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = zpl.Length;

        var requestStream = request.GetRequestStream();
        requestStream.Write(zpl, 0, zpl.Length);
        requestStream.Close();

        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            var responseStream = response.GetResponseStream();
            if (!File.Exists(fileName))
                File.Create(fileName);

            using (var fileStream = File.Open(fileName, FileMode.Append))
            {
                responseStream?.CopyTo(fileStream);
                responseStream?.Close();
                fileStream.Close();
            }
        }
        catch (WebException e)
        {
            Console.WriteLine(@"Error: {0}", e.Status);
        }
    }
Faure Ugo
  • 69
  • 2
  • 12
Drew Jackson
  • 9
  • 1
  • 13

1 Answers1

-1

I first check to see if the file existed (meaning there was going to be more than one label for the shipment). If not, process as normal. If yes, then read that file into a new pdf file. Then read the contents of the current response stream into a 2nd new pdf file.

I then delete the destination file freeing the name for the new combined pdf. I then use the suggested link to PDFSharp and combine the pages and save as the original file name. This will enable a continuous appending of the file regardless of how many package labels are generated.

private static void SaveLabel(string label, string labelDir, string caseNumber)
    {
        var zpl = Encoding.UTF8.GetBytes(label);
        var destFileName = $@"{labelDir}\{caseNumber}.pdf";

        // adjust print density (8dpm), label width (4 inches), label height (6 inches), and label index (0) as necessary
        var request = (HttpWebRequest)WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
        request.Method = "POST";
        request.Accept = "application/pdf"; // omit this line to get PNG images back
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = zpl.Length;

        var requestStream = request.GetRequestStream();
        requestStream.Write(zpl, 0, zpl.Length);
        requestStream.Close();

        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            var responseStream = response.GetResponseStream();

            if (File.Exists(destFileName))
            {
                var oldStream = File.OpenRead(destFileName);
                var oldFileName = $@"{labelDir}\{caseNumber}-1.pdf";
                using (var fileStream = File.Open(oldFileName, FileMode.Create))
                {
                    oldStream.CopyTo(fileStream);
                    oldStream.Close();
                    fileStream.Close();
                }

                var newFileName = $@"{labelDir}\{caseNumber}-2.pdf";
                using (var fileStream = File.Open(newFileName, FileMode.Create))
                {
                    responseStream?.CopyTo(fileStream);
                    responseStream?.Close();
                    fileStream.Close();
                }

                File.Delete(destFileName);

                using (var pdfOne = PdfReader.Open(oldFileName, PdfDocumentOpenMode.Import))
                {
                    using (var pdfTwo = PdfReader.Open(newFileName, PdfDocumentOpenMode.Import))
                    {
                        using (var outPdf = new PdfDocument())
                        {
                            CopyPages(pdfOne, outPdf);
                            CopyPages(pdfTwo, outPdf);

                            outPdf.Save(destFileName);
                        }
                    }
                }

                File.Delete(oldFileName);
                File.Delete(newFileName);
            }
            else
            {
                using (var fileStream = File.Open(destFileName, FileMode.Create))
                {
                    responseStream?.CopyTo(fileStream);
                    responseStream?.Close();
                    fileStream.Close();
                }
            }
        }
        catch (WebException e)
        {
            Console.WriteLine(@"Error: {0}", e.Status);
        }
    }
Drew Jackson
  • 9
  • 1
  • 13
  • Can you add a description of what you changed and why it solves the issue? –  Dec 19 '18 at 22:08
  • I first check to see if the file existed (meaning there was going to be more than one label for the shipment). If not, process as normal. If yes, then read that file into a new pdf file. Then read the contents of the current response stream into a 2nd new pdf file. I then delete the destination file freeing the name for the new combined pdf. I then use the suggested link to PDFSharp and combine the pages and save as the original file name. This will enable a continuous appending of the file regardless of how many package labels are generated. – Drew Jackson Dec 19 '18 at 22:12
  • Click the "Edit" link under your answer and add it above your code. –  Dec 19 '18 at 22:17