Click here to Skip to main content
14,696,627 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


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));
                       
//NOt sure how I can continue from here

                        message.FinancialDocumentAttachments.Add(new MessageQueueAttachment()
                        {
                            Location = documentPath,
                            IsNew = true,
                            Id = Guid.NewGuid()
                        });
                    }
Posted
Updated 16-Jan-20 5:40am

1 solution

For Each file in the list
  Create new PDF
  Add contents
  Save (As)
End For
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900