DATA STORED IN STREAM DATA TYPE TO PDF

s = ac.ExportApplicationResultDocument(templatePath, spiderDiagram, bestDiagram, worstDiagram,
this._positionsNormalCaseResults, this._positionsWorstCaseResults,
this._normalCaseRequiredTrainings, this._worstCaseRequiredTrainings,
this._assessedLevel, this._justification, this._normalCaseResultingDecision,
this._worstCaseResultingDecision,
(InternalStatus)application.IdCurrentApplicationStatus.Value,
applicant.FirstName,
applicant.LastName,
string.Format(Resources.TextResources.ResultPageGlobalResultEADSCorpId, applicant.EADSPersonalID), worstCaseTableImage, bestCaseTableImage, this._assessedDate);

Variable S is of stream data type. I am able to export it into WORD using OpenXML. I have permission to use Aspose.PDF for PDF generation. I need help to export this data inside stream data type to PDF.

Thank You.

@arun007

It depends upon the Stream data type. You can try initializing Document Object using Stream and save it as PDF. However, if you notice any issue, please share complete use case with us so that we can further investigate the issue in our environment and address it accordingly.

Document doc = new Document(STREAM);
doc.Save("output.pdf");

This is all the data fed into the stream variable s by the function and ac is just an application controller

public System.IO.Stream ExportApplicationResultDocument(string templatePath,
Image spiderDiagram,
Image bestDiagram,
Image worstDiagram,
DataTable questionsDurationBestCase,
DataTable questionsDuration,
IEnumerable trainingBestTable,
IEnumerable trainingWorstTable,
string assessedLevel,
string justification,
string resultingDecisionBest,
string resultingDecisionWorst,
InternalStatus status,
string applicantFirstName,
string applicantLastName,
string applicantEadsCorpId,
Image worstCaseTableImage,
Image bestCaseTableImage,
string assessedDate
)
{
DataRow[] partiallyAssessedResults;
DataTable exportQuestionResults;
WordDocument doc = WordDocument.OpenCopy(File.OpenRead(templatePath), true);
foreach (WordBookmark bm in doc.GetBookmarks())
{

            switch (bm.Name)
            {
                    
                case Constants.ApplicationResult.CriterionBestDiagram:
                    if (bestDiagram != null)
                        bm.SetBookmarkImage(bestDiagram);
                    break;
                case Constants.ApplicationResult.CriterionBestTable:
                    exportQuestionResults = questionsDurationBestCase.Copy();
                    exportQuestionResults.Columns.Remove("1");
                    exportQuestionResults.Columns.Remove("2");
                    exportQuestionResults.Columns.Remove("3");
                    exportQuestionResults.Columns.Remove("4");
                    exportQuestionResults.Columns.Remove("5");
                    exportQuestionResults.Columns.Remove("6");
                    exportQuestionResults.Columns.Remove("7");
                    exportQuestionResults.Columns.Remove("8");
                    exportQuestionResults.Columns.Remove("9");
                    bm.SetBookmarkRow(exportQuestionResults, 2);
                    break;
                case Constants.ApplicationResult.CriterionWorstDiagram:
                    if (worstDiagram != null)
                        bm.SetBookmarkImage(worstDiagram);
                    break;
                case Constants.ApplicationResult.CriterionWorstTable:
                    exportQuestionResults = questionsDuration.Copy();
                    exportQuestionResults.Columns.Remove("1");
                    exportQuestionResults.Columns.Remove("2");
                    exportQuestionResults.Columns.Remove("3");
                    exportQuestionResults.Columns.Remove("4");
                    exportQuestionResults.Columns.Remove("5");
                    exportQuestionResults.Columns.Remove("6");
                    exportQuestionResults.Columns.Remove("7");
                    exportQuestionResults.Columns.Remove("8");
                    exportQuestionResults.Columns.Remove("9");
                    bm.SetBookmarkRow(exportQuestionResults, 2);
                    break;
                case Constants.ApplicationResult.TrainingBestTable:
                    if (trainingBestTable != null)
                    {
                        bm.SetBookmarkRow(trainingBestTable, 2, "Title", "Duration");
                    }
                    break;
                case Constants.ApplicationResult.TrainingWorstTable:
                    if (trainingWorstTable != null)
                    {
                        bm.SetBookmarkRow(trainingWorstTable, 2, "Title", "Duration");
                    }
                    break;
                case Constants.ApplicationResult.AssessedLevel:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    else
                    {
                        bm.Text = assessedLevel;
                    }
                    break;
                case Constants.ApplicationResult.Justification:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    else
                    {
                        bm.Text = justification;
                    }
                    break;
                case Constants.ApplicationResult.AssessmentDate:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    else
                    {
                        bm.Text = assessedDate;
                    }
                    break;
                case Constants.ApplicationResult.ResultingDecisionBestCase:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    else
                    {
                        bm.Text = resultingDecisionBest;
                    }
                    break;
                case Constants.ApplicationResult.ResultingDecisionHeader:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    break;
                case Constants.ApplicationResult.AssessedLevelHeader:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    break;
                case Constants.ApplicationResult.JustificationHeader:
                    if (status.Equals(InternalStatus.SelfAssessed))
                    {
                        bm.Text = string.Empty;
                    }
                    break;
                case Constants.ApplicationResult.ResultingDecisionWorstCase:
                    bm.Text = resultingDecisionWorst;
                    break;
                case Constants.ApplicationResult.SpiderDiagram:
                    if (spiderDiagram != null)
                        bm.SetBookmarkImage(spiderDiagram);
                    break;
                case Constants.ApplicationResult.GenericDisciplineWorst:
                    var query = from DataRow question in questionsDuration.Rows where ((string)question["GenericDiscipline"]).ToLower() != "none" select (string)question["GenericDiscipline"];
                    bm.Text = query.Distinct().Count().ToString();
                    break;
                case Constants.ApplicationResult.GenericDisciplineBest:
                    var queryBest = from DataRow question in questionsDurationBestCase.Rows where ((string)question["GenericDiscipline"]).ToLower() != "none" select (string)question["GenericDiscipline"];
                    bm.Text = queryBest.Distinct().Count().ToString();                     
                    break;
                case Constants.ApplicationResult.Q1SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "1"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q1SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "1"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q2BisSumWorst:
                    bm.Text = "";
                    break;
                case Constants.ApplicationResult.Q2BisSumBest:
                    bm.Text = "";
                    break;
                case Constants.ApplicationResult.Q2SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "2"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q2SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "2"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q3SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "3"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q3SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "3"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q4SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "4"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q4SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "4"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q5SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "5"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q5SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "5"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q6SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "6"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q6SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "6"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q7SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "7"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q7SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "7"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q8SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "8"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q8SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "8"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q9SumWorst:
                    bm.Text = questionsDuration.Compute(string.Format(Constants.Format.ComputeSumPattern, "9"), null).ToString();
                    break;
                case Constants.ApplicationResult.Q9SumBest:
                    bm.Text = questionsDurationBestCase.Compute(string.Format(Constants.Format.ComputeSumPattern, "9"), null).ToString();
                    break;
                case Constants.ApplicationResult.ApplicantName:
                    bm.Text = string.Format("{0} {1}", applicantFirstName, applicantLastName);
                    break;
                case Constants.ApplicationResult.ApplicantEADSCorpId:
                    bm.Text = applicantEadsCorpId;
                    break;
                case Constants.ApplicationResult.TrainingBestSum:
                    if (trainingBestTable != null)
                    {
                        bm.Text = trainingBestTable.Sum(t => t.Duration).ToString();
                    }
                    break;
                case Constants.ApplicationResult.TrainingWorstSum:
                    if (trainingWorstTable != null)
                    {
                        bm.Text = trainingWorstTable.Sum(t => t.Duration).ToString();
                    }
                    break;
                case Constants.ApplicationResult.LabelPartialAssessorPercent:
                    // Search for partially assessed results (displayed values ends with *)
                    partiallyAssessedResults = questionsDurationBestCase.Select("[1Display] LIKE '*[*]'"
                        + " OR [2Display] LIKE '*[*]'"
                        + " OR [3Display] LIKE '*[*]'"
                        + " OR [4Display] LIKE '*[*]'"
                        + " OR [5Display] LIKE '*[*]'"
                        + " OR [6Display] LIKE '*[*]'"
                        + " OR [7Display] LIKE '*[*]'"
                        + " OR [8Display] LIKE '*[*]'"
                        + " OR [9Display] LIKE '*[*]'");
                    if (partiallyAssessedResults.Length == 0)
                    {
                        bm.Text = string.Empty;
                    }
                    break;
                case Constants.ApplicationResult.LabelPartialAssessorPercentBestCase:
                    // Search for partially assessed results (displayed values ends with *)
                    partiallyAssessedResults = questionsDuration.Select("[1Display] LIKE '*[*]'"
                        + " OR [2Display] LIKE '*[*]'"
                        + " OR [3Display] LIKE '*[*]'"
                        + " OR [4Display] LIKE '*[*]'"
                        + " OR [5Display] LIKE '*[*]'"
                        + " OR [6Display] LIKE '*[*]'"
                        + " OR [7Display] LIKE '*[*]'"
                        + " OR [8Display] LIKE '*[*]'"
                        + " OR [9Display] LIKE '*[*]'");
                    if (partiallyAssessedResults.Length == 0)
                    {
                        bm.Text = string.Empty;
                    }
                    break;
                case Constants.ApplicationResult.WorstCaseTable:
                    if (worstCaseTableImage != null)
                    {
                        bm.SetBookmarkImage(worstCaseTableImage);
                    }
                    break;
                case Constants.ApplicationResult.BestCaseTable:
                    if (bestCaseTableImage != null)
                    {
                        bm.SetBookmarkImage(bestCaseTableImage);
                    }
                    break;
                case Constants.ApplicationResult.ResultTitleHeader:
                    var valueResultTitle = Session["ResultTitleHeader"];
                    if (valueResultTitle != null)
                    {
                        bm.Text = valueResultTitle.ToString();
                    }
                    break;
                default:
                    break;
            }
        }

        return doc.GetStream();
    }

Well i tried your way beforehand, but its throwing error “incorrect header file”

@arun007

We are afraid that the shared information is not sufficient to investigate and address the issue. Are you preparing stream/data for a Word document? The Document constructor in Aspose.PDF will raise such exception when incoming stream/file or data is not a valid PDF. In case the stream or file is another file format (which is supported by the API already), you need to specify the respective LoadOptions e.g. HtmlLoadOptions.

Please share a sample console application that is able to replicate the same issue that you are facing so that we can test the scenario in our environment and address it accordingly.

PS: If you are able to export it to Word document, you can use Aspose.Words to convert the output file into PDF as well.

I need to export it to PDF only. In the function, we are returning it as word doc. Kindly let me know the chages required to return it as a pdf

Hello @asad.ali,
In the last code snippet, what is done is that there is a template in docx and by wordBookmark i am fetching each headings separately in for loop and depending on the name property of each heading, I have switch cases.
I need to do the same for PDF. I have converted all the templates in PDF.
But I don’t know how to read each bookmark separately from PDF in for loop like wordboookmark did here.
[PS- I know how to read whole text using Aspose]

@arun007

In order to work with bookmarks in the PDF, please check the below documentation article(s) in the API. In case you face any issue, please share a sample PDF along with the details of your requirements so that we can test the scenario in our environment and address it accordingly.

ResultTemplateSingleNoTraining-converted.pdf (118.1 KB)
I have uploaded a template PDF.
From this PDF, I have to extract each line and set value/diagram/graphs accordingly[ refer to the given code, I have done that in word].
now I am facing problems o convert System Image to Apose.Pdf.OutLineCollection, How to set spider diagram and all into PDF using Aspose?

@arun007

We are afraid that we could understand you requirements clearly by checking the earlier shared code snippet. It would be helpful if you can please share an expected output PDF for our reference so that we can try to produce the same in our environment and share our findings with you.

I have uploaded a output file in docx format. These diagrams i have to do with Aspose using the template[file uploaded in my prev comment].
P.S. - WORD -> PDF conversion will not work for me. As i have to download only the PDF from my application.Result (1).docx (71.4 KB)

@arun007

We have checked the word document and images in it. Are you also creating these images/charts/graphs using the code? If so, are you able to save these images separately as .jpg or .png? We noticed that in the Word file, the whole graph/chart was added as an image. In case of PDF format, you can add images in the PDF pages using the code given in the below articles: