public Bitmap ToBitmap(string pdfFile) { Bitmap bitmap = null; using (PDFDoc doc = new PDFDoc(pdfFile)) { foxit.common.ErrorCode error_code = doc.Load(null); if (error_code == foxit.common.ErrorCode.e_ErrSuccess) { // Console.WriteLine("The PDFDoc [{0}] Error: {1}\n", pdfFile, error_code); //} //else //{ int nPageCount = doc.GetPageCount(); for (int i = 0; i < nPageCount; i++) { using (PDFPage page = doc.GetPage(i)) { // Parse page. page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false); int width = (int)(page.GetWidth()) * 600 / 96; int height = (int)(page.GetHeight()) * 600 / 96; Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation()); // Prepare a bitmap for rendering. bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bitmap.SetResolution((float)600, (float)600); using (Graphics draw = Graphics.FromImage(bitmap)) { draw.Clear(Color.White); // Render page foxit.common.Renderer render = new foxit.common.Renderer(bitmap, false); render.StartRender(page, matrix, null); } } } } } return(bitmap); }
private static string GeneratePDF(UserDataJsonModel model, bool applyRedaction) { Library.Initialize(serialNo, key); try { // Create a new PDF with a blank page. using PDFDoc doc = new PDFDoc(); using Form form = new Form(doc); using PDFPage page = doc.InsertPage(0, PDFPage.Size.e_SizeLetter); // Create a text field for each property of the user data json model. int iteration = 0; foreach (var prop in model.GetType().GetProperties()) { int offset = iteration * 60; using Control control = form.AddControl(page, prop.Name, Field.Type.e_TypeTextField, new RectF(50f, 600f - offset, 400f, 640f - offset)); using Field field = control.GetField(); var propValue = prop.GetValue(model); field.SetValue($"{prop.Name}: {propValue}"); iteration++; } // Convert fillable form fields into readonly text labels. page.Flatten(true, (int)PDFPage.FlattenOptions.e_FlattenAll); if (applyRedaction) { // Configure our text search to look at the first (and only) page in our PDF. using var redaction = new Redaction(doc); page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false); using TextPage textPage = new TextPage(page, (int)foxit.pdf.TextPage.TextParseFlags.e_ParseTextUseStreamOrder); using TextSearch search = new TextSearch(textPage); RectFArray rectArray = new RectFArray(); // Mark text starting with the pattern "SIN:" to be redacted. search.SetPattern("SIN:"); while (search.FindNext()) { using var searchSentence = new TextSearch(textPage); var sentence = search.GetMatchSentence(); searchSentence.SetPattern(sentence.Substring(sentence.IndexOf("SIN:"))); while (searchSentence.FindNext()) { RectFArray itemArray = searchSentence.GetMatchRects(); rectArray.InsertAt(rectArray.GetSize(), itemArray); } } // If we had matches to redact, then apply the redaction. if (rectArray.GetSize() > 0) { using Redact redact = redaction.MarkRedactAnnot(page, rectArray); redact.SetFillColor(0xFF0000); redact.ResetAppearanceStream(); redaction.Apply(); } } // Save the file locally and return the file's location to the caller. string fileOutput = "./Output.pdf"; doc.SaveAs(fileOutput, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal); return(fileOutput); } finally { Library.Release(); } }