PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDF Sharp Obtener imagen FromStream funciona a veces
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4108
Page 1 of 1

Author:  edudome9 [ Mon Mar 09, 2020 12:49 am ]
Post subject:  PDF Sharp Obtener imagen FromStream funciona a veces

Buenas,

Estoy utilizando el Nuget PDF Sharp para poder extraer la primer página de un archivo PDF que contiene 2 tablas de datos iguales una arriba de la otra, y así guardar en otro archivo PDF de una única página la primera de las tablas.

Esto lo hago así:



Code:
   
// Split del pdf para que quede en una sola página cada contenido
   
    Stream sourceStreamPdfOriginal = uploadfile.InputStream; // se carga el stream del PDF subido.
   
    using (MemoryStream streamPdfOriginal = new MemoryStream())
    {
             sourceStreamPdfOriginal.CopyTo(streamPdfOriginal);
             pdfOriginalBytes = streamPdfOriginal.ToArray();
    }
   
    PdfDocument originalDocument = PdfReader.Open(sourceStreamPdfOriginal, PdfDocumentOpenMode.Import);
   
    PdfDocument splitedDocument = new PdfDocument();
   
                                           
    splitedDocument.AddPage(originalDocument.Pages[0]);
   
    using (MemoryStream memoryStream = new MemoryStream())
    {
           splitedDocument.Save(memoryStream, false);
                                           
           //if (System.IO.File.Exists("prueba.dbf")) {
           //    System.IO.File.Delete("prueba.dbf");
           //}
   
           //System.IO.File.WriteAllBytes("prueba.pdf", memoryStream.ToArray());
           //Process.Start("prueba.pdf");
   
   
           PdfDocument tablaIndividualDocument = new PdfDocument();
   
           PdfSharp.Pdf.PdfPage page = tablaIndividualDocument.AddPage();
           page.Width = splitedDocument.Pages[0].Width;
           page.Height = splitedDocument.Pages[0].Height / 2;
   

   
           XImage img = XImage.FromStream(memoryStream);
           XGraphics gfx = XGraphics.FromPdfPage(page);
           XRect rectangle = new XRect(0, 0, page.Width, page.Height * 2);
           
           gfx.DrawImage(img, rectangle); // Acá me da el ERROR
   
   
           using (MemoryStream streamPdfIndividual = new MemoryStream())
           {                                 
                    tablaIndividualDocument.Save(streamPdfIndividual, false);
                    paginaIndividualPdfBytes = streamPdfIndividual.ToArray();
           }
 
    }



Este codigo en realidad lo llamo varias veces, para hacer lo mismo con todas las páginas del PDF original. Pero para ejemplo lo simplifiqué a la primer página nada más. El error no depende de esto.

El problema está en que funciona todo bien, pero a veces no. En algunas páginas sí funciona y en otras no.

Y no se trata del contenido de la página, creo, porque en otro PDF tengo una página con los mismos datos (las mismas dos tablas) y funciona bien.


En el código marqué donde da error, al hacer un DrawImage().

El error que arroja es el siguiente:

> "Illegal character.\r\nNombre del parámetro: data"


Tengo comentado algo que usé para guardar el pdf que contiene solamente la única página (pero aún con las 2 tablas), y así fijarme si estaba bien esto, y sí lo está porque el pdf queda bien con una sola página.

Pero lo que busco es que quede con la primera de las 2 tablas y no ambas, por eso tengo que usar lo de DrawImage para cortar la imagen con Rectangle, que es lo que falla.


Si pueden ayudarme les agradezco, que estoy hace días buscando y no encuentro solución!

Saludos

Author:  IRlyDunno [ Mon Mar 09, 2020 6:47 pm ]
Post subject:  Re: PDF Sharp Obtener imagen FromStream funciona a veces

This is to the OP / Esto es para el OP
Hola, no hay personas en este forum que hablem castellano, pero voy a intentar traducir para ellos, proxima vez haz la pergunta en ingles.


#Start of Translation:

Hello

I'm using the PDFSharp nuget so that I can extract the first page of a PDF file that contains 2 tables one on top of the other with the same data, and thensave on another PDF file (that only has one page), the first of the 2 tables.

I do This with this code:

edudome9 wrote:
// Split del pdf para que quede en una sola página cada contenido

Stream sourceStreamPdfOriginal = uploadfile.InputStream; // se carga el stream del PDF subido.

using (MemoryStream streamPdfOriginal = new MemoryStream())
{
sourceStreamPdfOriginal.CopyTo(streamPdfOriginal);
pdfOriginalBytes = streamPdfOriginal.ToArray();
}

PdfDocument originalDocument = PdfReader.Open(sourceStreamPdfOriginal, PdfDocumentOpenMode.Import);

PdfDocument splitedDocument = new PdfDocument();


splitedDocument.AddPage(originalDocument.Pages[0]);

using (MemoryStream memoryStream = new MemoryStream())
{
splitedDocument.Save(memoryStream, false);

//if (System.IO.File.Exists("prueba.dbf")) {
// System.IO.File.Delete("prueba.dbf");
//}

//System.IO.File.WriteAllBytes("prueba.pdf", memoryStream.ToArray());
//Process.Start("prueba.pdf");


PdfDocument tablaIndividualDocument = new PdfDocument();

PdfSharp.Pdf.PdfPage page = tablaIndividualDocument.AddPage();
page.Width = splitedDocument.Pages[0].Width;
page.Height = splitedDocument.Pages[0].Height / 2;



XImage img = XImage.FromStream(memoryStream);
XGraphics gfx = XGraphics.FromPdfPage(page);
XRect rectangle = new XRect(0, 0, page.Width, page.Height * 2);

gfx.DrawImage(img, rectangle); // Acá me da el ERROR


using (MemoryStream streamPdfIndividual = new MemoryStream())
{
tablaIndividualDocument.Save(streamPdfIndividual, false);
paginaIndividualPdfBytes = streamPdfIndividual.ToArray();
}

}


In reality this code is called several times, to do the same with all the pages of the original pdf. But I only simplified it to do the first page nothing else, The error doesn't depend on this.

The problem is in that everything works fine, it only fails sometimes, In some pages it works, in others it doesn't.

I verified that this isn't linked to the content of the page, I think, because in another PDF I have a page with the same data (the same tables) and it works.

In the code I maked where it the error ocurrs while doing a DrawImage():

this is the error:

> "Illegal character.\r\nName of the parameter: data" // data in spanish could be date but I don't know that is the case here

I have commented the code that I used to save the PDF that contains only one page (but still with the 2 tables), and this way make sure that this was right, and yes it is, because the pdf is okay as long as it only has one page.

But what I'm searching for a way to get the first table on another pdf. To do that I have to use the DrawImage() to cut the image with a rectangle, and that is missing.

If someone can help me I thank you in advance, I'm looking for a solution since some days now, and I just can't find it.

With best regards.

#End of Translation

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/