每次迭代或遍历文档列表时如何创建新的 PDF 文件?
How do I create new PDF file every time i iterate or loop through the documents list?
我是 PDF 创建的新手,我正在按照现有代码创建 pdf 文件。我正在通过创建新的 pdf 来修改现有代码。
从文档列表中,我正在遍历每个文档并为其指定一个新名称。
如何在每次迭代或循环访问文档列表时创建一个新的 PDF 文件?
resultCollection - 获取文档列表
currentCompanySegmentsSetings - 具有详细信息的对象
creatingTableOfContent - table 内容
我试过的
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()
});
}
您正在使用:
var firstDocsMetadata = resultCollection.First().MetaData;
什么时候应该是:
var firstDocsMetadata = item.MetaData;
由于您正在迭代 resultCollection
并且 item
是在每个循环中获得的元素。
我假设您想为 resultCollection
中的每个 item
创建一个单独的 PDF 文件。
您已经为每个文件生成了一个文件名:
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
循环的下一次迭代将生成不同的文件名,因此下一个文档将写入不同的文件。
我是 PDF 创建的新手,我正在按照现有代码创建 pdf 文件。我正在通过创建新的 pdf 来修改现有代码。
从文档列表中,我正在遍历每个文档并为其指定一个新名称。
如何在每次迭代或循环访问文档列表时创建一个新的 PDF 文件?
resultCollection - 获取文档列表
currentCompanySegmentsSetings - 具有详细信息的对象
creatingTableOfContent - table 内容
我试过的
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()
});
}
您正在使用:
var firstDocsMetadata = resultCollection.First().MetaData;
什么时候应该是:
var firstDocsMetadata = item.MetaData;
由于您正在迭代 resultCollection
并且 item
是在每个循环中获得的元素。
我假设您想为 resultCollection
中的每个 item
创建一个单独的 PDF 文件。
您已经为每个文件生成了一个文件名:
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
循环的下一次迭代将生成不同的文件名,因此下一个文档将写入不同的文件。