static void Main(string[] args) { //Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../../../Data/PDF_Succinctly.pdf"); Dictionary <int, List <RectangleF> > textFound = new Dictionary <int, List <RectangleF> >(); //Find the text in the PDF document loadedDocument.FindText("portable", out textFound); PdfDocument document = new PdfDocument(); //import page based on the find text index foreach (int index in textFound.Keys) { if (textFound[index].Count > 0) { document.ImportPage(loadedDocument, index); } } //save the PDF document document.Save("portable.pdf"); //Close the document document.Close(true); loadedDocument.Close(true); }
public IActionResult FindText(string ViewTemplate, string Find) { string basePath = _hostingEnvironment.WebRootPath; FileStream fileStreamInput = new FileStream(basePath + @"/PDF/Manual.pdf", FileMode.Open, FileAccess.Read); if (!string.IsNullOrEmpty(ViewTemplate)) { FileStreamResult fileStreamResult = new FileStreamResult(fileStreamInput, "application/pdf"); fileStreamResult.FileDownloadName = "Manual.pdf"; return(fileStreamResult); } else if (!string.IsNullOrEmpty(Find)) { PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStreamInput); Dictionary <int, List <Syncfusion.Drawing.RectangleF> > matchRects = new Dictionary <int, List <Syncfusion.Drawing.RectangleF> >(); loadedDocument.FindText("document", out matchRects); FindTextMessage message = new FindTextMessage(); for (int i = 0; i < loadedDocument.Pages.Count; i++) { List <Syncfusion.Drawing.RectangleF> rectCoords = matchRects[i]; message.Message = "First Occurrence: X:" + rectCoords[0].X + "; Y:" + rectCoords[0].Y + "; Width:" + rectCoords[0].Width + "; Height:" + rectCoords[0].Height + Environment.NewLine + "Second Occurrence: X:" + rectCoords[1].X + "; Y:" + rectCoords[1].Y + "; Width:" + rectCoords[1].Width + "; Height:" + rectCoords[1].Height + Environment.NewLine + "Third Occurrence: X:" + rectCoords[2].X + "; Y:" + rectCoords[2].Y + "; Width:" + rectCoords[2].Width + "; Height:" + rectCoords[2].Height + Environment.NewLine + "Fourth Occurrence: X:" + rectCoords[3].X + "; Y:" + rectCoords[3].Y + "; Width:" + rectCoords[3].Width + "; Height:" + rectCoords[3].Height + Environment.NewLine; return(View("FindText", message)); } } return(View("FindText")); }
static void Main(string[] args) { // Load an existing PDF PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../../../../../Data/Invoice.pdf"); TextSearchResultCollection searchCollection; TextSearchItem text = new TextSearchItem("Invoice Number", TextSearchOptions.None); //Search the text from PDF dcoument loadedDocument.FindText(new List <TextSearchItem> { text }, out searchCollection); //Iterate serach collection to get serach results foreach (KeyValuePair <int, MatchedItemCollection> textCollection in searchCollection) { //Get age number Console.WriteLine("Page Number : " + textCollection.Key); foreach (MatchedItem textItem in textCollection.Value) { //Get actual text and bounds Console.WriteLine("Text :" + textItem.Text); Console.WriteLine("Text bounds :" + textItem.Bounds); } } //Close the document loadedDocument.Close(true); }
public static void AssinarDocumento() { var arquivoEnt = $"d:\\temp\\modelo.pdf"; var pdf = DSHelper.ReadContent(arquivoEnt); var arquivo = $"d:\\temp\\modeloOut.pdf"; float x; float y; Stream pfxStream = File.OpenRead("MRV ENGENHARIA E PARTICIPAÇÕES S.A..pfx"); //Creates a certificate instance from PFX file with private key. PdfCertificate pdfCert = new PdfCertificate(pfxStream, "zzzzz"); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(pdf); var lista = new Dictionary <int, List <Syncfusion.Drawing.RectangleF> >(); loadedDocument.FindText("Assinado:", out lista); foreach (var item in lista) { x = item.Value[0].X + 100; y = item.Value[0].Y; var page = loadedDocument.Pages[item.Key] as PdfLoadedPage; //aplica logo da assinatura em todas as paginas if (page != null) { Stream seloStream = File.OpenRead("SeloMrv.jpg"); PdfBitmap signatureImage = new PdfBitmap(seloStream); PdfGraphics gfx = page.Graphics; gfx.DrawImage(signatureImage, x, y, 90, 80); } //Applica o certificado somente na ultima pagina if (item.Value == lista[lista.Keys.Count - 1]) { //Creates a signature field. PdfSignatureField signatureField = new PdfSignatureField(page, "AssinaturaMRV"); signatureField.Bounds = new Syncfusion.Drawing.RectangleF(x, item.Value[0].Y, 50, 50); signatureField.Signature = new PdfSignature(page, "MRV Engenharia"); //Adds certificate to the signature field. signatureField.Signature.Certificate = pdfCert; signatureField.Signature.Reason = "Assinado pela MRV Engenharia"; //Adds the field. loadedDocument.Form.Fields.Add(signatureField); } } //Saves the certified PDF document. using (FileStream fileOut = new FileStream(arquivo, FileMode.Create)) { loadedDocument.Save(fileOut); loadedDocument.Close(true); } //return arquivo; }
private void Button2_Click(object sender, RoutedEventArgs e) { string text = textbox.Text; // Get the template PDF file stream from assembly. Stream documentStream = typeof(FindText).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.pdf_succinctly.pdf"); //Load the PDF document from stream. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); TextSearchResultCollection results; //Create list and add a string text to find from PDF document. List <string> searchItem = new List <string>(); searchItem.Add(text); //Find text from PDF document and get the result collection. loadedDocument.FindText(searchItem, out results); //Iterate over the result collection. foreach (var result in results) { //Get the PDF page using the result. PdfLoadedPage page = loadedDocument.Pages[result.Key] as PdfLoadedPage; //Save the current graphics state. page.Graphics.Save(); //Set transparency to the page graphics. page.Graphics.SetTransparency(0.5F); foreach (var matchedItem in result.Value) { //Draw rectangle to highlight the text in PDF page. page.Graphics.DrawRectangle(PdfBrushes.Yellow, matchedItem.Bounds); } //Restore the graphics state. page.Graphics.Restore(); } //Creating the stream object. using (MemoryStream stream = new MemoryStream()) { //Save and close the document. loadedDocument.Save(stream); loadedDocument.Close(); stream.Position = 0; //Save the output stream as a file using file picker. PdfUtil.Save("FindText.pdf", stream); } }