PDFPage CreateNewPage(PDFFont font, string serialNumber) { var headerPen = new PDFPen(Color.DarkSlateBlue, 120); var serialfont = new PDFFont(FontType.Helvetica, 16, PDF.Drawing.FontStyle.Regular); var companyNameFont = new PDFFont(FontType.Helvetica, 30, PDF.Drawing.FontStyle.Bold); pageNumber++; PDFPage page = pdfDocument.AddPage(); page.Height = 1000; page.Width = 700; page.DrawLine(headerPen, 0, 0, 700, 0); page.DrawString(LabelConstant.Title, serialfont, Color.White, 10, 50); page.DrawString(LabelConstant.SerialNumber + serialNumber, serialfont, Color.White, 550, 50); page.DrawString(LabelConstant.CompanyName, companyNameFont, Color.White, 230, 40); page.DrawString(LabelConstant.TradeMark, font, Color.White, 450, 20); page.DrawString(LabelConstant.Page + pageNumber, font, Color.Black, 600, 980); page.DrawString(LabelConstant.Website, font, Color.Black, PDFcoordinates.siteX, PDFcoordinates.siteY); page.DrawString(DateTime.UtcNow.ToString("dd/MM/yyy HH:mm:sss UTC"), font, Color.White, PDFcoordinates.dateX, PDFcoordinates.dateY); page.DrawString("0.1.9.1", font, Color.Black, PDFcoordinates.versionX, PDFcoordinates.versionY); return(page); }
/** * Encrypts the PDF using AES block by block * */ public void GetEncryptedPDF() { using (RijndaelManaged aesEncryption = new RijndaelManaged()) { aesEncryption.KeySize = _keySize; aesEncryption.BlockSize = _blockSize; aesEncryption.Mode = CipherMode.ECB; aesEncryption.Padding = PaddingMode.PKCS7; aesEncryption.Key = _keyIv.Key; //aesEncryption.Key = ASCIIEncoding.UTF8.GetBytes("BTikvHBatPdAtgT3317QIQqGFY25WpIz"); aesEncryption.IV = _keyIv.IV; foreach (KeyValuePair <Object, Object> entry in _words) { _encryptedPDF.AddPage(); if (entry.Key.GetType() != typeof(string)) { List <Blocks> Words = (List <Blocks>)entry.Value; int pageIndex = (int)entry.Key; for (int i = 0; i < Words.Count; i++) { Blocks word = Words[i]; byte[] plainText = Encoding.ASCII.GetBytes(word.Block); ICryptoTransform crypto = aesEncryption.CreateEncryptor(); byte[] EncryptedText = crypto.TransformFinalBlock(plainText, 0, plainText.Length); string key = Encoding.ASCII.GetString(aesEncryption.Key); string CipherText = Convert.ToBase64String(EncryptedText); string c = Encoding.ASCII.GetString(EncryptedText); string cAn = ToBinary(ConvertToByteArray(word.Block, Encoding.ASCII)); string keyAnci = ToBinary(ConvertToByteArray(key, Encoding.ASCII)); if (word.Chunks.Count == 0) { _encryptedPDF.Pages[0].Canvas.DrawText(CipherText, word.FontBase, word.FontBrush, word.Left[0], word.Top[0]); } else { for (int j = 0; j <= word.Chunks.Count; j++) { string text = GetString(CipherText, j, word.Chunks); _encryptedPDF.Pages[0].Canvas.DrawText(text, word.FontBase, word.FontBrush, word.Left[j], word.Top[j]); } } } } } } string encryptedFilePath = Path.GetFullPath("C:/Meet/STUDY/Cryptography/securePDFmerging/securePDFEncrytion/bin/Release/UploadedFiles/Encrypted_letter.pdf"); _encryptedPDF.Save(encryptedFilePath); }
public static List <PDFDocument> getPdf(PDFDocument pdf, String xyz) { Share[] textShares; System.Drawing.Bitmap[] imageShares = new System.Drawing.Bitmap[5]; //PdfFont font = secretPdf.AddFont("Arial"); System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); //timer.Start(); ////lOOP THROUGH EACH PAGE IN THE SECRET PDF //for (int i = 0; i < secretPdf.PageCount; i++ ) //{ // //Text Extraction // for (int j = 0; j < secretPdf.Pages[i].Canvas.GetTextData().Count; j++ ) // { // //int test = secretPdf.Pages[i].Canvas.GetTextData().Count; // PdfTextData secretData = secretPdf.Pages[i].GetWords()[j]; //our pdf text chunk to process // textShares = ShareGenerator.GenerateShares(secretData.Text, n, k); // ////Write the share data to the pdf shares // //for (int l = 0; l < sharePdfs.Count; l++) // //{ // //} // System.Console.WriteLine("Chunk Complete"); // } // //add next page for future processing // for (int l = 0; l < sharePdfs.Count; l++) // sharePdfs[l].AddPage(); // System.Console.WriteLine("Page Complete"); //} //timer.Stop(); //System.Console.WriteLine("\n\nTime Elapsed: " + timer.ElapsedMilliseconds+"\n\n\n"); //timer.Reset(); //secretPdf.Dispose(); PDFDocument pdfDoc = pdf; //new PDFDocument("pdfimage.pdf"); //PDFDocument secondDoc = new PDFDocument("white.pdf"); PDFDocument recon = new PDFDocument(); List <int> usedShares = new List <int>(); usedShares.Add(2); usedShares.Add(3); usedShares.Add(4); List <PDFDocument> pdfShares = new List <PDFDocument>(); for (int i = 0; i < 2; i++) { pdfShares.Add(new PDFDocument()); } PDFImportedPage ip; O2S.Components.PDF4NET.Text.PDFTextRunCollection textRuns; timer.Start(); for (int i = 0; i < pdfDoc.Pages.Count; i++) { ip = (PDFImportedPage)pdfDoc.Pages[i]; textRuns = ip.ExtractTextRuns(); PDFPageObjectCollection objects = ip.ExtractPageObjects(); //add next page for future processing for (int l = 0; l < pdfShares.Count; l++) { pdfShares[l].AddPage(); } recon.AddPage(); for (int j = 0; j < textRuns.Count; j++) { textShares = ShareGenerator.GenerateShares(textRuns[j].Text, 5, 3); PDFFont font = new PDFFont(PDFFontFace.Helvetica, textRuns[j].FontSize); PDFBrush fontBrush = new PDFBrush(textRuns[j].FillColor); //PDFPen pen = new PDFPen(textRuns[j].StrokeColor); string[] shareString = new string[5]; for (int l = 0; l < 2; l++) { shareString[l] = textShares[l].GetCipherText(); pdfShares[l].Pages[i].Canvas.DrawText(shareString[l], font, fontBrush, textRuns[j].DisplayBounds.Left, textRuns[j].DisplayBounds.Top); //pdfShares[l].Pages[i].Canvas.DrawTextBox(textShares[l].GetCipherText(), font, pen, fontBrush, textRuns[j].DisplayBounds.Left, textRuns[j].DisplayBounds.Top, textRuns[j].DisplayBounds.Width, textRuns[j].DisplayBounds.Height); } //System.Console.WriteLine("Chunk Complete"); string reconString = ShareAssembler.TextReconstruction(shareString, usedShares, 5, 3); //System.Console.WriteLine(reconString); recon.Pages[i].Canvas.DrawText(reconString, font, fontBrush, textRuns[j].DisplayBounds.Left, textRuns[j].DisplayBounds.Top); } for (int j = 0; j < objects.Count; j++) { PDFTextPageObject text; PDFImagePageObject image; PDFFont font; PDFBrush fontBrush; if (objects[j] is PDFTextPageObject) { text = (PDFTextPageObject)objects[j]; Console.WriteLine(text.FontName); Console.WriteLine("textpdf"); font = new PDFFont(PDFFontFace.Helvetica, text.FontSize); fontBrush = new PDFBrush(text.FillColor); textShares = ShareGenerator.GenerateShares(text.Text, 2, 2); string[] shareString = new string[2]; for (int l = 0; l < textShares.Length; l++) { shareString[l] = textShares[l].GetCipherText(); pdfShares[l].Pages[i].Canvas.DrawText(shareString[l], font, fontBrush, text.DisplayBounds.Left, text.DisplayBounds.Top); } string reconString = ShareAssembler.TextReconstruction(shareString, usedShares, 5, 3); recon.Pages[i].Canvas.DrawText(reconString, font, fontBrush, text.DisplayBounds.Left, text.DisplayBounds.Top); } else if (objects[j] is PDFImagePageObject) { Console.WriteLine("imagepdf"); image = (PDFImagePageObject)objects[j]; /* * * following line is what needs to be revised until end * * * * * * * */ //imageShares = ShareGenerator.GenerateReducedShares(image.Image, 5, 3, 15); //imageShares = ShareGenerator.GenerateShares(image.Image, 5, 3); ShareGenerator genShares = new ShareGenerator(); imageShares = genShares.GenShares(image.Image, 2, 2); for (int l = 0; l < imageShares.Length; l++) { pdfShares[l].Pages[i].Canvas.DrawImage(imageShares[l], image.DisplayBounds.Left, image.DisplayBounds.Top, image.DisplayBounds.Width, image.DisplayBounds.Height); } shares = pdfShares; ShareAssembler assembler = new ShareAssembler(); System.Drawing.Bitmap reconImage = assembler.ImageConstruction(imageShares, 2, 2); recon.Pages[i].Canvas.DrawImage(reconImage, image.DisplayBounds.Left, image.DisplayBounds.Top, image.DisplayBounds.Width, image.DisplayBounds.Height); } else { } } System.Console.WriteLine("Page Complete"); //return shares; } timer.Stop(); System.Console.WriteLine("\n\nTime Elapsed: " + timer.ElapsedMilliseconds + "\n\n\n"); timer.Reset(); for (int i = 0; i < pdfShares.Count; i++) { pdfShares[i].Save("Share" + (i + 1) + ".pdf"); pdfShares[i].Dispose(); } recon.Save(@"D:\Recon123-" + xyz + ".pdf"); recon.Dispose(); return(shares); }
public override PDFDocument HydratePdfForm(PDFDocument template, params object[] args) { foreach (var thing in args) { var model = thing as TieSheetPdfModel; if (model == null) { continue; } var canvas = template.Pages[0].Canvas; var center = template.PageWidth / 2; if (model.OverrideTitle) { var titleFont = new TrueTypeFont(FontPath, 39, true, true); var subFont = new TrueTypeFont(FontPath, 25, true, true); var pen = new PDFPen(new PDFColor(0)); var brush = new PDFBrush(new PDFColor(0)); canvas.DrawText(model.County + " County", titleFont, pen, brush, center, 17, 0, TextAlign.MiddleCenter); canvas.DrawText("Monument Record Sheet", subFont, pen, brush, center, 55, 0, TextAlign.MiddleCenter); } else { if (File.Exists(HeaderImagePath)) { var header = Image.FromFile(HeaderImagePath); canvas.DrawImage(header, 36, 0, 528, 71); } } if (model.Sketch != null) { var msSketch = new MemoryStream(model.Sketch); var sketch = Image.FromStream(msSketch); canvas.DrawImage(sketch, 217, 115, 359, 272); } if (model.Thumb != null) { var msThumb = new MemoryStream(model.Thumb); var thumb = Image.FromStream(msThumb); canvas.DrawImage(thumb, 217, 399, 179, 134); } if (model.Thumb2 != null) { var msThumb2 = new MemoryStream(model.Thumb2); var thumb2 = Image.FromStream(msThumb2); canvas.DrawImage(thumb2, 397, 399, 179, 134); } if (model.ExtraPages != null) { var stream = new MemoryStream(model.ExtraPages); var doc = new PDFDocument(stream); foreach (PDFPage page in doc.Pages) { template.AddPage(page); } } if (model.SurveyorSeal != null) { var sealStream = new MemoryStream(model.SurveyorSeal); var image = Image.FromStream(sealStream); canvas.DrawImage(image, 431, 648, 143, 113); } template.WriteToPdfTextField("BLMPointName", model.BlmPointId); template.WriteToPdfTextField("Date", model.CollectionDate); template.WriteToPdfTextField("CornerOfSection", model.SectionCorner); template.WriteToPdfTextField("Township", model.Township); template.WriteToPdfTextField("BaseMeridian", model.BaseMeridian); template.WriteToPdfTextField("State", "Utah"); template.WriteToPdfTextField("County", model.County); template.WriteToPdfTextField("Datum", model.Datum); template.WriteToPdfTextField("Notes", model.Accuracy); template.WriteToPdfTextField("Description", model.Description); template.WriteToPdfTextField("Status", model.MonumentStatus); template.WriteToPdfTextField("LicenseNumber", model.SurveryorLicenseNumber); template.WriteToPdfTextField("ContactName", model.SurveyorName); template.WriteToPdfTextField("CoordinateSystem", model.CoordinateSystem); template.WriteToPdfTextField("Zone", model.Zone); template.WriteToPdfTextField("Northing", model.Northing); template.WriteToPdfTextField("Easting", model.Easting); template.WriteToPdfTextField("OrthoHeight", model.Elevation); template.WriteToPdfTextField("NGSAdjustment", model.CoordinateAdjustment); template.WriteToPdfTextField("VerticalUnits", "meters"); template.WriteToPdfTextField("HorizontalUnits", "meters"); template.WriteToPdfTextField("VerticalDatum", "NAVD88"); template.WriteToPdfTextField("Latitude", model.Latitude); template.WriteToPdfTextField("Longitude", model.Longitude); template.WriteToPdfTextField("EllipsoidHeight", model.ElipsoidHeight); template.WriteToPdfTextField("NGSAdjustment", model.GridAdjustment); } return(template); }
private byte[] ProcessExtraPages(IEnumerable <HttpPostedFileBase> files) { var pdfStreams = new List <Stream>(); if (files == null) { return(null); } foreach (var file in files) { AcceptableFileTypes type; if (file == null || file.ContentLength <= 0 || !IsValidExtension(file.FileName, out type)) { continue; } if (type == AcceptableFileTypes.Pdf) { pdfStreams.Add(file.InputStream); continue; } var doc = new PDFDocument(); var page = doc.AddPage(); var image = Image.FromStream(file.InputStream); var widthPixels = page.Width; var heightPixels = page.Height; if (image.Height <= image.Width && image.Width > widthPixels) { //landscape image.RotateFlip(RotateFlipType.Rotate90FlipNone); var newWidth = image.Width / (image.Height / heightPixels); var size = new ImageSize((int)newWidth, (int)heightPixels); var resizeStream = new MemoryStream(_imageService.CreateSizedImage(image, size)); var newImage = Image.FromStream(resizeStream); page.Canvas.DrawImage(newImage, 0, 0, newImage.Width, newImage.Height); } else if (image.Height > image.Width && image.Height > heightPixels) { //portrait var newWidth = image.Width / (image.Height / heightPixels); var size = new ImageSize((int)newWidth, (int)heightPixels); var resizeStream = new MemoryStream(_imageService.CreateSizedImage(image, size)); var newImage = Image.FromStream(resizeStream); page.Canvas.DrawImage(newImage, 0, 0, newImage.Width, newImage.Height); } else { page.Canvas.DrawImage(image, 0, 0, image.Width, image.Height); } Stream stream = new MemoryStream(); doc.SaveToStream(stream); pdfStreams.Add(stream); image.Dispose(); } if (pdfStreams.Count > 0) { var document = _pdfService.MergePdfDocs(pdfStreams); var bytes = document.GetPDFAsByteArray(); foreach (var stream in pdfStreams) { stream.Dispose(); } return(bytes); } return(null); }
public static bool Tiff2PDFPageByPage(string _tiffFilePath, string _pdfFilePath) { try { // Create the pdf document PDFDocument doc = new PDFDocument(); //Serial number goes here doc.SerialNumber = "PDF4NET-AYBAM-8ARRR-B4EX2-OXGCC-KN2Q5"; // Load TIFF file Bitmap bitmap = new Bitmap(_tiffFilePath); // Calculate number of pages/images used in TIFF file FrameDimension frameDimension = new FrameDimension(bitmap.FrameDimensionsList[0]); int framesCount = bitmap.GetFrameCount(frameDimension); for (int i = 0; i < framesCount; i++) { // Create a new page PDFPage page = doc.AddPage(); // draw the current tiff frame on the page PDFImage image = new PDFImage(bitmap, i); page.Canvas.DrawImage(image, 0, 0, page.Width, page.Height, 0, PDFKeepAspectRatio.KeepNone); } doc.Save(_pdfFilePath); bitmap.Dispose(); return true; } catch { return false; } }
public static void EncryptPDF(string _pathToPDF, string _password) { O2S.Components.PDF4NET.PDFDocument _pdfDocument = new O2S.Components.PDF4NET.PDFDocument(_pathToPDF); //_pdfDocument.s // Create the pdf document O2S.Components.PDF4NET.PDFDocument pdfDoc = new PDFDocument( ); // set the security options pdfDoc.SecurityManager = new PDFSecurityManager( ); // use 128 bit encryption pdfDoc.SecurityManager.KeySize = EncryptionKeySize.Use128BitKey; // set user password to "userpass" pdfDoc.SecurityManager.UserPassword = Encoding.ASCII.GetBytes( "userpass" ); // set owner password to "ownerpass" pdfDoc.SecurityManager.OwnerPassword = Encoding.ASCII.GetBytes( "ownerpass" ); // allow to print the pdf document pdfDoc.SecurityManager.AllowPrint = true; // do not allow high quality print pdfDoc.SecurityManager.FullQualityPrint = false; // do not alow to modify the document pdfDoc.SecurityManager.AllowModifyDocument = false; // do not allow to extract content (text and images) from the document pdfDoc.SecurityManager.AllowExtractContent = false; // do not allow to fill forms or to create annotations pdfDoc.SecurityManager.AllowInteractiveEdit = false; // do not allow forms fill pdfDoc.SecurityManager.AllowFormsFill = false; // allow to extract content in support for accessibility pdfDoc.SecurityManager.AllowAccessibilityExtractContent = true; // do not allow to assemble document pdfDoc.SecurityManager.AllowAssembleDocument = false; // Create one page PDFPage pdfPage = pdfDoc.AddPage( ); // Draw "Encrypted Hello world" in the center of the page //pdfPage.Canvas.DrawText( "Encrypted", // new PDFFont( FontFace.Helvetica, 100 ), new PDFPen( new PDFColor( 255, 0, 0 ), 1 ), // new PDFBrush( new PDFColor( 0, 0, 255 ) ), pdfPage.Width/2, pdfPage.Height/2 - 3, // 0, TextAlign.BottomCenter ); //pdfPage.Canvas.DrawText( "Hello world !", // new PDFFont( FontFace.Helvetica, 100 ), new PDFPen( new PDFColor( 255, 0, 0 ), 1 ), // new PDFBrush( new PDFColor( 0, 0, 255 ) ), pdfPage.Width/2, pdfPage.Height/2 + 3, // 0, TextAlign.TopCenter ); // Save the document to disk pdfDoc.Save(_pathToPDF); }
/** * Recontructs the encrypted shares of the PDF to get original PDF. * Hightlights every occurrences of the word that a user has searched for. * */ public void SharesReconstructor() { List <List <PDFPageObjectCollection> > pDFPageObjects = ExtractObjects(); List <PDFPageObjectCollection> ObjectsPerPage; PDFBrush redBrush = new PDFBrush(new PDFRgbColor(204, 255, 51)); PDFPen pen = new PDFPen(new PDFRgbColor(204, 255, 51), 1); string[] shares; for (int pageIndex = 0; pageIndex < pDFPageObjects.Count; pageIndex++) { _decryptedPDF.AddPage(); ObjectsPerPage = pDFPageObjects[pageIndex]; for (int wordIndex = 0; wordIndex < ObjectsPerPage[0].Count; wordIndex++) { shares = new string[_shareforRec]; PDFTextPageObject TextObject = null; FontsProcessor fontsProcessor = null; bool shouldProcess = false; for (int shareIndex = 0; shareIndex < _shareforRec; shareIndex++) { PDFPageObjectCollection collection = ObjectsPerPage[shareIndex]; if (collection[wordIndex] is PDFTextPageObject) { TextObject = (PDFTextPageObject)collection[wordIndex]; if (TextObject.Text == "PDF4NET evaluation version 5.0.1.0") { shouldProcess = false; break; } shares[shareIndex] = TextObject.Text; fontsProcessor = new FontsProcessor(); shouldProcess = true; } } if (shouldProcess) { string decryptedText = ShareAssembler.TextReconstruction(shares, _shareNumbers, _numOfShares, _shareforRec); PDFBrush brush = new PDFBrush(TextObject.FillColor); PDFFontBase FontBase = fontsProcessor.GetFontBase(TextObject.FontName, TextObject.FontSize); if (decryptedText.ToLower().Equals(_keyword.ToLower())) { _decryptedPDF.Pages[pageIndex].Canvas.DrawRectangle(pen, redBrush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top, 4 * decryptedText.Length, FontBase.Size, 0); for (int i = 0; i < _encryptedPdf.Count; i++) { _encryptedPdf[i].Pages[pageIndex].Canvas.DrawRectangle(pen, redBrush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top, 4 * decryptedText.Length, FontBase.Size, 0); _encryptedPdf[i].Pages[pageIndex].Canvas.DrawText(shares[i > shares.Length - 1 ? 0 : i], FontBase, brush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top); } } _decryptedPDF.Pages[pageIndex].Canvas.DrawText(decryptedText, FontBase, brush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top); } } } for (int i = 0; i < _encryptedPdf.Count; i++) { _encryptedPdf[i].Save(_encryptedFilePath + (i + 1) + ".pdf"); } _decryptedPDF.Save(_decryptedFilePath); }