Extract the center-aligned lines from a PDF document using itext7 in C#.Net application - asp.net-core

anybody help me extract the center-aligned lines from a PDF document using itext7 in .Net Core application.
I have written the following extraction code so far, but cannot get the lines that are center aligned. Is there any way, please help
private async Task Extruct()
{
_pdfFile = await UploadedFilesService.GetUploadedPdfById(entryId);
MemoryStream stream = new MemoryStream(_pdfFile);
using (PdfReader pdfReader = new PdfReader(stream))
{
using (PdfDocument pdfDocument = new PdfDocument(pdfReader))
{
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
{
PdfPage page = pdfDocument.GetPage(i);
string pageText = PdfTextExtractor.GetTextFromPage(page);
string[] lineTexts = pageText.Split('\n');
foreach (string lineText in lineTexts)
{
float lineWidth = lineText.Length;
float pageWidth = page.GetPageSize().GetWidth();
if ((pageWidth / 2 - lineWidth * 10) > 20)
{
persons.Add(lineText);
}
}
}
extracted = true;
}
}
this.StateHasChanged();
}

Related

Using Drawing to create a graphic verification code does not display

I am developing an API tool for generating verification codes. I use the Drawing package. I have found some methods, but why can't I generate the graphic verification codes I need? Where am I doing wrong?
public void Output(HttpResponse objHttpResponse)
{
using (Bitmap bitmap = this.GetImage())
{
if (bitmap != null)
{
using (MemoryStream ms= new MemoryStream())
{
bitmap .Save(ms, ImageFormat.Jpeg);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Jpeg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
}
I have a complete code for generating verification code using Bitmap, you can refer to the following:
Controller:
[ApiController]
public class CaptchaController : Controller
{
[Route("get_captcha")]
public Object VerifyCode()
{
string code = "";
Bitmap bitmap = Captcha.CreateCaptcha(out code);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Gif);
return File(stream.ToArray(), "image/gif");
}
}
API to generate verification code:
public class Captcha
{
public static Bitmap CreateCaptcha(out string code)
{
//Create a Bitmap object and draw
Bitmap bitmap = new Bitmap(200, 60);
Graphics graph = Graphics.FromImage(bitmap);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "0123456789";
StringBuilder sb = new StringBuilder();
//Add random 4 numbers
for (int x = 0; x < 4; x++)
{
string letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
sb.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
}
code = sb.ToString();
//Confuse the background
Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
return bitmap;
}
}
Result:

Generate charts, graphs in PDF using iTextSharp in ASP.NET Core

I need to generate a PDF document at the server side of a ASP.NET Core 3.1. I am using iTextSharp to generate PDF. I am able to create data tables in PDF using iTextSharp. However I am not able to find a solution to add charts or graphs into the PDF using the same library.
I have used ChartJS and other similar packages but none of them let me create a chart at the backend and create it as an image to be embedded in the PDF.
UPDATE 8/12/2020
Create excel then convert to pdf.
Essential XlsIO is a native .NET class librry that can be used to create and modify Microsoft Excel files.
XlsIO allows you to convert an entire workbook or a single worksheet into PDF document.
[Route("/chart")]
public IActionResult CreateChart()
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
//Inserts the sample data for the chart
sheet.Range["A1"].Text = "Month";
sheet.Range["B1"].Text = "Product A";
sheet.Range["C1"].Text = "Product B";
//Months
sheet.Range["A2"].Text = "Jan";
sheet.Range["A3"].Text = "Feb";
sheet.Range["A4"].Text = "Mar";
sheet.Range["A5"].Text = "Apr";
sheet.Range["A6"].Text = "May";
//Create a random Data
Random r = new Random();
for (int i = 2; i <= 6; i++)
{
for (int j = 2; j <= 3; j++)
{
sheet.Range[i, j].Number = r.Next(0, 500);
}
}
IChartShape chart = sheet.Charts.Add();
//Set chart type
chart.ChartType = ExcelChartType.Line;
//Set Chart Title
chart.ChartTitle = "Product Sales comparison";
//Set first serie
IChartSerie productA = chart.Series.Add("ProductA");
productA.Values = sheet.Range["B2:B6"];
productA.CategoryLabels = sheet.Range["A2:A6"];
//Set second serie
IChartSerie productB = chart.Series.Add("ProductB");
productB.Values = sheet.Range["C2:C6"];
productB.CategoryLabels = sheet.Range["A2:A6"];
//Saving the workbook as stream
//FileStream stream = new FileStream("Chart.xlsx", FileMode.Create, FileAccess.ReadWrite);
//workbook.SaveAs(stream);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
Syncfusion.Pdf.PdfDocument pdfDocument = renderer.ConvertToPDF(sheet);
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
//stream.Dispose();
return File(stream, "application/pdf", "chart.pdf");
}
}
Generate PDF with Image by using iTextsharp in ASPNET Core 3.1
Here are the codes of action.
[Route("/pdf")]
public FileStreamResult GeneratePDFwithImage()
{
var imagepath = System.IO.Path.Combine(_env.WebRootPath, "/images") + "/test.png";
Document doc = new Document();
MemoryStream stream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, stream);
pdfWriter.CloseStream = false;
doc.Open();
doc.Add(new Paragraph("Hello World"));
Image png = Image.GetInstance(imagepath);
doc.Add(png);
doc.Close();
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
return File(stream, "application/pdf", "HelloWorld.pdf");
}
Test of result

Change PDF Annotation font size using itext 7

My question is a bit similar to this one : Change PDF Annotation properties using iTextSharp C#
But I want to specifically change font size of pdf annotation using iText 7. I have searched a lot online but haven't been able to find any great examples or documentation regarding this. Following is the code I have used.
static void EditAnnot(string PDF)
{
string OutPDF = #"C:\Users\AP037X\Desktop\test.pdf";
iText.Kernel.Pdf.PdfDocument pdfDoc = new iText.Kernel.Pdf.PdfDocument(new iText.Kernel.Pdf.PdfReader(PDF), new iText.Kernel.Pdf.PdfWriter(OutPDF));
iText.Kernel.Pdf.PdfDictionary pageDict = pdfDoc.GetPage(1).GetPdfObject();
iText.Kernel.Pdf.PdfArray annots = pageDict.GetAsArray(iText.Kernel.Pdf.PdfName.Annots);
if (annots != null)
{
for (int i = 0; i < annots.Size(); i++)
{
Console.WriteLine("Scan..");
if (annots.GetAsDictionary(i) == null)
{
Console.WriteLine("1");
//return;
}
iText.Kernel.Pdf.PdfString t = annots.GetAsDictionary(i).GetAsString(iText.Kernel.Pdf.PdfName.Contents);
if (t == null)
{
Console.WriteLine("2");
//return;
}
Console.WriteLine(t);
if (Convert.ToString(t).Trim() == "Change")
{
Console.WriteLine("Found");
Console.WriteLine(annots.Size());
iText.Kernel.Geom.Rectangle rect = annots.GetAsDictionary(i).GetAsRectangle(iText.Kernel.Pdf.PdfName.Rect);
iText.Kernel.Pdf.PdfString cont = new iText.Kernel.Pdf.PdfString("New String");
iText.Kernel.Pdf.Annot.PdfFreeTextAnnotation NewAnnot = new iText.Kernel.Pdf.Annot.PdfFreeTextAnnotation(rect,cont);
float[] color = { 1f,1f,0f};
NewAnnot.SetColor(color);
NewAnnot.Put(iText.Kernel.Pdf.PdfName.Contents, new iText.Kernel.Pdf.PdfString("lion"));
NewAnnot.Put(iText.Kernel.Pdf.PdfName.Font, *What to type here?*);
annots.Remove(i);
annots.Add(i, NewAnnot.GetPdfObject());
}
}
}
pdfDoc.Close();
CompressPDF(OutPDF);
}

Merging PDFs using iTextSharp removes Trim Box Detail

I am trying to use iTextSharp to merge 2 or more PDF files. However I am not getting any details about the TrimBox. Performing the code below on the PDF (which was merged) always return NULL
Rectangle rect = reader.GetBoxSize(1, "trim");
This is the code for merging.
public void Merge(List<String> InFiles, String OutFile)
{
using (FileStream stream = new FileStream(OutFile, FileMode.Create))
using (Document doc = new Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
InFiles.ForEach(file =>
{
reader = new PdfReader(file);
for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}
pdf.FreeReader(reader);
reader.Close();
});
}
}
How to keep I keep the box information after the merge?
-Alan-
Here is the code I created to merge Portrait and Landscape docs using iTextSharp. It works rather well.
public void MergeFiles(System.Collections.Generic.List<string> sourceFiles, string destinationFile)
{
Document document=null;
if (System.IO.File.Exists(destinationFile))
System.IO.File.Delete(destinationFile);
try
{
PdfCopy writer = null;
int numberOfPages=0;
foreach(string sourceFile in sourceFiles)
{
PdfReader reader = new PdfReader(sourceFile);
reader.ConsolidateNamedDestinations();
numberOfPages = reader.NumberOfPages;
if(document==null)
{
document = new Document(reader.GetPageSizeWithRotation(1));
writer = new PdfCopy(document, new FileStream(destinationFile, FileMode.Create));
document.Open();
}
for (int x = 1;x <= numberOfPages;x++ )
{
if (writer != null)
{
PdfImportedPage page = writer.GetImportedPage(reader, x);
writer.AddPage(page);
}
}
PRAcroForm form = reader.AcroForm;
if (form != null && writer != null)
writer.CopyAcroForm(reader);
}
}
finally
{
if (document != null && document.IsOpen())
document.Close();
}
}

How can I copy annotations/highlighting from one PDF to an updated version of that PDF?

I've recently moved to almost exclusively electronic books. I prefer to mark up documents with highlighting or annotations as I read them.
However, when I get an updated version of a PDF - O'Reilly, for example, will give access to corrected versions of the books you've purchased - I'm then stuck with a marked up older copy and a newer copy, without my notes.
My preferred language being C# I realize that iTextSharp is probably what I'd need to use if I wanted to programmatically do this (see for example Copy pdf annotations via C#), but is there an easier way to handle this?
I can't believe I'm the only one with this issue, so is there perhaps already a solution that will handle this for me?
You can use this example for iTextSharp to approach your problem:
var output = new MemoryStream();
using (var document = new Document(PageSize.A4, 70f, 70f, 20f, 20f))
{
var readers = new List<PdfReader>();
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
document.Open();
const Int32 requiredWidth = 500;
const Int32 zeroBottom = 647;
const Int32 left = 50;
Action<String, Action> inlcudePdfInDocument = (filename, e) =>
{
var reader = new PdfReader(filename);
readers.Add(reader);
var pageCount = reader.NumberOfPages;
for (var i = 0; i < pageCount; i++)
{
e?.Invoke();
var imp = writer.GetImportedPage(reader, (i + 1));
var scale = requiredWidth / imp.Width;
var height = imp.Height * scale;
writer.DirectContent.AddTemplate(imp, scale, 0, 0, scale, left, zeroBottom - height);
var annots = reader.GetPageN(i + 1).GetAsArray(PdfName.ANNOTS);
if (annots != null && annots.Size != 0)
{
foreach (var a in annots)
{
var newannot = new PdfAnnotation(writer, new Rectangle(0, 0));
var annotObj = (PdfDictionary) PdfReader.GetPdfObject(a);
newannot.PutAll(annotObj);
var rect = newannot.GetAsArray(PdfName.RECT);
rect[0] = new PdfNumber(((PdfNumber)rect[0]).DoubleValue * scale + left); // Left
rect[1] = new PdfNumber(((PdfNumber)rect[1]).DoubleValue * scale); // top
rect[2] = new PdfNumber(((PdfNumber)rect[2]).DoubleValue * scale + left); // right
rect[3] = new PdfNumber(((PdfNumber)rect[3]).DoubleValue * scale); // bottom
writer.AddAnnotation(newannot);
}
}
document.NewPage();
}
}
foreach (var apprPdf in pdfs)
{
document.NewPage();
inlcudePdfInDocument(apprPdf.Pdf, null);
}
document.Close();
readers.ForEach(x => x.Close());
}
output.Position = 0;
return output;
This example copies a list of pdf files with annotations into a new pdf file.
Obtain data from two PdfReaders simultaneously - one for copying new pdf and another for copying annotations from old pdf.