Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.
Wed Nov 30, 2016 12:40 pm
Hi,
- Code: Select all
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(@"D:\PDF\test2.pdf");
pdf.PrinterName = "PdfCreator";
PdfTextFind[] result = null;
foreach (PdfPageBase page in pdf.Pages)
{
result = (page.FindText("Michal Barchacki").Finds);
foreach (PdfTextFind find in result)
{
//if (find.LineText.Contains("KW12341"))
Console.WriteLine(find.LineText);
Console.Read();
}
}
}
In my code im searching a text in pdf file. If I find then i want to print the page where the text is.
How can I define the number of page to print?
Best regards
Bartek
-

[email protected]
-
- Posts: 2
- Joined: Wed Nov 30, 2016 12:35 pm
Thu Dec 01, 2016 2:45 am
Dear Bartek,
Thanks for your inquiry.
Here is sample code for your reference.
- Code: Select all
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(@"F:\testing\pdf form testing\original document\9223.pdf");
PdfTextFind[] result = null;
foreach (PdfPageBase page in pdf.Pages)
{
result = (page.FindText("2").Finds);
foreach (PdfTextFind find in result)
{
//get the page number of the text
int pageNumber = find.SearchPageIndex + 1;
//print that page
PrintPage(pdf, pageNumber);
}
}
}
public static void PrintPage(PdfDocument doc, int index)
{
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.FromPage = index;
dialogPrint.PrinterSettings.ToPage = index;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
//Set the pagenumber which you choose as the start page to print
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
//Set the pagenumber which you choose as the final page to print
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
PrintDocument printDoc = doc.PrintDocument;
dialogPrint.Document = printDoc;
printDoc.Print();
}
}
If there is any question, welcome to get it back to us.
Sincerely,
Betsy
E-iceblue support team
-


Betsy.jiang
-
- Posts: 3099
- Joined: Tue Sep 06, 2016 8:30 am
Fri Dec 02, 2016 7:18 am
Dear Bartek,
Did you test the code ? Did it help you ?
Thanks,
Betsy
E-iceblue support team
-


Betsy.jiang
-
- Posts: 3099
- Joined: Tue Sep 06, 2016 8:30 am