Hello.
I have got a problem. Part of my application converts normal pdf (with text, images…) to scaned pdf (only image on all page).
This is part of my function:
public void ConverAllDocument(string pathToDocument, string pathToOutputDocument)
{
int pageCountInDocument = 0;
using (Document readedPdfDocument = new Document(pathToDocument))
{
pageCountInDocument = readedPdfDocument.Pages.Count;
}Dictionary<int, Page> pagesForNewDocument = new Dictionary<int, Page>(); Parallel.For(1, pageCountInDocument, new ParallelOptions() { MaxDegreeOfParallelism = 100 }, (int pageNumber) => { using (Document readedPdfDocument = new Document(pathToDocument)) { Page pageWithOrginalDocument = readedPdfDocument.Pages[pageNumber]; Document document = new Document(); Page page = document.Pages.Add(); if (pageWithOrginalDocument.Rotate == Rotation.on90 || pageWithOrginalDocument.Rotate == Rotation.on270) { page.SetPageSize(pageWithOrginalDocument.CropBox.Height, pageWithOrginalDocument.CropBox.Width); } else { page.SetPageSize(pageWithOrginalDocument.CropBox.Width, pageWithOrginalDocument.CropBox.Height); } using (MemoryStream imageStream = GetPageAsImage(quality, pageWithOrginalDocument, pageWithOrginalDocument)) { System.Drawing.Image pageAsImage = System.Drawing.Image.FromStream(imageStream); Rectangle rectangleForCreatePage = GetImageRectangle(page, pageAsImage); ChangePageSizeIfNeeded(page, pageAsImage); using (MemoryStream imageStreamWithRotatedImage = RotatePageAsImageIfNeededAndSaveToStream(pageAsImage, page.Rotate)) { ConfigurePage(page, rectangleForCreatePage, imageStreamWithRotatedImage); } } pagesForNewDocument.Add(pageNumber, page); } }); using (FileStream fileStream = new FileStream(pathToOutputDocument, FileMode.Open, FileAccess.ReadWrite)) { using (Document newDocument = new Document(fileStream)) { foreach (var page in pagesForNewDocument.OrderBy(x => x.Key)) { newDocument.Pages.Add(page.Value); } newDocument.Save(fileStream); } } }
private void ConfigurePage(Page page, Rectangle rectangleForCreatePage, MemoryStream imageStreamWithRotatedImage) { page.Resources.Images.Add(imageStreamWithRotatedImage); **ERROR** page.Contents.Add(new GSave()); Rectangle rectangle = rectangleForCreatePage; Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY }); page.Contents.Add(new ConcatenateMatrix(matrix)); XImage ximage = page.Resources.Images[page.Resources.Images.Count]; page.Contents.Add(new Do(ximage.Name)); page.Contents.Add(new GRestore()); }
Error (in ConfigurePage method, first line):
{
“ClassName”: “System.ArgumentException”,
“Message”: “Invalid image stream (Brak pamięci.)”,
“Data”: null,
“InnerException”: null,
“HelpURL”: null,
“StackTraceString”: " w Aspose.Pdf.XImageCollection.#=zugwH$5w4M4K2ckjivQ==(Stream #=zkfE32z0=, #=zIkw$81NGhiTS0cSTL7dp4IPsSplb3jXycDr6RYN0E6hB #=z$76wheo=, #=zbRWZoBT1wG$RxNotWyeLI2uyicHuE6M4mw== #=zGfmCbS0=, Int32 #=zsEszqF4=, Image& #=zPZvIpjnZ3z3q, Stream #=zwA$zjgx7dCHh4_egt3XvPCM=)\r\n w Aspose.Pdf.XImageCollection.#=z2KZ$jsSPDGwU(Int32 #=zkkF7YfQ=, Stream #=zkfE32z0=, Int32 #=zsEszqF4=, Boolean #=z2kg_4Zsb8NN_, ImageFilterType #=zbhXGP0s=, Byte #=zdDZG87L8wmVe, Boolean #=zrDu6iS0=, String& #=zZMaykoiaLohK, Stream #=zwA$zjgx7dCHh4_egt3XvPCM=)\r\n w Aspose.Pdf.XImageCollection.#=z2KZ$jsSPDGwU(Int32 #=zkkF7YfQ=, Stream #=zkfE32z0=, Int32 #=zsEszqF4=, Boolean #=z2kg_4Zsb8NN_, ImageFilterType #=zbhXGP0s=, String& #=z9VTkN60=)\r\n w Aspose.Pdf.XImageCollection.#=z_EHCSMg=(Stream #=zkfE32z0=, Int32 #=zsEszqF4=, Boolean #=z2kg_4Zsb8NN_, String& #=z9VTkN60=)\r\n w Aspose.Pdf.XImageCollection.#=z_EHCSMg=(Stream #=zkfE32z0=, Boolean #=z2kg_4Zsb8NN_, String& #=z9VTkN60=)\r\n w Aspose.Pdf.XImageCollection.Add(Stream image)\r\n w Anon.Common.FileConverter.PdfToImagePdfConverter.ConfigurePage(Page page, Rectangle rectangleForCreatePage, MemoryStream imageStreamWithRotatedImage)
“RemoteStackTraceString”: null,
“RemoteStackIndex”: 0,
“ExceptionMethod”: “8\n#=zugwH$5w4M4K2ckjivQ==\nAspose.PDF, Version=21.8.0.0, Culture=neutral, PublicKeyToken=f0262d67fe233d63\nAspose.Pdf.XImageCollection\nVoid #=zugwH$5w4M4K2ckjivQ==(System.IO.Stream, #=zIkw$81NGhiTS0cSTL7dp4IPsSplb3jXycDr6RYN0E6hB, #=zbRWZoBT1wG$RxNotWyeLI2uyicHuE6M4mw==, Int32, System.Drawing.Image ByRef, System.IO.Stream)”,
“HResult”: -2147024809,
“Source”: “Aspose.PDF”,
“WatsonBuckets”: null,
“ParamName”: null
}
How can I avoid this error?