我是创建PDF的新手,并且遵循现有代码创建pdf文件。我正在通过创建新的pdf修改现有代码。
从文档列表中,我遍历每个文档并为其赋予新的名称。
每次迭代或遍历文档列表时如何创建新的PDF文件?
resultCollection-获得文档列表
currentCompanySegmentsSetings-具有详细信息的对象
creationTableOfContent-表内容
我尝试过的
foreach (var item in resultCollection)
{
var guidID = Guid.NewGuid().ToString();
var newFileName = $"{currentCompanySegmentsSetings.FriendlySegmentName}-{Translator.TranslateDocumentType("invoice", currentCompanySegmentsSetings).ToLower()}-{guidID}-{message.documents.First().AccountNumber}.pdf";
outputFileNames.Add(newFileName);
//Create PDF's and send to the location
System.IO.Directory.CreateDirectory(currentOutputDirectory);
var firstDocsMetadata = resultCollection.First().MetaData;
string generatedPDFLocation = System.IO.Path.Combine(currentOutputDirectory, newFileName);
var file = DocumentsToPDFDocs(financialDocument);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(generatedPDFLocation, CreateEncryptionWriteProperties()));
Document doc = new Document(pdfDoc);
//Bookmarks
pdfDoc.GetCatalog().SetPageMode(PdfName.UseOutlines);
doc.SetMargins(22f, 22f, 22f, 22f);
doc.SetFontSize(8);
doc.SetFontColor(Color.BLACK);
//1.Create table of contents
var tableOfContentTopMargin = 176;
Table tableOfContent = creatingTableOfContent(file, currentCompanySegmentsSetings);
tableOfContent.SetDestination("p" + "index");
doc.Add(tableOfContent.SetMarginTop(tableOfContentTopMargin));
//How do i continue from here to create pdf to the directory
message.FinancialDocumentAttachments.Add(new MessageQueueAttachment()
{
Location = documentPath,
IsNew = true,
Id = Guid.NewGuid()
});
}
我假设你要创建每个单独的PDF文件item
在resultCollection
。
您已经为每个文件生成了文件名:
var newFileName = ...
string generatedPDFLocation = System.IO.Path.Combine(currentOutputDirectory, newFileName);
然后创建一个新实例,PdfDocument
并将Document
其写入具有该文件名的文件中:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(generatedPDFLocation,
CreateEncryptionWriteProperties()));
Document doc = new Document(pdfDoc);
然后只需将内容添加到Document
实例(或PdfDocument
)。我认为内容在item
或resultCollection
以某种方式包含在其中。
doc.add(new Paragraph("content for this document"));
最后,关闭文档,将PDF文件刷新到磁盘。
doc.close();
foreach
循环的下一次迭代将生成不同的文件名,因此下一个文档将被写入不同的文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句