ABCPDF Converting Text PDF to Image PDF with correct layout

I am trying to convert a text pdf to image pdf, and for that I found the following article:

ABCpdf convert text to image

So I took the code an produced the following code:

WebSupergoo.ABCpdf9.Doc firstDoc = new WebSupergoo.ABCpdf9.Doc();
WebSupergoo.ABCpdf9.Doc secondDoc = new WebSupergoo.ABCpdf9.Doc();

firstDoc.Read(@"C:\pdf1.pdf");

for (int i = 1; i <= firstDoc.PageCount; i++)
{
    secondDoc.Page = secondDoc.AddPage();
    firstDoc.PageNumber = i;
    secondDoc.MediaBox.String = firstDoc.MediaBox.String;

    using (Bitmap bm = firstDoc.Rendering.GetBitmap())
    {
        secondDoc.AddImageBitmap(bm, false);
    }
}

secondDoc.Save(@"c:\pdf2.pdf");

Now the code above works well except when I have pdf documents that have some page in portrait layout and other pages in landscape. What ends happening is the following:

let's say that I have a pdf document that has;

Page 1 - portrait
Page 2 - landscape
Page 3 - portrait
Page 4 - portrait

The result that this code is producing is:

Page 1 - portrait
Page 2 - portrait
Page 3 - landscape
Page 4 - portrait

Is there anything else that I would need to do other than setting the MediaBox to have the correct outcome?

Answers


Thanks for the helpful feedback in the comments, I was able to solve the problem by putting

secondDoc.Page = secondDoc.AddPage();

after

secondDoc.MediaBox.String = firstDoc.MediaBox.String;

The working code now looks like this:

WebSupergoo.ABCpdf9.Doc firstDoc = new WebSupergoo.ABCpdf9.Doc();
WebSupergoo.ABCpdf9.Doc secondDoc = new WebSupergoo.ABCpdf9.Doc();

firstDoc.Read(@"C:\pdf1.pdf");

for (int i = 1; i <= firstDoc.PageCount; i++)
{
    firstDoc.PageNumber = i;
    secondDoc.MediaBox.String = firstDoc.MediaBox.String;
    secondDoc.Page = secondDoc.AddPage();

    using (Bitmap bm = firstDoc.Rendering.GetBitmap())
    {
        secondDoc.AddImageBitmap(bm, false);
    }
}

secondDoc.Save(@"c:\pdf2.pdf");

Need Your Help

lamp: Warning: Remote connection disconnect. Retrying

apache symfony vagrant virtualbox lamp

I created an Ubuntu vhost, it's my virtual server for current project. Since I'm using symfony I downloaded composer, apache and etc. While doing this everything worked just fine. So at the last st...

How to show Vimeo videos in my own app

video appcelerator vimeo appcelerator-titanium appcelerator-alloy

I have an app for Android and iPhone where we want to create a page with some videos for the user to see (instructions). These videos are all on Vimeo (you can see them here).