Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.
Tue Feb 23, 2021 7:46 pm
You have an example in your docs for building a portfolio PDF from various file types using files in the file system via the docPortfolio.Collection.AddFile(fileName) method which takes only a file system filename (string) as a parameter(?). How can I do the same thing, but add the file to the portfolio PDF from a memory stream? e.g.:
- Code: Select all
string returnValue = "false";
var tenantId = AbpSession.TenantId;
MemoryStream memoryStream = new MemoryStream();
BinaryObject fileStreamLocal = new BinaryObject();
byte[] fileBytes;
try
{
String[] fileBLOBIDs = fileObjectIDs.Split("|");
//create a PdfDocument instance and build portfolio PDF
using (PdfDocument docPortfolio = new PdfDocument())
{
//create a PDF Portfolio and add files to it
for (int iFile = 0; iFile < fileBLOBIDs.Length; iFile++)
{
//open BLOB file object from DB
fileStreamLocal = AsyncHelper.RunSync(() => _binaryObjectManager.GetOrNullAsync(Guid.Parse(fileBLOBIDs[iFile])));
memoryStream = new MemoryStream(fileStreamLocal.Bytes);
//add file to portfolio
docPortfolio.Collection.AddFile(fileBLOBIDs[iFile]); //AddFile method accepts ONLY string parm ?- DOESN'T SUPPORT MEMORY STREAM?
}
//save the result file
//serialize portfolio PDF to memory stream
var workMemoryStream = new MemoryStream();
docPortfolio.SaveToStream(workMemoryStream, FileFormat.PDF);
//create compatible binary object from memory stream
using (var stream = workMemoryStream)
{
fileBytes = stream.ToArray(); //convert memory stream to byte array
}
var fileObject = new BinaryObject(tenantId, fileBytes);
//save as BLOB in DB
await _binaryObjectManager.SaveAsync(fileObject);
docPortfolio.Dispose();
}
//
returnValue = "true";
//
return returnValue;
}
...
-

schlarmage
-
- Posts: 1
- Joined: Tue Feb 23, 2021 12:51 pm
Wed Feb 24, 2021 3:36 am
Hello,
Thanks for your inquiry!
I am sorry that our Sprie.PDF does not support building portfolio PDF from memory stream now. We have added it as a new feature with the ticket SPIREPDF-4062 in our upgrade list. If there is any progress, we will keep you informed. Sorry for the inconvenience caused.
Sincerely,
Marcia
E-iceblue support team
-


Marcia.Zhou
-
- Posts: 858
- Joined: Wed Nov 04, 2020 2:29 am
Thu Feb 25, 2021 6:15 am
Hello,
Thanks for your patience!
Glad to inform you that our Dev team had added the new feature of building portfolio PDF from stream. Here we made a temporary version (
spire.pdf_7.2.13.zip) for you, please download it and refer to the following code to do a test with it.
- Code: Select all
string inputfile1 = @"E:\testdoc\sample.pdf";
string inputfile2 = @"E:\testdoc\sample1.pdf";
string inputfile3 = @"E:\testdoc\sample2.pdf";
MemoryStream ms1 = new MemoryStream(File.ReadAllBytes(inputfile1));
MemoryStream ms2 = new MemoryStream(File.ReadAllBytes(inputfile2));
MemoryStream ms3 = new MemoryStream(File.ReadAllBytes(inputfile3));
PdfDocument pdf = new PdfDocument();
pdf.Collection.AddFile("file1.pdf", ms1);
pdf.Collection.AddFile("file2.pdf", ms2);
pdf.Collection.AddFile("file3.pdf", ms3);
pdf.SaveToFile("newMethodAddFromStream.pdf", FileFormat.PDF);
Sincerely,
Marcia
E-iceblue support team
-


Marcia.Zhou
-
- Posts: 858
- Joined: Wed Nov 04, 2020 2:29 am