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 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); }