C# iText7 PDF

Przykłady iText: https://kb.itextpdf.com/itext/examples

Biblioteka w NuGet : itext7 oraz itext7.bouncy-castle-adapter

Dodawanie paragrafów z polską czcionką

using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace Test_wpf
{
    class PdfCreator
    {
        public PdfCreator()
        {
            const string PATH = @"pdf/mypdf.pdf";
            const string FONT = "Roboto-Black.ttf";
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(PATH));
            Document doc = new Document(pdfDoc);
            PdfFont f = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            doc.SetFont(f);
            Paragraph header = new Paragraph("iText7 Pdf").SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).SetFontSize(26);
            doc.Add(header);

            Paragraph par = new Paragraph("Test polskich znaków: ");
            par.Add(new Text("ążćłóśź"));
            doc.Add(par);

            doc.Close();
        }
    }
}

Dodawanie grafik do pdf

using iText.IO.Font;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Element;

namespace Test_wpf
{
    class PdfCreator
    {
        public PdfCreator()
        {
            const string PATH = @"pdf/mypdf.pdf";
            const string FONT = "Roboto-Black.ttf";
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(PATH));
            Document doc = new Document(pdfDoc);
            PdfFont f = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            doc.SetFont(f);
            
            Paragraph header = new Paragraph("Hello").SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).SetFontSize(26);
            doc.Add(header);
            
            LineSeparator s = new LineSeparator(new SolidLine());
            doc.Add(s);

            Image img = new Image(ImageDataFactory
            .Create("car1.jpg"))
            .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
            doc.Add(img);

            doc.Close();
        }
    }
}

// Wywołanie kodu w skrypcie

PdfCreator pdf = new();

Tabela w pdf

 // Table
Table table = new Table(2, false);
 Cell cell11 = new Cell(1, 1)
    .SetBackgroundColor(ColorConstants.GRAY)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("State"));
 Cell cell12 = new Cell(1, 1)
    .SetBackgroundColor(ColorConstants.GRAY)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("Capital"));

 Cell cell21 = new Cell(1, 1)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("New York"));
 Cell cell22 = new Cell(1, 1)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("Albany"));

 Cell cell31 = new Cell(1, 1)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("New Jersey"));
 Cell cell32 = new Cell(1, 1)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("Trenton"));

 Cell cell41 = new Cell(1, 1)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("California"));
 Cell cell42 = new Cell(1, 1)
    .SetTextAlignment(TextAlignment.CENTER)
    .Add(new Paragraph("Sacramento"));

 table.AddCell(cell11);
 table.AddCell(cell12);
 table.AddCell(cell21);
 table.AddCell(cell22);
 table.AddCell(cell31);
 table.AddCell(cell32);
 table.AddCell(cell41);
 table.AddCell(cell42);
 document.Add(table);

Szybka tabela z użyciem pętli

  string path = @"pdf/mypdf.pdf";
  PdfDocument pdfDoc = new PdfDocument(new PdfWriter(path));
  Document doc = new Document(pdfDoc);
  Table table = new Table(UnitValue.CreatePercentArray(8)).UseAllAvailableWidth();
  for (int i = 0; i < 16; i++)
  {
      table.AddCell("hi");
  }
  doc.Add(table);
  doc.Close();

Odnośnik w pdf

// Hyper link
Link link = new Link("click here", PdfAction.CreateURI("https://www.google.com"));
Paragraph hyperLink = new Paragraph("Please ")
   .Add(link.SetBold().SetUnderline()
   .SetItalic().SetFontColor(ColorConstants.BLUE))
   .Add(" to go www.google.com.");
document.Add(hyperLink);

Numery stron

  // Page numbers
  int n = pdf.GetNumberOfPages();
  for (int i = 1; i <= n; i++)
  {
      document.ShowTextAligned(new Paragraph(String
         .Format("page" + i + " of " + n)),
          559, 806, i, TextAlignment.RIGHT,
          VerticalAlignment.TOP, 0);
  }

Scroll to Top