Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
aspose.com blog gists
Document pdfdoc = new Document();
Page page = pdfdoc.Pages.Add();
TextBoxField nameBox = new TextBoxField(pdfdoc, new Aspose.Pdf.Rectangle(275, 740, 440, 770));
nameBox.PartialName = "nameBox1";
nameBox.DefaultAppearance.FontSize = 10;
nameBox.Multiline = true;
Border nameBorder = new Border(nameBox);
nameBorder.Width = 1;
nameBox.Border = nameBorder;
nameBox.Characteristics.Border = System.Drawing.Color.Black;
nameBox.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
TextBoxField mrnBox = new TextBoxField(pdfdoc, new Aspose.Pdf.Rectangle(275, 718, 440, 738));
mrnBox.PartialName = "Box1";
mrnBox.DefaultAppearance.FontSize = 10;
Border mrnBorder = new Border(mrnBox);
mrnBorder.Width = 1;
mrnBox.Border = mrnBorder;
mrnBox.Characteristics.Border = System.Drawing.Color.Black;
mrnBox.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
// Add form fields to first page of PDF document
pdfdoc.Form.Add(nameBox, 1);
pdfdoc.Form.Add(mrnBox, 1);
//Add Radiobuttons at specific position coordinates in PDF
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
//Set position here
table.Left = 200;
table.Top = 300;
table.ColumnWidths = "120";
page.Paragraphs.Add(table);
Aspose.Pdf.Row r1 = table.Rows.Add();
Aspose.Pdf.Row r2 = table.Rows.Add();
Aspose.Pdf.Cell c1 = r1.Cells.Add();
Aspose.Pdf.Cell c2 = r2.Cells.Add();
RadioButtonField rf = new RadioButtonField(page);
rf.PartialName = "radio";
pdfdoc.Form.Add(rf, 1);
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
opt1.OptionName = "Yes";
opt2.OptionName = "No";
opt1.Width = 15;
opt1.Height = 15;
opt2.Width = 15;
opt2.Height = 15;
rf.Add(opt1);
rf.Add(opt2);
opt1.Border = new Border(opt1);
opt1.Border.Width = 1;
opt1.Border.Style = BorderStyle.Solid;
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Yes");
opt2.Border = new Border(opt1);
opt2.Border.Width = 1;
opt2.Border.Style = BorderStyle.Solid;
opt2.Characteristics.Border = System.Drawing.Color.Black;
opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt2.Caption = new TextFragment("No");
c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
pdfdoc.Save(dataDir + "Fillable_PDF_Form.pdf");
// Open document
Document pdfDocument = new Document(dataDir + "Fill_PDF_Form_Field.pdf");
// Delete a particular field by name
pdfDocument.Form.Delete("nameBox1");
dataDir = dataDir + "Delete_Form_Field.pdf";
// Save modified document
pdfDocument.Save(dataDir);
// Read the source PDF form with FileAccess of Read and Write.
// We need ReadWrite permission because after modification,
// We need to save the updated contents in same document/file.
FileStream fs = new FileStream(dataDir + "Fill_PDF_Form_Field.pdf", FileMode.Open, FileAccess.ReadWrite);
// Instantiate Document instance
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(fs);
// Get values from all fields
foreach (Field formField in pdfDocument.Form)
{
// If the fullname of field contains A1, perform the operation
if (formField.FullName.Contains("nameBox1"))
{
// Cast form field as TextBox
TextBoxField textBoxField = formField as TextBoxField;
// Modify field value
textBoxField.Value = "Preserve Extended Features";
}
}
// Save the updated document in save FileStream
pdfDocument.Save();
// Close the File Stream object
fs.Close();
Document doc = new Document(dataDir + "ExtractHighlightedText.pdf");
// Loop through all the annotations
foreach (Annotation annotation in doc.Pages[1].Annotations)
{
// Filter TextMarkupAnnotation
if (annotation is TextMarkupAnnotation)
{
TextMarkupAnnotation highlightedAnnotation = annotation as TextMarkupAnnotation;
// Retrieve highlighted text fragments
TextFragmentCollection collection = highlightedAnnotation.GetMarkedTextFragments();
foreach (TextFragment tf in collection)
{
// Display highlighted text
Console.WriteLine(tf.Text);
}
}
}
// Open PDF document
Document pdfDocument = new Document(dataDir + "ExtractTextAll.pdf");
// Access required page in PDF document
Page page = pdfDocument.Pages[1];
// Create TextAbsorber object to extract text
TextAbsorber textAbsorber = new TextAbsorber();
// Accept the absorber for specified page
page.Accept(textAbsorber);
// Get the extracted text
string extractedText = textAbsorber.Text;
// Create a writer and open the file
TextWriter tw = new StreamWriter(dataDir + "extracted-text.txt");
// Write a line of text to the file
tw.WriteLine(extractedText);
// Close the stream
tw.Close();
// Open document
Document pdfDocument = new Document(dataDir + "ExtractTextAll.pdf");
// Create TextAbsorber object to extract text
TextAbsorber absorber = new TextAbsorber();
absorber.TextSearchOptions.LimitToPageBounds = true;
absorber.TextSearchOptions.Rectangle = new Aspose.Pdf.Rectangle(100, 200, 250, 350);
// Accept the absorber for first page
pdfDocument.Pages[1].Accept(absorber);
// Get the extracted text
string extractedText = absorber.Text;
// Create a writer and open the file
TextWriter tw = new StreamWriter(dataDir + "extracted-text.txt");
// Write a line of text to the file
tw.WriteLine(extractedText);
// Close the stream
tw.Close();
// Open PDF document
Document pdfDocument = new Document(dataDir + "ExtractTextAll.pdf");
// Create TextAbsorber object to extract text
TextAbsorber textAbsorber = new TextAbsorber();
// Accept the absorber for all pages
pdfDocument.Pages.Accept(textAbsorber);
// Get the extracted text
string extractedText = textAbsorber.Text;
// Create a writer and open the file
TextWriter tw = new StreamWriter(dataDir + "extracted-text.txt");
// Write a line of text to the file
tw.WriteLine(extractedText);
// Close the stream
tw.Close();
Document pdfDocument = new Document(dataDir + @"ITF-TTF Manual.pdf");
TextFragmentAbsorber absorber = new TextFragmentAbsorber(new TextEditOptions(TextEditOptions.FontReplace.RemoveUnusedFonts));
foreach (Page page in pdfDocument.Pages)
{
page.Accept(absorber);
//Read something from fragments
count += absorber.TextFragments.Count;
absorber.Reset();
page.FreeMemory();
//GC.Collect();
}
// Open document
Document pdfDocument = new Document(dataDir + "Test.pdf");
System.Text.StringBuilder builder = new System.Text.StringBuilder();
// String to hold extracted text
string extractedText = "";
foreach (Page pdfPage in pdfDocument.Pages)
{
using (MemoryStream textStream = new MemoryStream())
{
// Create text device
TextDevice textDevice = new TextDevice();
// Set text extraction options - set text extraction mode (Raw or Pure)
TextExtractionOptions textExtOptions = new
TextExtractionOptions(TextExtractionOptions.TextFormattingMode.MemorySaving);
textDevice.ExtractionOptions = textExtOptions;
// Convert a particular page and save text to the stream
textDevice.Process(pdfPage, textStream);
// Convert a particular page and save text to the stream
textDevice.Process(pdfDocument.Pages[1], textStream);
// Close memory stream
textStream.Close();
// Get text from memory stream
extractedText = Encoding.Unicode.GetString(textStream.ToArray());
}
builder.Append(extractedText);
}
dataDir = dataDir + "Memory_Text_Extracted.txt";
// Save the extracted text in text file
File.WriteAllText(dataDir, builder.ToString());
// open document
Document pdfDocument = new Document(dataDir + @"Test.pdf");
// create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(@"\d{4}"); //like 1999-2020 // set text search option to specify regular expression usage
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
// accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
Console.WriteLine(textFragment.Text);
}
Document pdfDocument = new Document(dataDir + "Test.pdf");
TableAbsorber absorber = new TableAbsorber();
absorber.Visit(pdfDocument.Pages[1]);
foreach (AbsorbedTable table in absorber.TableList)
{
foreach (AbsorbedRow row in table.RowList)
{
foreach (AbsorbedCell cell in row.CellList)
{
TextFragment textfragment = new TextFragment();
TextFragmentCollection textFragmentCollection = cell.TextFragments;
foreach (TextFragment fragment in textFragmentCollection)
{
Console.WriteLine(fragment.Text);
}
}
}
}
// Open document
Document pdfDocument = new Document(dataDir + "Fillable_PDF_Form.pdf");
// Get the fields
TextBoxField textBoxField1 = pdfDocument.Form["nameBox1"] as TextBoxField;
TextBoxField textBoxField2 = pdfDocument.Form["Box1"] as TextBoxField;
// Fill form fields' values
textBoxField1.Value = "A quick brown fox jumped over a lazy dog.";
textBoxField2.Value = "A quick brown fox jumped over a lazy dog.";
// Get radio button field
RadioButtonField radioField = pdfDocument.Form["radio"] as RadioButtonField;
// Specify the index of radio button from group
radioField.Selected = 1;
dataDir = dataDir + "Fill_PDF_Form_Field.pdf";
// Save updated document
pdfDocument.Save(dataDir);
Aspose.Pdf.Document pdfdoc = new Aspose.Pdf.Document();
pdfdoc.Pages.Add();
Aspose.Pdf.Forms.TextBoxField textBoxField = new Aspose.Pdf.Forms.TextBoxField(pdfdoc.Pages[1], new Aspose.Pdf.Rectangle(85, 750, 215, 770));
textBoxField.PartialName = "textbox1";
textBoxField.Value = "Text Box";
//TextBoxField.Border = new Border();
Border border = new Border(textBoxField);
border.Width = 2;
border.Dash = new Dash(1, 1);
textBoxField.Border = border;
textBoxField.DefaultAppearance.FontSize = 10;
textBoxField.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
// Add field to the document
pdfdoc.Form.Add(textBoxField, 1);
string JS = @"var w = this.getField('" + textBoxField.PartialName + "'); var today = new Date(); w.value = today.toLocaleString();";
pdfdoc.OpenAction = new JavascriptAction(JS);
pdfdoc.Save(dataDir + "JS_Form.pdf");
// Open document
Document pdfDocument = new Document(dataDir + "Fill_PDF_Form_Field.pdf");
// Get a field
TextBoxField textBoxField = pdfDocument.Form["nameBox1"] as TextBoxField;
// Modify field value
textBoxField.Value = "Changed Value";
textBoxField.ReadOnly = true;
dataDir = dataDir + "ModifyFormField.pdf";
// Save updated document
pdfDocument.Save(dataDir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.