We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

I am trying to search text using TextFragmentAbsorber but in its constructor, if their is colon( : ) in text , then its collection size is zero

I am trying to search text using TextFragmentAbsorber but in its constructor, if their is colon( : ) in text , then its collection size is zero

@aviral

Can you please share the used sample code, source file and snapshot of issue. We will be able to investigate that on our end on provision of requested information.

I think their some problem with the pdf.

Meanwhile, can you help me how to set alignment of a text in existing pdf.

I am using this type of logic for setting alignment

package com.digivalet.aspose;

import java.util.Scanner;

import com.aspose.pdf.Document;
import com.aspose.pdf.Font;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.Position;
import com.aspose.pdf.Rectangle;
import com.aspose.pdf.TextBuilder;
import com.aspose.pdf.TextFragment;
import com.aspose.pdf.TextFragmentAbsorber;
import com.aspose.pdf.TextFragmentCollection;

public class AsposePDFEditor
{
private String text;
private Rectangle rectangle;
Document document;
TextFragmentAbsorber absorber;
TextFragmentCollection coll;
private double pageWidth;
private double textWidth;
private double rightXIndent;
private double leftXIndent = 25;
private double centerXIndent;
private double yIndent;
private double textHeight;
private float fontSize;
private int fontStyle;
private int initialAlign = 0;
private double initialXIndent;
private Font font;

//leftXIntent is 22 for printer mailer
public AsposePDFEditor(String text) throws Exception
{
	this.text = text;
	init();
}



public void init() throws Exception
{	
	//read all text

// Document readDocument = new Document("/home/aviralshah/Downloads/SMT-131 ver 2 Manual.pdf");
// TextFragmentAbsorber fragmentAbsorber = new TextFragmentAbsorber();
// readDocument.getPages().get_Item(3).accept(fragmentAbsorber);
// TextFragmentCollection textFragmentCollection = absorber.getTextFragments();
// for (TextFragment textFragment : (Iterable) textFragmentCollection) {
// // Iterate through text segments
// for (TextSegment textSegment : (Iterable) textFragment.getSegments()) {
// System.out.println("Text :- " + textSegment.getText());
// System.out.println("Position :- " + textSegment.getPosition());
// System.out.println("XIndent :- " + textSegment.getPosition().getXIndent());
// System.out.println("YIndent :- " + textSegment.getPosition().getYIndent());
// System.out.println("Font - Name :- " + textSegment.getTextState().getFont().getFontName());
// System.out.println("Font - IsAccessible :- " + textSegment.getTextState().getFont().isAccessible());
// System.out.println("Font - IsEmbedded - " + textSegment.getTextState().getFont().isEmbedded());
// System.out.println("Font - IsSubset :- " + textSegment.getTextState().getFont().isSubset());
// System.out.println("Font Size :- " + textSegment.getTextState().getFontSize());
// }
// }

	//perform operations
	document = new Document("sample.pdf");
	absorber = new TextFragmentAbsorber(text);
//	absorber.getTextSearchOptions().setLimitToPageBounds(true);
//	absorber.setTextEditOptions(new );
	document.getPages().get_Item(1).accept(absorber);
	coll = absorber.getTextFragments();
	System.out.println(coll.size());
	fontSize = coll.get_Item(1).getTextState().getFontSize();
	fontStyle = coll.get_Item(1).getTextState().getFontStyle();
	font = coll.get_Item(1).getTextState().getFont();
	rectangle = coll.get_Item(1).getRectangle();
	textWidth = rectangle.getWidth();
	pageWidth = document.getPages().get_Item(1).getRect().getWidth();
	textHeight = coll.get_Item(1).getRectangle().getHeight();
	yIndent = coll.get_Item(1).getRectangle().getLLY(); 
	rightXIndent = pageWidth - textWidth - leftXIndent - 10 ;
	centerXIndent = pageWidth/2 - textWidth/2;
	System.out.println(rightXIndent+"   "+centerXIndent + "   "+leftXIndent+"   "+yIndent+"  "+pageWidth+"  "+textWidth);
	initialXIndent = coll.get_Item(1).getPosition().getXIndent();
	if(Math.round(initialXIndent) == Math.round(leftXIndent))
	{
		initialAlign = 1;
		System.out.println("leftxIndent "+leftXIndent+"  "+initialAlign);
	}
	else if(Math.round(initialXIndent) == Math.round(centerXIndent))
	{
		initialAlign = 2;
		System.out.println("centerXindent "+centerXIndent+"  "+initialAlign);
	}
	else if(Math.round(initialXIndent) == Math.round(rightXIndent))
	{
		initialAlign = 3;
		System.out.println("rightXIndent "+rightXIndent+"  "+initialAlign);
	}
	System.out.println("initial align is "+initialAlign+"  "+initialXIndent);
	System.out.println(coll.get_Item(1).getBaselinePosition().getXIndent());
	//System.out.println(new FontRepository().);
}

public void deleteText() 
{

// absorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
// float font = coll.get_Item(1).getTextState().getFontSize();
coll.get_Item(1).setText("");
TextFragment textFragment = new TextFragment("");
Position position = new Position(leftXIndent, yIndent);
textFragment.setPosition(position);
TextBuilder builder = new TextBuilder(document.getPages().get_Item(1));
builder.appendText(textFragment);
save();
}

public void setAlignment(int align)
{
	if(align == 4)
	{
		coll.get_Item(1).setText("");
		TextFragment fragment = new TextFragment(text);
		fragment.setPosition(new Position(leftXIndent, yIndent));
		fragment.getTextState().setFontSize(fontSize);
		fragment.getTextState().setFontStyle(fontStyle);
		fragment.getTextState().setFont(font);
		TextBuilder builder = new TextBuilder(document.getPages().get_Item(3));
		builder.appendText(fragment);
	}
	else if(align == 5)
	{
		coll.get_Item(1).setText("");
		TextFragment fragment = new TextFragment(text);
		fragment.setPosition(new Position(centerXIndent, yIndent));
		fragment.getTextState().setFontSize(fontSize);
		fragment.getTextState().setFontStyle(fontStyle);
		fragment.getTextState().setFont(font);
		TextBuilder builder = new TextBuilder(document.getPages().get_Item(1));
		
		builder.appendText(fragment);
	}
	else if(align == 6)
	{
		coll.get_Item(1).setText("");
		TextFragment fragment = coll.get_Item(1);
		fragment.setPosition(new Position(rightXIndent, yIndent));
		fragment.getTextState().setFontSize(fontSize);
		fragment.getTextState().setFontStyle(fontStyle);
		fragment.getTextState().setFont(font);
		TextBuilder builder = new TextBuilder(document.getPages().get_Item(1));
		System.out.println(rightXIndent+"   "+yIndent);
		builder.appendText(fragment);
	}
	save();
}

public void setFontSize(float size)
{
	coll.get_Item(1).setText("");
	TextFragment fragment = new TextFragment(text);
	fragment.getTextState().setFontSize(size);
	fragment.getTextState().setFontStyle(coll.get_Item(1).getTextState().getFontStyle());
	fragment.setPosition(new Position(coll.get_Item(1).getPosition().getXIndent(), yIndent));
	fragment.getTextState().setFontStyle(fontStyle);
	fragment.getTextState().setFont(font);
	TextBuilder builder = new TextBuilder(document.getPages().get_Item(1));
	builder.appendText(fragment);
	save();
	setAlignment(initialAlign);
}

public void setFont(Font font)
{
	coll.get_Item(1).setText("");
	TextFragment fragment = new TextFragment(text);
	System.out.println("Previous bold state is"+(coll.get_Item(1).getTextState().getFontStyle()));
	fragment.getTextState().setFont(font);
	fragment.getTextState().setFontSize(fontSize);
	fragment.getTextState().setFontStyle(fontStyle);
	fragment.getTextState().setFont(font);
	fragment.setPosition(new Position(coll.get_Item(1).getPosition().getXIndent(), yIndent));
	TextBuilder builder = new TextBuilder(document.getPages().get_Item(1));
	builder.appendText(fragment);
	save();
}

public void save()
{
	document.save();
	document = new Document("sample.pdf");
	absorber = new TextFragmentAbsorber(text);
	absorber.getTextSearchOptions().setLimitToPageBounds(true);
	document.getPages().get_Item(1).accept(absorber);
	coll = absorber.getTextFragments();
	if(coll.size() != 0)
	{
		fontSize = coll.get_Item(1).getTextState().getFontSize();
		fontStyle = coll.get_Item(1).getTextState().getFontStyle();
		rectangle = coll.get_Item(1).getRectangle();
		textWidth = rectangle.getWidth();
		pageWidth = document.getPages().get_Item(1).getRect().getWidth();
		textHeight = coll.get_Item(1).getRectangle().getHeight();
		yIndent = coll.get_Item(1).getRectangle().getLLY(); 
		rightXIndent = pageWidth - textWidth - leftXIndent;
		centerXIndent = pageWidth/2 - textWidth/2;
		System.out.println(rightXIndent+"   "+centerXIndent + "   "+leftXIndent+"   "+yIndent+"  "+pageWidth);
		System.out.println(fontSize+"  fontSize");
		initialXIndent = coll.get_Item(1).getRectangle().getLLX();
		if(Math.round(initialXIndent) == Math.round(leftXIndent))
		{
			initialAlign = 1;
			System.out.println("leftXindent "+leftXIndent+"  "+initialAlign);
		}
		else if(Math.round(initialXIndent) == Math.round(centerXIndent))
		{
			initialAlign = 2;
			System.out.println("centerXIndent "+centerXIndent+"  "+initialAlign);
		}
		else if(Math.round(initialXIndent) == Math.round(rightXIndent)-Math.round(leftXIndent))
		{
			initialAlign = 3;
			System.out.println("rightXIndent"+rightXIndent+"  "+initialAlign);
		}
		System.out.println("initial align is "+initialAlign+"  "+initialXIndent);
		//System.out.println((initialXIndent == leftXIndent)+"   "+ (initialXIndent == centerXIndent) + "    "+(initialXIndent == rightXIndent));
	}
}

public void positionTextInOrder()
{
	
}

public static void main(String args[])
{
	Scanner scan = new Scanner(System.in);
	try
	{
		
		AsposePDFEditor asposePDFEditor = new AsposePDFEditor("sample text");
		while(true)
		{
			System.out.println("Enter 1 to delete text\nEnter 4 to set text to left\nEnter 5 to set text to center\nEnter 6 to set text to right");			
			int function = scan.nextInt();
			
			if(function == 1)
			{
				asposePDFEditor.deleteText();
			}
			else if(function == 4 || function ==5 || function ==6)
			{
				asposePDFEditor.setAlignment(function);
			}
			else
			{
				break;
			}
		}
	}
	catch (Exception e) 
	{
		System.out.println(e.getMessage());
	}
}

}

@aviral

I request you to share the source file and desired output. We will be able to investigate that on our end on provision of requested information.