Hi.
I’ve noticed an unusual behaviour for some files after saving them via Aspose.Pdf: the file no longer displays correctly in Google Chrome. The original file is displayed correctly, but after saving it via Aspose, it is not (no matter what manipulations were performed on the file or if any).
Here is the code that is enough to reproduce the problem:
var pdfDocIn = File.ReadAllBytes(Environment.CurrentDirectory + "/Example/in.pdf");
using (var pdfDocStream = new MemoryStream())
{
pdfDocStream.Write(pdfDocIn, 0, pdfDocIn.Length);
pdfDocStream.Seek(0, SeekOrigin.Begin);
using (var pdfDoc = new Aspose.Pdf.Document(pdfDocStream))
{
pdfDoc.Save(pdfDocStream);
}
var pdfDocOut = pdfDocStream.ToArray();
File.WriteAllBytes(Environment.CurrentDirectory + "/Example/out.pdf", pdfDocOut);
}
The result looks like an empty file:
image.png (8.3 KB)
If you change the code slightly, the content will appear, but it will be broken.
var pdfDocIn = File.ReadAllBytes(Environment.CurrentDirectory + "/Example/in.pdf");
using (var pdfDocStream = new MemoryStream())
{
pdfDocStream.Write(pdfDocIn, 0, pdfDocIn.Length);
pdfDocStream.Seek(0, SeekOrigin.Begin);
using (var pdfDoc = new Aspose.Pdf.Document(pdfDocStream))
{
var taggedContent = pdfDoc.TaggedContent;
pdfDoc.Save(pdfDocStream);
}
var pdfDocOut = pdfDocStream.ToArray();
File.WriteAllBytes(Environment.CurrentDirectory + "/Example/out.pdf", pdfDocOut);
}
image.png (36.6 KB)
The issue is reproduced only for some files.
I am using Aspose.PDF.Drawing.
I have found that the problem can be solved using this code, but I would still like to know the cause of the problem:
var pdfDocIn = File.ReadAllBytes(Environment.CurrentDirectory + "/Example/in.pdf");
using (var pdfDocStream = new MemoryStream())
using (var pdfDocStreamOut = new MemoryStream())
{
pdfDocStream.Write(pdfDocIn, 0, pdfDocIn.Length);
pdfDocStream.Seek(0, SeekOrigin.Begin);
using (var pdfDoc = new Aspose.Pdf.Document(pdfDocStream))
{
pdfDoc.Save(pdfDocStreamOut);
}
var pdfDocOut = pdfDocStreamOut.ToArray();
File.WriteAllBytes(Environment.CurrentDirectory + "/Example/out.pdf", pdfDocOut);
}
In the attachment file example:
in.pdf (231.4 KB)