2025-02-26 08:20:12 -07:00

56 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace PDF_Text_Stripper;
public class Program
{
private static void PDFTextStripper(string file)
{
string altFileName = Path.ChangeExtension(file, ".txt");
if(File.Exists(altFileName))
File.Delete(altFileName);
org.apache.pdfbox.pdmodel.PDDocument pdfDocument = org.apache.pdfbox.pdmodel.PDDocument.load(file);
org.apache.pdfbox.util.PDFTextStripper stripper = new();
string text = stripper.getText(pdfDocument);
pdfDocument.close();
File.WriteAllText(altFileName, text);
}
public static void Secondary(List<string> args)
{
int silentIndex = args.IndexOf("s");
if (silentIndex > -1)
args.RemoveAt(silentIndex);
try
{
if (args.Any() && File.Exists(args[0]))
PDFTextStripper(args[0]);
else
throw new Exception(args[0]);
}
catch (Exception ex)
{
Console.WriteLine(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace));
}
if (silentIndex > -1)
Console.WriteLine("Done. Bye");
else
{
Console.WriteLine("Done. Press 'Enter' to end");
_ = Console.ReadLine();
}
}
public static void Main(string[] args)
{
if (args is not null)
Secondary(args.ToList());
else
Secondary(new List<string>());
}
}