Tutorials

Convert PDF to TXT in Objective-C, Kotlin, and C#

By ComPDFKit | Thu. 14 Nov. 2024
Conversion

As we all know, PDF is a universal file format due to its flexibility and portability. However, it's troublesome for us to process PDF documents like editing and searching without certain applications. In fact, processing text from PDF files is an extremely common request. To solve this problem, you can convert PDF to TXT.

 

 

What Is TXT?

 

Generally speaking, a TXT file consists of plain text only and stripping away formatting and special elements such as font, font size, color, bold, italic, underline, strikethrough, and alignment. TXT really is the plainest format of text files, including line breaks only. 

 

A TXT file can be opened by any text editor or word-processing application on different platforms and operating systems. For example, Windows OS comes with Notepad and Wordpad. Similarly, macOS comes with TextEdit for creating and editing TXT Documents. So TXT is still widely used even though it is one of the oldest file formats in existence. TXT format remains popular in the long run and will never be eliminated.

 

 

Strengths & Weaknesses of TXT

 

Strengths

- Most operating systems have default applications for opening and editing TXT files, so heavy application suites are not required.

- TXT files have very small sizes. They can contain plenty of details within KBs.

- TXT files can easily be shared on the internet via email or other methods.

 

Weaknesses

- TXT files can’t support images, hyperlinks, chapter division, etc.

 

 

How to Convert

 

Objective-C

// Get the path of the PDF file.
NSString *pdfPath = @"...";
// Get the path to the TXT file.
NSString *outputPath = @"...";

CPDFConverterTxt *converter = [[[CPDFConverterTxt alloc] initWithURL:[NSURL fileURLWithPath:pdfPath] password:nil] autorelease];
[converter convertToFilePath:outputPath pageIndexs:nil options:nil];

 

Kotlin

val cPDFConvert = CPDFConverterTxt(context, uri, "")
val params = CPDFConvertTxtOptions()
val result: ConvertError = cPDFConvert.convert(outputDir, outputfilename, params, pageArrays, 
onHandle = onHandleCal, 
onProgress = onProgressCal, 
onPost = onPostCal)

 

C#

string resPath = "***";
string inputFilePath = "***";
string outputFolderPath = "***";
string outputFileName = "***";

CPDFConverter.Init(resPath);
CPDFConverterTxt converter = new CPDFConverterTxt(inputFilePath);

int pageCount = converter.GetPagesCount();
int[] pageArray = new int[pageCount];
for (int i = 0; i < pageArray.Length; i++)
{
    pageArray[i] = i + 1;
}

CPDFConverterError error = CPDFConverterError.C_UNKNOWN;
converter.Convert(outputFolderPath,ref outputFileName, pageArray, ref error, getPorgress);

 

 

Conclusion

 

Now we've learned about a new conversion feature. If you want to read more about our ComPDFKit Conversion SDK and what you can do with it, please check out our guides.