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.

Mon Sep 16, 2024 9:21 am

Hello, how can I move a PdfTextFragment up above all other elements?

metrizzi
 
Posts: 1
Joined: Mon Sep 16, 2024 9:14 am

Tue Sep 17, 2024 2:34 am

Hi,

Thanks for your inquiry.
The text fragments extracted by our product include positional information. I assume that what you want is to move a certain text fragment to the top while also resetting the position of the text fragment. Please refer to the following code to implement. If my understanding is incorrect, please provide more details about your requirement.
Code: Select all
// Create a new instance of PdfDocument
PdfDocument document = new PdfDocument();

// Load the PDF file from the specified path
document.LoadFromFile("test.pdf");

// Get the first page of the document
PdfPageBase pageBase = document.Pages[0];

// Create an instance of PdfTextFinder to find text fragments on the page
PdfTextFinder finder = new PdfTextFinder(pageBase);

// Find all text fragments on the page and store them in a list
List<PdfTextFragment> result = finder.FindAllText();

// Get the first text fragment from the list
PdfTextFragment firstTextFragment = result[0];

// Get the position information of the first text fragment
PointF point = firstTextFragment.Positions[0];

// Get the X and Y coordinate
float x = point.X;
float y = point.Y;

// Get the third text fragment from the list
PdfTextFragment thirdTextFragment = result[2];

// Set the X-coordinate and Y-coordinate
thirdTextFragment.Positions[0].X = x;
thirdTextFragment.Positions[0].Y = y - 10;

// Insert the third text fragment at the beginning of the list so it appears above all other fragments
result.Insert(0, thirdTextFragment);

// Remove the original position of the third text fragment from the list to avoid duplicates
result.RemoveAt(3);

// Save the modified document to a new PDF file
document.SaveToFile("result.pdf", FileFormat.PDF);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1195
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF