Rotate native PDFs and merge with generated PDFs

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF Generator for .NET (v4)  /  Re: Rotate native PDFs and merge with generated PDFs

DynamicPDF Generator for .NET (v4) Forum

I don't know if this belongs in Generator or Merger. I have 2-4 native PDFs uploaded and stored in letter/portrait format. I then generate a legal/landscape PDF from a webpage. When I merge these documents together, I want the native PDFs rotated to letter/landscape format (or legal/landscape), with the legal/landscape PDF appended. I have this working with the exception of rotating the native PDFs (and converting to legal). Can I pre-rotate the native files, or do I do it at merge execution? I've seen posts about page rotation, but no details on the method or classes needed. A code snippet example would help greatly if I can do this.
Posted by a ceTe Software moderator
Hello,

Yes. you can rotate the pages of the already existing PDF document and then append a generated PDF document to it without any problem using our DynamicPDF Merger for .NET product. You will have to open the already existing PDF document using the PdfDocument object and then create MergeDocument object using PdfDocument object. You will have to loop through each page in the PDF document and rotate each page using the Rotate property of the Page class.

Once all the pages of the PDF document are rotated then you can create new PDF document from scratch and output it to the byte array using the Draw method of the Document class and then append this to the MergeDocument object using Append method. Please find the below code for rotating the pages in the already existing PDF document and appending a newly generated PDF Document.

            PdfDocument pdf = new PdfDocument(@"C:\temp\DocumentC.pdf");
            MergeDocument mergeDocument= new MergeDocument(pdf);
            //The below code will rotate the pages of the already exsting PDF document to anlge 90.
            for (int i = 0; i < mergeDocument.Pages.Count; i++)
            {
                mergeDocument.Pages[i].Rotate = 90;
            }
            //Below code is to generate PDF document.
            Document document = new Document();
            Page page = new Page(PageSize.Legal, PageOrientation.Landscape);
            Label label = new Label("Page with size Legal and page orientation Landscape",100,100,400,20);
            page.Elements.Add(label);
            document.Pages.Add(page);
            byte[] byteData = document.Draw();
            //Appending the generated PDF document
            mergeDocument.Append(new PdfDocument(byteData));
            mergeDocument.Draw(@"C:\Temp \Test.pdf");



Thanks,
ceTe Software Support Team.
This is close, but I can't make it work. What I want to do is:

1. Convert/rotate 1 existing PDF file
2. Save the file
3. Merge it with another PDF file in a separate operation

I am using VB, and converted your C# example as follows:

Dim pdf As New PdfDocument("C:\temp\DocumentC.pdf")
Dim mergeDocument As New MergeDocument(pdf)
'The below code will rotate the pages of the already exsting PDF document to anlge 90.
For i As Integer = 0 To mergeDocument.Pages.Count - 1
        mergeDocument.Pages(i).Rotate = 90
Next
'Below code is to generate PDF document.
Dim document As New Document()
Dim page As New Page(PageSize.Legal, PageOrientation.Landscape)
Dim label As New Label("Page with size Legal and page orientation Landscape", 100, 100, 400, 20)
page.Elements.Add(label)
document.Pages.Add(page)
Dim byteData As Byte() = document.Draw()
'Appending the generated PDF document
mergeDocument.Append(New PdfDocument(byteData))
mergeDocument.Draw("C:\Temp \Test.pdf")

So really, all I need is to convert and save the existing file. I have the merge already coded and working without the rotate portion.

Joel

I got it to work. My issue was that the MergeDocument.Draw call will not overwrite the existing file. So I created a new filetype in my web.config, write the rotated file to that name, then do my merge with the other file at a later time.

Thanks for your help,
Joel
Thanks for this post.  I trying to do something similar where I want to rotate a document 180 degrees.  The code I am using is below and it works great to rotate a document.  The problem is when I try to rotate the newly created document again.  It doesn't do the rotation.  It creates the new document, but it hasn't been rotated.  Any ideas why or how to fix this?


Dim fname As String = DocName.Substring(0, DocName.LastIndexOf("."))
Dim final As String = DocPath & fname & "_r.pdf"
Dim pdf As New PdfDocument(DocPath & DocName)
Dim mergeDocument As New MergeDocument(pdf)
'The below code will rotate the pages of the already exsting PDF document 180 degrees.
For i As Integer = 0 To mergeDocument.Pages.Count - 1
    mergeDocument.Pages(i).Rotate = 180
Next
mergeDocument.Draw(final)
Posted by a ceTe Software moderator
Hello,

You can rotate the pages in the final PDF document which you got after rotating the pages and merging the PDF documents. You will have to loop through the pages of the final PDF document and set some anlge(multiples of 90) using Rotate property of the Page class. This shhould work for you. Below is the sample code for it.

        Dim fname As String = "C:\Temp\DocumentC.pdf"
        Dim final As String = "C:\Temp\Firsrt.pdf"
        Dim pdf As New PdfDocument(fname)
        Dim mergeDocument As New MergeDocument(pdf)
        'The below code will rotate the pages of the already exsting PDF document 180 degrees.
        For i As Integer = 0 To mergeDocument.Pages.Count - 1
            mergeDocument.Pages(i).Rotate = 180
        Next
        mergeDocument.Draw(final)
        'The below code is to rotate the pages of the final PDF document.
        Dim mergeRotated As New MergeDocument(final)
        For i As Integer = 0 To mergeRotated.Pages.Count - 1
            mergeRotated.Pages(i).Rotate = 90
        Next
        mergeRotated.Draw("C:\Temp\Second.pdf")

Thanks,
ceTe Software Support Team.
Yes, I've found that using a temp document set when rotating and merging 2 or more documents makes the process quite easy. This is especially so if the orientations and/or page sizes of the various documents are different. Just remember to delete the temp document(s) before you exit your sub so you don't end up with a lot of orphaned files.

Joel

All times are US Eastern Standard time. The time now is 2:46 AM.