La fusion de la Mémoire des Flux de créer un PDF http de la réponse en c#

Je suis en train de fusionner 2 crystal reports en un seul fichier pdf et je suis en utilisant Itextsharp v5.1.1. Mais il est dit que le document ne peut pas être ouvert. Il pourrait être corrompu. Il n'y a pas d'erreurs de génération. mais le pdf est incorrect et ne peut pas être ouvert. Voici l'ordre que j'ai choisi pour accomplir cette tâche.

  1. Exporter le rapport Crystal MemoryStream1 en format pdf
  2. Exporter le deuxième rapport en MemoryStream2.
  3. De fusion de la Mémoire des Flux
  4. Envoyer le Flux Http de Sortie Réponse en tant que PDF.

Voici le Code pour chaque étape dans l'ordre.

   ///Get the Dataset from Stored Procedure for the CSSource Report
   dsCS = CSData.GetUSSourceXML(ds_input);
   ///Create the Report of type CSsource
   rptCS = ReportFactory.GetReport(typeof(CSsource));
   rptCS .SetDataSource(dsCS);
   ///Set the Parameters to the CS report
   rptCS .ParameterFields["Parameterid"].CurrentValues.Clear();
   rptCS .SetParameterValue("Parameterid", PID);
   ////Serialize the Object as PDF                    
   msCS=(MemoryStream)rptCS .ExportToStream(ExportFormatType.PortableDocFormat);

Pour L'Étape 2

   ///Get the Dataset from Stored Procedure for the Aden Report
   dsAd = CSData.GetAdden(ds_input);
   ///Create the Report of type Aden
   rptAd = ReportFactory.GetReport(typeof(Aden));
   rptAd.SetDataSource(dsAd );
   ///Set the Parameters to the Aden report
   rptAd.ParameterFields["Parameterid"].CurrentValues.Clear();
   rptAd.SetParameterValue("Parameterid", PID);
   ////Serialize the Object as PDF                    
   msAD = (MemoryStream)rptAd.ExportToStream(ExportFormatType.PortableDocFormat);

Pour L'Étape 3

  System.Collections.Generic.List<byte[]> sourceFiles = new List<byte[]>();
  sourceFiles.Add(msCS.ToArray());
  sourceFiles.Add(msAD.ToArray());
  PdfMerger mpdfs = new PdfMerger();
  ///ms is the Memory stream to which both the streams are added
  ms=mpdfs.MergeFiles(sourceFiles);

MergeFiles méthode est comme suit

 public MemoryStream MergeFiles(Generic.List<byte[]> sourceFiles)
{
Document document = new Document();
MemoryStream output = new MemoryStream();
try
{
//Initialize pdf writer
PdfWriter writer = PdfWriter.GetInstance(document, output);
//writer.PageEvent = new PdfPageEvents();
//Open document to write
document.Open();
PdfContentByte content = writer.DirectContent;
//Iterate through all pdf documents
for (int fileCounter = 0; fileCounter < sourceFiles.Count; 
fileCounter++)
{
//Create pdf reader
PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
int numberOfPages = reader.NumberOfPages;
//Iterate through all pages
for (int currentPageIndex = 1; currentPageIndex <=
numberOfPages; currentPageIndex++)
{
//Determine page size for the current page
document.SetPageSize(
reader.GetPageSizeWithRotation(currentPageIndex));
//Create page
document.NewPage();
PdfImportedPage importedPage =
writer.GetImportedPage(reader, currentPageIndex);
//Determine page orientation
int pageOrientation = 
reader.GetPageRotation(currentPageIndex);
if ((pageOrientation == 90) || (pageOrientation == 270))
{
content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(currentPageIndex).Height);
}
else
{
content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
}
}
}
}
catch (Exception exception)
{
throw new Exception("There has an unexpected exception" +
" occured during the pdf merging process.", exception);
}
finally
{
//document.Close();
}
return output;
}

L'étape 4 pour Sérialiser les flux de Mémoire au format PDF

  //ms is the memory stream which is to be converted to PDF
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Charset = string.Empty;
Response.AddHeader("Content-Disposition", 
"attachment; filename=" + 
"Application of " + FullName.Trim() + ".pdf");
//Write the file directly to the HTTP content output stream.
Response.OutputStream.Write(ms.ToArray(), 0, 
Convert.ToInt32(ms.Length));
Response.OutputStream.Flush();
Response.OutputStream.Close();
rptCS.Close();
rptCS.Dispose();
rptAd.Close();
rptAd.Dispose();

Merci pour tous ces Développeurs de m'aider avec cela.
C'est Urgent car il doit aller de production dans un jour ou 2.
S'il vous plaît répondre.

Chandanan.

avez-vous vérifier si chaque pdf lui-même est ok ?
Oui, ils sont de travailler séparément avec la fonction de fusion et commenté. Je pouvais voir chaque rapport.

OriginalL'auteur Chandanan | 2011-07-21