Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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向PDF文件添加多个页面_C#_Itext - Fatal编程技术网

C#-使用iTextSharp向PDF文件添加多个页面

C#-使用iTextSharp向PDF文件添加多个页面,c#,itext,C#,Itext,我的代码有问题。我一直在这里查看StackOverflow,但这些示例似乎不适用于我的代码 我一直在尝试生成一个包含多个页面的pdf文件,但我找不到一种方法使其工作 我的意思是,使用现在的代码,它可以生成多个包含正确数据的pdf文件 你能帮我吗 foreach(ListCars.OrderBy(x=>x.Destiny)中的var项) { 文档=新文档(PageSize.A4、0f、0f、15f、0f); 图像Img=null; FileStream fsData=null; Img=Imag

我的代码有问题。我一直在这里查看StackOverflow,但这些示例似乎不适用于我的代码

我一直在尝试生成一个包含多个页面的pdf文件,但我找不到一种方法使其工作

我的意思是,使用现在的代码,它可以生成多个包含正确数据的pdf文件

你能帮我吗

foreach(ListCars.OrderBy(x=>x.Destiny)中的var项)
{
文档=新文档(PageSize.A4、0f、0f、15f、0f);
图像Img=null;
FileStream fsData=null;
Img=Image.GetInstance(Properties.Resources.CMODEL,System.Drawing.Imaging.ImageFormat.Png);
Img.ScaleToFit(页面大小A4);
Img.Alignment=Image.undernative | Image.ALIGN_CENTER;
字符串DataForTest=“”;
字符串PDFName=“TEST-”+item.Vin+”.PDF”;
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@“\Rems\Pages\”;
fsData=newfilestream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@“\Rems\Pages\”+PDFName,FileMode.Create);
PdfWriter writer=PdfWriter.GetInstance(文档,fsData);
Document.Open();
PdfContentByte cb=writer.DirectContent;
ColumnText ct=新的ColumnText(cb);
短语DataForTestT=新短语(DataForTest,FontFactory.GetFont(“IMPACT”,8));
ct.SetSimpleColumn(DataForTestT,115,824,561,307,8,Element.ALIGN_LEFT);
ct.Go();
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@“\Rems\Pages\”+PDFName);
文件。添加(Img);
Document.AddCreationDate();
Document.Close();

}
首先更改
文档文档=新文档(PageSize.A4、0f、0f、15f、0f)
文件文件=新文件(PageSize.A4、0f、0f、15f、0f)

让我们一步一步地把它弄清楚

不能将实例名称设置为类名。 像这样:

 foreach (var item in ListCars.OrderBy(x => x.Destiny))
                {
                    Document document= new Document(PageSize.A4, 0f, 0f, 15f, 0f);
                    Image Img = null;
                    FileStream fsData = null;
                    Img = Image.GetInstance(Properties.Resources.CMODEL, System.Drawing.Imaging.ImageFormat.Png);

                    Img.ScaleToFit(PageSize.A4);
                    Img.Alignment = Image.UNDERLYING | Image.ALIGN_CENTER;
                    string DataForTest = "";
                    string PDFName = "TEST - " + item.Vin + ".PDF";

                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\");
                    fsData = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName, FileMode.Create);
                    PdfWriter writer = PdfWriter.GetInstance(document, fsData);

                    document.Open();

                    PdfContentByte cb = writer.DirectContent;
                    ColumnText ct = new ColumnText(cb);

                    Phrase DataForTestT = new Phrase(DataForTest, FontFactory.GetFont("IMPACT", 8));
                    ct.SetSimpleColumn(DataForTestT, 115, 824, 561, 307, 8, Element.ALIGN_LEFT);
                    ct.Go();

                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName);
                    document.Add(Img);
                    document.AddCreationDate();
                    document.Close();
                }

你能再详细一点吗?我对所有这些东西都是新手。请把错误贴出来。会发生什么?撞车?不添加页面?它不会崩溃,只是完全忽略代码。我现在就照你说的做。啊,我知道你的意思,当我在这里写代码时,这是一个打字错误。另外,不要忘了在必要的地方切换其他
文档。粘贴我发布的内容。不,这只是我在这里写代码时的一个输入错误。别介意。