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