最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

itext - ItextSharp-LGPL-Core not printing text after graphics to PDF output - Stack Overflow

programmeradmin0浏览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.");
        }
       
 
    }
}
发布评论

评论列表(0)

  1. 暂无评论