Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在标题和段落之间使用itextsharp进行替换_C#_Asp.net_Pdf Generation_Itextsharp - Fatal编程技术网

C# 在标题和段落之间使用itextsharp进行替换

C# 在标题和段落之间使用itextsharp进行替换,c#,asp.net,pdf-generation,itextsharp,C#,Asp.net,Pdf Generation,Itextsharp,我正试图用itextsharp编写pdf文档,但我不想在两种样式之间交替:标题和段落 我试过这样的方法: Paragraph title= new Paragraph(); title.Alignment = Element.ALIGN_CENTER; title.Font = FontFactory.GetFont("Arial", 32); title.Add("\nTitle 1\n\n"); pdfDoc.Add(title); Paragraph paragraph = new P

我正试图用itextsharp编写pdf文档,但我不想在两种样式之间交替:标题和段落

我试过这样的方法:

Paragraph title= new Paragraph();
title.Alignment = Element.ALIGN_CENTER;
title.Font = FontFactory.GetFont("Arial", 32);
title.Add("\nTitle 1\n\n");
pdfDoc.Add(title);


Paragraph paragraph = new Paragraph();
paragraph.Alignment = Element.ALIGN_LEFT;
paragraph.Font = FontFactory.GetFont("Arial", 12);
paragraph.Add("Lorem ipsum dolor sit amet \n");
pdfDoc.Add(paragraph);


title.Add("\nTitle 2\n\n");
pdfDoc.Add(title);
paragraph.Add("Consectetur adipiscing elit \n");
pdfDoc.Add(paragraph);
Font titleFont = FontFactory.GetFont("Arial", 32);
Font regularFont = FontFactory.GetFont("Arial", 36);
Paragraph title;
Paragraph text;
title = new Paragraph("Title", titleFont);
title.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(title);
text = new Paragraph("Lorem ipsum dolor sit amet", regularFont);
pdfDoc.Add(text);
title = new Paragraph("Title 2", titleFont);
title.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(title);
text = new Paragraph("Consectetur adipiscing elit", regularFont);
pdfDoc.Add(text);

但看起来我需要在添加新文本之前清除内容。是否可以只清除文本,还是必须为每个副图创建新变量?希望不是……

解决问题的一种方法是如下更改代码:

Paragraph title= new Paragraph();
title.Alignment = Element.ALIGN_CENTER;
title.Font = FontFactory.GetFont("Arial", 32);
title.Add("\nTitle 1\n\n");
pdfDoc.Add(title);


Paragraph paragraph = new Paragraph();
paragraph.Alignment = Element.ALIGN_LEFT;
paragraph.Font = FontFactory.GetFont("Arial", 12);
paragraph.Add("Lorem ipsum dolor sit amet \n");
pdfDoc.Add(paragraph);


title.Add("\nTitle 2\n\n");
pdfDoc.Add(title);
paragraph.Add("Consectetur adipiscing elit \n");
pdfDoc.Add(paragraph);
Font titleFont = FontFactory.GetFont("Arial", 32);
Font regularFont = FontFactory.GetFont("Arial", 36);
Paragraph title;
Paragraph text;
title = new Paragraph("Title", titleFont);
title.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(title);
text = new Paragraph("Lorem ipsum dolor sit amet", regularFont);
pdfDoc.Add(text);
title = new Paragraph("Title 2", titleFont);
title.Alignment = Element.ALIGN_CENTER;
pdfDoc.Add(title);
text = new Paragraph("Consectetur adipiscing elit", regularFont);
pdfDoc.Add(text);
如果内容在添加文本后消失,这将被认为是一个严重的错误。这意味着曾经使用过的对象不能再被重用。简言之:在你的问题中,你要求iTextSharp的开发人员引入一个bug

通常,创建一个单独的类来定义所使用的所有
Font
对象。使用自定义方法(如
getTitle()
getText()
)等)创建一个类似于类的帮助器类也是一个好主意(请注意,POJO代表普通的旧Java对象。原始代码是用Java编写的,由…编写)


使用
\n
引入换行符是一个不好的想法。为什么不使用前导、前后间距等属性来定义行之间的空白?

它可以按照我的要求工作,谢谢!我使用\n是因为我还在学习,而且我什么都不知道,但我会尝试用前后间距来替换它,它看起来更干净。