Exemple #1
0
        private byte[] Crop(byte[] pdfbytes, float llx, float lly, float urx, float ury)
        {
            byte[] rslt = null;
            // Allows PdfReader to read a pdf document without the owner's password
            iTextSharp.text.pdf.PdfReader.unethicalreading = true;
            // Reads the PDF document
            using (iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfbytes))
            {
                // Set which part of the source document will be copied.
                // PdfRectangel(bottom-left-x, bottom-left-y, upper-right-x, upper-right-y)
                iTextSharp.text.pdf.PdfRectangle rect = new iTextSharp.text.pdf.PdfRectangle(llx, lly, urx, ury);

                using (MemoryStream ms = new MemoryStream())
                {
                    // Create a new document
                    //using (iTextSharp.text.Document doc =
                    //	new iTextSharp.text.Document(new iTextSharp.text.Rectangle(288f,432f)))
                    using (iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4))
                    {
                        // Make a copy of the document
                        iTextSharp.text.pdf.PdfSmartCopy smartCopy = new iTextSharp.text.pdf.PdfSmartCopy(doc, ms)
                        {
                            PdfVersion = iTextSharp.text.pdf.PdfWriter.VERSION_1_7
                        };
                        smartCopy.CloseStream = false;
                        // Open the newly created document
                        doc.Open();
                        // Loop through all pages of the source document
                        for (int i = 1; i <= pdfReader.NumberOfPages; i++)
                        {
                            doc.NewPage();// net necessary line
                            // Get a page
                            var page = pdfReader.GetPageN(i);
                            // Apply the rectangle filter we created
                            page.Put(iTextSharp.text.pdf.PdfName.CROPBOX, rect);
                            page.Put(iTextSharp.text.pdf.PdfName.MEDIABOX, rect);
                            // Copy the content and insert into the new document
                            var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
                            smartCopy.AddPage(copiedPage);

                            if (i.Equals(pdfReader.NumberOfPages))
                            {
                                doc.NewPage();
                                smartCopy.AddPage(copiedPage);
                            }
                        }
                        smartCopy.FreeReader(pdfReader);
                        smartCopy.Close();
                        ms.Position = 0;
                        rslt        = ms.GetBuffer();
                        // Close the output document
                        doc.Close();
                    }
                }
                return(rslt);
            }
        }
Exemple #2
0
        private void FedEx_Test(string BoxID)
        {
            var            boxList    = db.Box.Where(b => b.IsEnable && b.MainBox.Equals(BoxID)).ToList();
            ShippingMethod method     = db.ShippingMethod.Find(boxList.First().FirstMileMethod);
            DirectLine     directLine = db.DirectLine.Find(boxList.First().DirectLine);
            CarrierAPI     api        = method.Carriers.CarrierAPI;
            FedEx_API      FedEx      = new FedEx_API(api);
            var            result     = FedEx.CreateBox(boxList, method, directLine);

            if (result.HighestSeverity.Equals(FedExShipService.NotificationSeverityType.SUCCESS))
            {
                var    Data           = result.CompletedShipmentDetail.CompletedPackageDetails.First();
                string TrackingNumber = Data.TrackingIds.First().TrackingNumber;
                //System.IO.File.WriteAllBytes(@"G:\Downloads\FedEx_Test3.pdf", Crop(Data.Label.Parts.First().Image, 29f, 324f, 319f, 762f));
                //System.IO.File.WriteAllBytes(@"G:\Downloads\FedEx_Test4.pdf", Data.Label.Parts.First().Image);

                var    zpl     = Data.Label.Parts.First().Image;
                string str     = Encoding.ASCII.GetString(zpl);
                var    request = (HttpWebRequest)WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6.75/");
                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();
                    var fileStream     = System.IO.File.Create(@"G:\Downloads\FedEx_Test5.pdf"); // change file name for PNG images
                    responseStream.CopyTo(fileStream);
                    responseStream.Close();
                    fileStream.Close();
                }
                catch (WebException e)
                {
                    Console.WriteLine("Error: {0}", e.Status);
                }
            }

            iTextSharp.text.pdf.PdfReader.unethicalreading = true;
            // Reads the PDF document
            using (iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(@"G:\Downloads\FedEx_Test5.pdf"))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    // Create a new document
                    //using (iTextSharp.text.Document doc =
                    //	new iTextSharp.text.Document(new iTextSharp.text.Rectangle(288f,432f)))
                    using (iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4))
                    {
                        // Make a copy of the document
                        iTextSharp.text.pdf.PdfSmartCopy smartCopy = new iTextSharp.text.pdf.PdfSmartCopy(doc, ms)
                        {
                            PdfVersion = iTextSharp.text.pdf.PdfWriter.VERSION_1_7
                        };
                        smartCopy.CloseStream = false;
                        // Open the newly created document
                        doc.Open();
                        // Loop through all pages of the source document
                        for (int i = pdfReader.NumberOfPages; i >= 1; i--)
                        {
                            doc.NewPage();// net necessary line
                            // Get a page
                            var page = pdfReader.GetPageN(i);
                            // Copy the content and insert into the new document
                            var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
                            smartCopy.AddPage(copiedPage);

                            if (i.Equals(1))
                            {
                                doc.NewPage();
                                smartCopy.AddPage(copiedPage);
                            }
                        }
                        smartCopy.FreeReader(pdfReader);
                        smartCopy.Close();
                        ms.Position = 0;
                        System.IO.File.WriteAllBytes(@"G:\Downloads\FedEx_Test6.pdf", ms.GetBuffer());
                        // Close the output document
                        doc.Close();
                    }
                }
            }
        }