I am new to PDF creation and I am following the existing code to create a pdf file. I am amending the existing code by creating a new pdf.
From the list of the documents, I am looping through each document and give it a new name.
How do I create a new PDF file every time I iterate or loop through the documents list?
resultCollection - Got the list of the documents
currentCompanySegmentsSetings - An object with the details
creatingTableOfContent - table content
What I have tried
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()
});
}
I assume you want to create a separate PDF file for each item
in resultCollection
.
You already generate a filename for each file:
var newFileName = ...
string generatedPDFLocation = System.IO.Path.Combine(currentOutputDirectory, newFileName);
And you make a new PdfDocument
and Document
instance, to be written to a file with that filename:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(generatedPDFLocation,
CreateEncryptionWriteProperties()));
Document doc = new Document(pdfDoc);
Then just add content to the Document
instance (or to the PdfDocument
). I assume that content is in the item
or resultCollection
somehow.
doc.add(new Paragraph("content for this document"));
Finally, close the document which will flush the PDF file to disk.
doc.close();
The next iteration of the foreach
loop will generate a different filename and thus the next document will be written to a different file.
You're using:
var firstDocsMetadata = resultCollection.First().MetaData;
When it should be:
var firstDocsMetadata = item.MetaData;
Since you're iterating the resultCollection
and item
is the element that is obtained in each loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.