0

I use the ItextSharp-LGPL-Core Nuget Package by Ben Meredith, also getting the same behaviour with iTextSharp.LGPLv2.Core by Vahid Nasiri, so I suppose the issue might be in my local environment.

The PDF generates fine up to the point where I print the first line or rectangle. Thereafter, none of the text output is visible in the document anymore. If I comment the line and rectangle code out, the text outputs fine, but I also have to generate the lines and rectangles. I noticed the issue first after an operating system re-install (Laptop crash), and Visual Studio re-install. The issue is visible on my laptop with Visual Studio Version 17.13.0, on Windows 11 Pro 10.0.22631 Build 22631. When I build the project in our devops pipeline for x64 and running on Windows Server 2022, the PDF still shows OK (for now???). I noticed the issue while working on code targeting .NET 8, but this console example had the same issue in a VS project being set to target .NET 6.

So my issue is to generate the PDF locally in a VS debug session. I am also concerned that some environmental change might spill the problem over to the server (production) as well. Below is a Hello World app illustrating the problem

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

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {            
            Program p=new Program();            
            p.CreatePDFDocument("Hello World");            
        }

        string destinationDirectory = "c:\\temp\\";
        string filename = "";
        public void CreatePDFDocument(string input)
        {
            if (!Directory.Exists(destinationDirectory))
            {
                Directory.CreateDirectory(destinationDirectory);
            }
            filename = destinationDirectory + "test_pdf.pdf";

            System.IO.FileStream fs = new FileStream(filename, FileMode.Create);
            Document document = new Document(PageSize.A4, 25, 25, 30, 30);  
            
            PdfWriter writer = PdfWriter.GetInstance(document, fs);              
            document.Open();              
            document.Add(new Paragraph("Hello World!"));

            // Some initializing of fonts...
            BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);    
            BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 

            // Enable exact positioning
            PdfContentByte cb = writer.DirectContent;             
            cb.BeginText();                 
            cb.SetFontAndSize(f_cn, 9);     
            cb.SetTextMatrix(20, 700); 
            cb.ShowText("Hello exact position");   
            
            // Lines:
            cb.SetLineWidth(0f);
            cb.MoveTo(30, 650);
            cb.LineTo(570, 650);
            cb.Stroke(); 
            // Comment line code out, then this text is visible in PDF. Execute line code, then this text do not show in PDF
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This text is left aligned", 200, 800, 0);  
    
            // Rectangle
            cb.Rectangle(300, 600, 100, 100);
            cb.Stroke(); 
            // Comment both line and rectangle out, then this text will show in the PDF
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "This text is right aligned", 200, 788, 0);
             
            //Colour
            cb.SetCMYKColorStroke(255, 255, 0, 0);
            cb.SetCMYKColorFill(0, 255, 255, 0);
            cb.SetLineWidth(2f);
            cb.Circle(120f, 250f, 50f);
            cb.Fill();

            cb.EndText();  

            document.Close();  
            writer.Close();  
            fs.Close(); 

            Console.WriteLine("PDF created successfully.");
        }
       
 
    }
}
3
  • 1
    Please take a look at Figure 9 — Graphics objects — in the PDF spec ISO 32000-2: path construction and drawing are not allowed inside text objects. I cannot tell whether that's causing your current issue, but drawing the lines inside text objects clearly is an error in your code. Commented Mar 29 at 23:16
  • Thanks, mkl, I surrounded each text line with BeginText() and EndText() [removing the BeginText and EndText at the atart and end of the program] and the text now shows as expected. I think this answers my question. Commented Mar 31 at 6:58
  • 1
    Great! One remark, though: In general you do not need to surround each text line with BeginText() and EndText(). If you draw two text lines in sequence, you can have them in the same text object. Merely if you use PDF instructions not allowed in a text object according to the referred-to figure in-between, you need separate text objects. Commented Mar 31 at 13:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.