Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Мы можем получить текст из PDF-документа, ища определенный текст (с помощью “обычного текста” или “регулярных выражений”) с одной страницы или всего документа, или мы можем получить полный текст одной страницы, диапазона страниц или всего документа. Однако в некоторых случаях вам необходимо извлечь абзацы из PDF-документа или текст в виде абзацев. Мы реализовали функциональность для поиска разделов и абзацев в тексте страниц PDF-документа. Мы представили класс ParagraphAbsorber (как TextFragmentAbsorber и TextAbsorber), который можно использовать для извлечения абзацев из PDF-документов. Существует два следующих способа, которыми вы можете использовать ParagraphAbsorber:
Рисуя границу разделов и абзацев текста на странице PDF:
Следующий фрагмент кода также работает с библиотекой Aspose.PDF.Drawing.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractParagraphWithDrawingTheBorder()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentForExtract.pdf"))
{
var page = document.Pages[2];
var absorber = new Aspose.Pdf.Text.ParagraphAbsorber();
absorber.Visit(page);
Aspose.Pdf.Text.PageMarkup markup = absorber.PageMarkups[0];
foreach (Aspose.Pdf.Text.MarkupSection section in markup.Sections)
{
DrawRectangleOnPage(section.Rectangle, page);
foreach (Aspose.Pdf.Text.MarkupParagraph paragraph in section.Paragraphs)
{
DrawPolygonOnPage(paragraph.Points, page);
}
}
// Save PDF document
document.Save(dataDir + "DocumentWithBorder_out.pdf");
}
}
private static void DrawRectangleOnPage(Aspose.Pdf.Rectangle rectangle, Aspose.Pdf.Page page)
{
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(1, 0, 0, 1, 0, 0));
page.Contents.Add(new Aspose.Pdf.Operators.SetRGBColorStroke(0, 1, 0));
page.Contents.Add(new Aspose.Pdf.Operators.SetLineWidth(2));
page.Contents.Add(
new Aspose.Pdf.Operators.Re(rectangle.LLX,
rectangle.LLY,
rectangle.Width,
rectangle.Height));
page.Contents.Add(new Aspose.Pdf.Operators.ClosePathStroke());
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
}
private static void DrawPolygonOnPage(Aspose.Pdf.Point[] polygon, Aspose.Pdf.Page page)
{
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(1, 0, 0, 1, 0, 0));
page.Contents.Add(new Aspose.Pdf.Operators.SetRGBColorStroke(0, 0, 1));
page.Contents.Add(new Aspose.Pdf.Operators.SetLineWidth(1));
page.Contents.Add(new Aspose.Pdf.Operators.MoveTo(polygon[0].X, polygon[0].Y));
for (int i = 1; i < polygon.Length; i++)
{
page.Contents.Add(new Aspose.Pdf.Operators.LineTo(polygon[i].X, polygon[i].Y));
}
page.Contents.Add(new Aspose.Pdf.Operators.LineTo(polygon[0].X, polygon[0].Y));
page.Contents.Add(new Aspose.Pdf.Operators.ClosePathStroke());
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
}
Итерация по коллекции абзацев и получение текста из них:
Следующий фрагмент кода также работает с библиотекой Aspose.PDF.Drawing.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractParagraphByIteratingThroughParagraphsCollection()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "DocumentForExtract.pdf"))
{
// Instantiate ParagraphAbsorber
var absorber = new Aspose.Pdf.Text.ParagraphAbsorber();
absorber.Visit(document);
foreach (Aspose.Pdf.Text.PageMarkup markup in absorber.PageMarkups)
{
int i = 1;
foreach (Aspose.Pdf.Text.MarkupSection section in markup.Sections)
{
int j = 1;
foreach (Aspose.Pdf.Text.MarkupParagraph paragraph in section.Paragraphs)
{
StringBuilder paragraphText = new StringBuilder();
foreach (List<Aspose.Pdf.Text.TextFragment> line in paragraph.Lines)
{
foreach (Aspose.Pdf.Text.TextFragment fragment in line)
{
paragraphText.Append(fragment.Text);
}
paragraphText.Append("\r\n");
}
paragraphText.Append("\r\n");
Console.WriteLine("Paragraph {0} of section {1} on page {2}:", j, i, markup.Number);
Console.WriteLine(paragraphText.ToString());
j++;
}
i++;
}
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.