Headers With Images

Hello,


I am currently attempting to make a document which contains a header, footer and a table in-between spans multiple pages.

The table, header and footer are working fine but when I want to add an Image to the header I am stuck. I have tried adding the image directly to the page as described in the following page.

https://docs.aspose.com/pdf/net/working-with-images/

But this only appears on the first page. I though about repeating the code for all the pages in the document but as I am only adding one page and the table automatically going into page 2 is adding the rest of the pages I don’t seem to have access to the remaining pages.

Hi Neil,


Thanks for contacting support.

Can you please share the code snippet and sample PDF files being generated in your environment so that we can test the scenario at our end. We are sorry for this inconvenience.

Well this is the issue I don’t know where to start so I don’t really have any code.


The code I could find adds the image to the currentPage

private double AddImage(Page currentPage, Stream imageStream, double x, double y, double height, bool factorWidth = false)
{
currentPage.Resources.Images.Add(imageStream);
currentPage.Contents.Add(new Operator.GSave());

XImage ximage = currentPage.Resources.Images[currentPage.Resources.Images.Count];
double scale = height / ximage.Height;
double width = ximage.Width * scale;

if (factorWidth)
x = x - width;
Rectangle rectangle = new Rectangle(x, y, x + width, y + height);
Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
currentPage.Contents.Add(new Operator.ConcatenateMatrix(matrix));
currentPage.Contents.Add(new Operator.Do(ximage.Name));
currentPage.Contents.Add(new Operator.GRestore());

return width;
}


Which is fine but I can only access the first page when I add a table.

Page page = doc.Pages.Add();
Table table;
SetupTable(out table);
Row newRow;
foreach(DataRow dr in dt.Rows)
{
newRow = table.Rows.Add();
}
page.Paragraphs.Add(table);


So if I apply the above method in some sort of loop like below then I only get images on the first page

foreach(Page page in doc.Pages)
{
AddImage(page, LeftImage, 100, 100, 100);
}


Hi Neil,

Thanks for sharing the code snippet.

I have tested the scenario and I am able to notice the same problem. For the sake of correction, I have logged this problem as PDFNEWNET-36623 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hi Neil,

In addition to Nayyer’s comments, please note that PDF is created dynamically and in order to get real PDF document, the PDF engine needs to render its programming/dynamic model, so the actual structure is created. So please break your scenario into two parts. Create your PDF document and save it into a stream and then reopen it and add images on respective pages. Hopefully, it will help you to accomplish your task.

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
doc.save(outputStream);
doc = new Document( new ByteArrayInputStream(outputStream.toByteArray()));

Please feel free to contact us for any further assistance.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-36623) have been fixed in Aspose.Pdf for .NET 9.1.0.

For further details, you may check this blog post.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi Neil,

In order to generate correct output, please try using following code snippet.

string outFile = TestSettings.GetOutputFile("36623.pdf");
string inFile = TestSettings.GetInputFile("../Generator/36163.bmp");

//open document
Document pdfDocument = new Document();
Page page = pdfDocument.Pages.Add();
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// SetupTable(out table);
Aspose.Pdf.Row newRow;

for (int counter = 0; counter <= 100; counter++)
{
    newRow = table.Rows.Add();
    newRow.Cells.Add("Sample Cell" + counter);
}
page.Paragraphs.Add(table);

pdfDocument.Save(outFile);
pdfDocument = new Document(outFile);
FileStream fs = new FileStream(inFile, FileMode.Open);

foreach (Page innerpage in pdfDocument.Pages)
{
    AddImage(innerpage, fs, 100, 100, 100);
}
pdfDocument.Save(outFile);