Attached PDF page setup being ignored when being added to another PDF

I’m having an issue where I’ve created a document (pdf) from scratch using aspose .net and at the bottom of the document I have to attach another pdf which may include pages that are in landscape (this pdf may have not been created by aspose). I have no problems attaching the pdf however when I attach the pdf to the document I’ve created all the pages that were landscape in the pdf and now turned to portrait.

I know how to change the page orientation through aspose however I have no way to detect which pages need to be landscape without having to manually add the page numbers myself which is not desirable.

Could someone advise on how I could keep the original settings on the pdf while still adding it to my document i have created ?

The document being created has very few page info changes with standard pages being added to it:

            var document = new Document
            {
                PageInfo = new PageInfo { Margin = new MarginInfo(45, 28, 45, 60) }
            };

The pdfs being attached are being downloaded from SharePoint Online and attached to the document object:

            for (int i = 0; i < documentLinks.Count(); i++)
            {
                byte[] fileContent = await _documentService.GetDocumentsViaDownload(accessToken, documentLinks[i]);
                MemoryStream fileStream1 = new MemoryStream(fileContent);
                Document docAttached = new Document(fileStream1);
                document.Pages.Add(docAttached.Pages);
                
            }

If you need anymore information please do not hesitate to ask

@cbparl

Please try using Document.ProcessParagraphs() method before adding pages from other PDF documents into your original PDF (which you are creating from scratch). However, please share some sample PDF file(s) along with a complete sample code snippet to replicate the issue if the suggested method does not resolve your issue. We will test the scenario in our environment and address it accordingly.

HI @asad.ali,

I have added the method Document.ProcessParagraphs() as instructed however this never resolved the issue

Here are the pdfs that were requested:

Attached_PDF.pdf (2.8 KB)
PDF with attached PDF.pdf (90.5 KB)
PDF_from_scratch.pdf (89.6 KB)

The code snippets from the first post are the main features when creating the document. We do not stray from the default settings

let me know if you need more information

@cbparl

We have tested the scenario in our environment with Aspose.PDF for .NET 21.1. We concatenated 2 PDF files i.e. PDF_from_scratch.pdf and Attached_PDF.pdf using below code snippet:

Document document1 = new Document(dataDir + "PDF_from_scratch.pdf");
Document document2 = new Document(dataDir + "Attached_PDF.pdf");
document1.Pages.Add(document2.Pages);
document1.Save(dataDir + "merged.pdf");

We did not notice any issue in the output PDF generated by the above code.
merged.pdf (90.5 KB)

Could you please share a bit more details about how can the issue be replicated? Please share the steps to reproduce the issue so that it can be addressed accordingly.

Hi @asad.ali

I’ve copied the above code you have added and I can confirm that does not have an issue in our environment. That test above is different to our scenario as neither of our pdfs are coming from our local storage.

The pdf created from scratch is never official created until the other pdf is attached.

I will be able to send over a code snippet once i redact some of the code I do not wish to share.

@cbparl

The issue seems related to your specific scenario as we were unable to replicate it at our end. It would be helpful if you can please share a sample console application which is able to replicate the same issue that you are facing. Please take your time to share that with us. We will again test the scenario and address it accordingly.

hi @asad.ali

Here is a console application that has the issue above:

The project does not include the license for aspose as I’m assuming that should not be shared.

If you have any issues please do not hesitate to ask.

@cbparl

The archive includes only executables. It does not have a complete Visual Studio project with code files. Please try to share a sample Visual Studio project which we can run at our end and observe the issue.

@asad.ali

A sample Visual Studio project has been added to the folder.

Looking forward to hearing from you.

@cbparl

We have tested the scenario and found the issue in the generated output PDF as you mentioned. We also noticed that you were setting margins at Document-level using Document.PageInfo. Please note that Document.PageInfo property is an old one that will be removed from the API sooner or later. Instead, it is recommended to use Page.PageInfo property to set the PDF margins at the time of generation. So please try to set the margins as below:

 var document = new Document();
//{
  //    PageInfo = new PageInfo { Margin = new MarginInfo(45, 28, 45, 60) }
//};


// Step 2
var pdfPage = document.Pages.Add();
pdfPage.PageInfo = new PageInfo { Margin = new MarginInfo(45, 28, 45, 60) };

Furthermore, you are doing a lot of processing before merging the PDF document with an existing one and Document.ProcessParagraphs() method is not saving the document state completely. It is recommended to use Document.Save() - Without any argument in order to save the document with an incremental approach. Replacing the Document.ProcessParagraphs() method with Document.Save() has resolved the issue in the output PDF:

testingDocument.pdf (77.6 KB)

Thank you, this has resolved our issue :slight_smile:

@cbparl

It is good to know that your issue has been resolved. Please keep using our API and feel free to create a new topic if you need further assistance.