How to Generate PDF in C# (.NET 6) using PDFSharp

    To generate a PDF in C# using PDFSharp library in .NET 6, follow these steps:
    • Install the PDFSharp library:
    • Right-click on the project in Solution Explorer and select "Manage NuGet Packages".
    • Search for "PDFSharp" and click on "Install" to add the library to your project.
  1. Add the necessary using statements at the top of your .cs file: using PdfSharp.Drawing; using PdfSharp.Pdf;
  2. Create a new PDF document:
    PdfDocument document = new PdfDocument();
  3. Add a new page to the document:
    PdfPage page = document.AddPage();
  4. Create a drawing object to draw on the page:
    XGraphics gfx = XGraphics.FromPdfPage(page);
  5. Set the font and size for the text:
    XFont font = new XFont("Arial", 12);
  6. Draw text on the page:
    gfx.DrawString("Hello World!", font, XBrushes.Black, new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);
  7. Save the document to a file or stream:
    document.Save("path/to/file.pdf");
  8. Dispose of the drawing object and document to free resources:
    gfx.Dispose(); document.Dispose();

Here is a complete example:

Here is a complete example:

using PdfSharp.Drawing;
using PdfSharp.Pdf;

class Program
{
    static void Main()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();

        // Add a new page to the document
        PdfPage page = document.AddPage();

        // Create a drawing object to draw on the page
        XGraphics gfx = XGraphics.FromPdfPage(page);

        // Set the font and size for the text
        XFont font = new XFont("Arial", 12);

        // Draw text on the page
        gfx.DrawString("Hello World!", font, XBrushes.Black, new XRect(10, 10, page.Width, page.Height), XStringFormats.TopLeft);

        // Save the document to a file or stream
        document.Save("path/to/file.pdf");

        // Dispose of the drawing object and document
        gfx.Dispose();
        document.Dispose();
    }
}

Replace "path/to/file.pdf" with the desired file path to save the PDF document.

    To read a PDF in C# using iText, you need to follow these steps:
  1. Install iTextSharp library using NuGet Package Manager.
  2. Create an instance of PdfReader class to open the PDF file.
  3. Use the PdfReader object to extract the PDF content and store it in a StringBuilder.
  4. Close the PdfReader object.
  5. Access the extracted content from the StringBuilder.

Here is a complete example:

using System;
using System.Text;
using iTextSharp.text.pdf;

namespace ReadPDFExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "path_to_your_pdf_file.pdf";
            StringBuilder text = new StringBuilder();

            using (PdfReader reader = new PdfReader(path))
            {
                for (int page = 1; page <= reader.NumberOfPages; page++)
                {
                    text.Append(PdfTextExtractor.GetTextFromPage(reader, page));
                }
            }

            Console.WriteLine(text);
        }
    }
}

Replace "path/to/file.pdf" with the desired file path to save the PDF document.

This code reads the PDF file page by page using the PdfReader and PdfTextExtractor classes from iTextSharp library. The extracted text from each page is appended to the StringBuilder object. Finally, the extracted text is printed to the Console.

Note: iTextSharp is a third-party library that is not actively maintained.