コード例 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            String      input = @"..\..\..\..\..\..\Data\SearchReplaceTemplate.pdf";
            PdfDocument doc   = new PdfDocument();

            // Read a pdf file
            doc.LoadFromFile(input);

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            // Create PdfTextFindCollection object to find all the matched phrases
            PdfTextFindCollection collection = page.FindText("Spire.PDF for .NET", TextFindParameter.IgnoreCase);

            // Create a StringBuilder object to put the details of the text searched
            StringBuilder builder = new StringBuilder();

            foreach (PdfTextFind find in collection.Finds)
            {
                builder.AppendLine("==================================================================================");
                builder.AppendLine("Match Text: " + find.MatchText);
                builder.AppendLine("Text: " + find.SearchText);
                builder.AppendLine("Size: " + find.Size);
                builder.AppendLine("Position: " + find.Position);
                builder.AppendLine("The index of page which is including the searched text : " + find.SearchPageIndex);
                builder.AppendLine("The line that contains the searched text : " + find.LineText);
                builder.AppendLine("Match Text: " + find.MatchText);
            }

            String result = "GetDetailsOfSearchedText_out.txt";

            File.WriteAllText(result, builder.ToString());
            //Launch the result file
            DocumentViewer(result);
        }
コード例 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            String      input = @"..\..\..\..\..\..\Data\SearchReplaceTemplate.pdf";
            PdfDocument doc   = new PdfDocument();

            // Read a pdf file
            doc.LoadFromFile(input);

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            // Create PdfTextFindCollection object to find all the matched phrases
            PdfTextFindCollection collection = page.FindText("e-iceblue", TextFindParameter.IgnoreCase);

            // hyperlink url
            String url = "http://www.e-iceblue.com";

            foreach (PdfTextFind find in collection.Finds)
            {
                // Create a PdfUriAnnotation object to add hyperlink for the searched text
                PdfUriAnnotation uri = new PdfUriAnnotation(find.Bounds);
                uri.Uri    = url;
                uri.Border = new PdfAnnotationBorder(1f);
                uri.Color  = Color.Blue;
                page.AnnotationsWidget.Add(uri);
            }

            String result = "SearchTextAndAddHyperlink_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
コード例 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            String      input = @"..\..\..\..\..\..\Data\SearchReplaceTemplate.pdf";
            PdfDocument doc   = new PdfDocument();

            // Read a pdf file
            doc.LoadFromFile(input);

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            // Create PdfTextFindCollection object to find all the matched phrases
            PdfTextFindCollection collection = page.FindText("Spire.PDF for .NET", TextFindParameter.IgnoreCase);

            foreach (PdfTextFind find in collection.Finds)
            {
                // Draw a rectangle with red pen
                page.Canvas.DrawRectangle(new PdfPen(PdfBrushes.Red, 0.9f), find.Bounds);
            }

            String result = "SearchTextAndDrawRectangle_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
コード例 #4
0
        //向签发日期后添加日期
        /// <summary>
        /// 向签发日期后添加日期
        /// </summary>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <param name="flagTxt">要查找的字符串</param>
        /// <param name="issueDate">要写入的日期</param>
        public void addIssueDateToPdf(string oldPath, string newPath, string flagTxt, string issueDate)
        {
            try
            {
                //Create a pdf document
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(oldPath);

                PdfPageBase   page   = null;
                PdfTextFind[] result = null;
                for (int i = 2; i < doc.Pages.Count; i++)
                {
                    page = doc.Pages[i];
                    try
                    {
                        //pdfTextFindCollection = page.FindText(flagTxt);
                        //pdfTextFindCollection = page.ExecuteCommandFindText(flagTxt);
                        PdfTextFindCollection pdfTextFindCollection = page.FindText(flagTxt);
                        result = pdfTextFindCollection.Finds;
                    }catch (Exception ex)
                    {
                        classLims_NPOI.WriteLog("查询标记字符串时出错,可能是文件损坏或编码格式错误!\n"
                                                + ex.ToString(), "");
                        continue;
                    }
                    if (result.Length > 0)
                    {
                        break;
                    }
                }
                //如果没找到标记字符串,记录并返回
                if (result == null || result.Length == 0)
                {
                    classLims_NPOI.WriteLog("标记字符串未找到", "");
                    return;
                }

                //获取第一次出现文字的坐标,宽度和高度
                PointF pointf = result[0].Position;
                //获取文字的宽高
                SizeF size = result[0].Size;

                AlignText(page, issueDate, pointf.X + size.Width + 50, pointf.Y + 3);
                //save pdf file
                doc.SaveToFile(newPath);
                doc.Close();
            }
            catch (Exception ex)
            {
                classLims_NPOI.WriteLog(ex, "");
            }
        }
コード例 #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            String      input = @"..\..\..\..\..\..\Data\SearchReplaceTemplate.pdf";
            PdfDocument doc   = new PdfDocument();

            // Read a pdf file
            doc.LoadFromFile(input);

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            // Create PdfTextFindCollection object to find all the phrases matching the regular expression
            PdfTextFindCollection collection = page.FindText("\\d{4}", TextFindParameter.IgnoreCase);

            String newText = "New Year";

            // Creates a brush
            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

            // Defines a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 7f, FontStyle.Bold));

            // Defines text horizontal/vertical center format
            PdfStringFormat centerAlign = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            RectangleF rec;

            foreach (PdfTextFind find in collection.Finds)
            {
                // Gets the bound of the found text in page
                rec = find.Bounds;

                page.Canvas.DrawRectangle(PdfBrushes.GreenYellow, rec);
                // Draws new text as defined font and color
                page.Canvas.DrawString(newText, font, brush, rec, centerAlign);

                // This method can directly replace old text with newText,but it just can set the background color, can not set font/forecolor
                // find.ApplyRecoverString(newText, Color.Gray);
            }

            String result = "ReplaceTextWithRegularExpression_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
コード例 #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            String      input = @"..\..\..\..\..\..\Data\SearchReplaceTemplate.pdf";
            PdfDocument doc   = new PdfDocument();

            // Read a pdf file
            doc.LoadFromFile(input);

            // Get the first page of pdf file
            PdfPageBase page = doc.Pages[0];

            // Searches "Spire.PDF for .NET" by ignoring case
            PdfTextFindCollection collection = page.FindText("Spire.PDF for .NET", TextFindParameter.IgnoreCase);

            String newText = "E-iceblue Spire.PDF";

            // Creates a brush
            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

            // Defines a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));

            RectangleF rec;

            foreach (PdfTextFind find in collection.Finds)
            {
                // Gets the bound of the found text in page
                rec = find.Bounds;

                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                // Draws new text as defined font and color
                page.Canvas.DrawString(newText, font, brush, rec);

                // This method can directly replace old text with newText,but it just can set the background color, can not set font/forecolor
                // find.ApplyRecoverString(newText, Color.Gray);
            }

            String result = "ReplaceAllSearchedText_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
コード例 #7
0
        //向PDF指定位置添加图片
        /// <summary>
        /// 向PDF指定位置添加图片,按照标记字符串
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="newPath"></param>
        /// <param name="flagTxt">标记字符串</param>
        /// <param name="imgPath">图片路径</param>
        /// <param name="imgHeight">图片高度,为0时使用图片原始高度</param>
        /// <param name="imgWidth">图片宽度,为0时使用图片原始宽度</param>
        public void addImage2Pdf_path(string filePath, string newPath, string flagTxt, string imgPath, float imgWidth, float imgHeight)
        {
            try
            {
                //Create a pdf document
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(filePath);

                PdfPageBase   page   = null;
                PdfTextFind[] result = null;
                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    page = doc.Pages[i];
                    PdfTextFindCollection pdfTextFindCollection = null;
                    //pdfTextFindCollection = page.FindText(flagTxt);
                    //pdfTextFindCollection = page.ExecuteCommandFindText(flagTxt);
                    pdfTextFindCollection = page.FindText(flagTxt);
                    if (pdfTextFindCollection == null)
                    {
                        continue;
                    }
                    result = pdfTextFindCollection.Finds;
                    if (result.Length > 0)
                    {
                        break;
                    }
                }
                //如果没找到标记字符串,记录并返回
                if (result == null || result.Length == 0)
                {
                    classLims_NPOI.WriteLog("标记字符串未找到", "");
                    return;
                }

                //获取第一次出现文字的坐标,宽度和高度
                PointF pointf = result[0].Position;
                //获取文字的宽高
                SizeF size = result[0].Size;

                if (!File.Exists(imgPath))
                {
                    classLims_NPOI.WriteLog("图片文件未找到", "");
                    return;
                }
                PdfImage image  = PdfImage.FromFile(imgPath);
                float    width  = imgWidth;
                float    height = imgHeight;
                if (imgWidth == 0)
                {
                    width = image.Width;
                }
                if (imgHeight == 0)
                {
                    height = image.Height;
                }


                //签名图片Y轴位置为 始终和标记字符串居中对齐
                pointf.Y = pointf.Y - (height - size.Height) / 2;
                //insert image
                page.Canvas.DrawImage(image, pointf.X, pointf.Y, width, height);

                //save pdf file
                doc.SaveToFile(newPath);
                doc.Close();
            }
            catch (Exception ex)
            {
                classLims_NPOI.WriteLog(ex, "");
            }
        }