public static bool CropCockleBriefPages(string src) { // works for both saddle stitch and perfect bind string dest = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(src), "brief cropped.pdf"); try { using (var stream = new System.IO.FileStream(dest, System.IO.FileMode.Create)) { iTextSharp.text.pdf.PdfRectangle croppedRectangle = new iTextSharp.text.pdf.PdfRectangle(377f, 189f, 0f, 792f); iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(src); for (int i = 1; i <= reader.NumberOfPages; i++) { iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(i); pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, croppedRectangle); } var stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream); stamper.Close(); reader.Close(); } System.IO.File.Delete(src); System.IO.File.Move(dest, src); return(true); } catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); return(false); } }
private void SaveImages(string InputPDFFile, int specificPageNumber, int degree) { Directory.CreateDirectory(Constents.ROOTPATH + "\\" + Constents.PDFROTATE_FOLDER); var filename = Path.GetFileName(InputPDFFile); string outfile = Constents.ROOTPATH + "\\" + Constents.PDFROTATE_FOLDER + filename; txtOutputPath.Text = outfile; using (FileStream outStream = new FileStream(outfile, FileMode.Create)) { iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(InputPDFFile); iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream); int noofPages = reader.NumberOfPages; if (specificPageNumber > 0) //Rotae Specific Page { iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(specificPageNumber); int desiredRot = degree; // 90 degrees clockwise from what it is now iTextSharp.text.pdf.PdfNumber rotation = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE); if (rotation != null) { desiredRot += rotation.IntValue; desiredRot %= 360; // must be 0, 90, 180, or 270 } pageDict.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(desiredRot)); } else // Rotate All Pages { for (int i = 1; i <= noofPages; i++) { iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(i); int desiredRot = degree; // 90 degrees clockwise from what it is now iTextSharp.text.pdf.PdfNumber rotation = pageDict.GetAsNumber(iTextSharp.text.pdf.PdfName.ROTATE); if (rotation != null) { desiredRot += rotation.IntValue; desiredRot %= 360; // must be 0, 90, 180, or 270 } pageDict.Put(iTextSharp.text.pdf.PdfName.ROTATE, new iTextSharp.text.pdf.PdfNumber(desiredRot)); } } stamper.Close(); } }
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); } }
public static bool CropCoverAndInsideCover(string src, int?coverLength) { // works for both saddle stitch and perfect bind string dest = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(src), "cover cropped.pdf"); try { using (var stream = new System.IO.FileStream(dest, System.IO.FileMode.Create)) { iTextSharp.text.pdf.PdfRectangle croppedRectangleCover; switch (coverLength) { case 48: default: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 130f, 21f, 741f); break; case 49: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 120f, 21f, 741f); break; case 50: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 108f, 21f, 741f); break; case 51: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 92f, 21f, 741f); break; } iTextSharp.text.pdf.PdfRectangle croppedRectangleInsideCover = new iTextSharp.text.pdf.PdfRectangle(377f, 189f, 0f, 792f); iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(src); for (int i = 1; i <= reader.NumberOfPages; i++) { iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(i); if (i == 1) { pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, croppedRectangleCover); } else { pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, croppedRectangleInsideCover); } } var stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream); stamper.Close(); reader.Close(); } System.IO.File.Delete(src); System.IO.File.Move(dest, src); return(true); } catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); return(false); } }
private System.IO.MemoryStream cropTypesetCover(System.IO.MemoryStream orig_stream) { { // works for both saddle stitch and perfect bind System.IO.MemoryStream dest_stream = null; try { dest_stream = new System.IO.MemoryStream(); iTextSharp.text.pdf.PdfRectangle croppedRectangleCover; switch (LengthOfCover) { case 48: default: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 130f, 21f, 741f); break; case 49: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 120f, 21f, 741f); break; case 50: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 108f, 21f, 741f); break; case 51: croppedRectangleCover = new iTextSharp.text.pdf.PdfRectangle(356f, 92f, 21f, 741f); break; } var reader = new iTextSharp.text.pdf.PdfReader(orig_stream.ToArray()); var pageDict = reader.GetPageN(1); pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, croppedRectangleCover); var stamper = new iTextSharp.text.pdf.PdfStamper(reader, dest_stream); stamper.Close(); reader.Close(); } catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); } return(dest_stream); } }
private System.IO.MemoryStream cropTypesetBrief(System.IO.MemoryStream orig_stream) { System.IO.MemoryStream dest_stream = null; try { dest_stream = new System.IO.MemoryStream(); var croppedRectangle = new iTextSharp.text.pdf.PdfRectangle(377f, 189f, 0f, 792f); var reader = new iTextSharp.text.pdf.PdfReader(orig_stream.ToArray()); for (int i = 1; i <= reader.NumberOfPages; i++) { iTextSharp.text.pdf.PdfDictionary pageDict = reader.GetPageN(i); pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, croppedRectangle); } var stamper = new iTextSharp.text.pdf.PdfStamper(reader, dest_stream); stamper.Close(); reader.Close(); } catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); } return(dest_stream); }
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(); } } } }