简体   繁体   中英

C# ITextSharp Copy existing PDF to new PDF

I am working with a series of PDF files. Many of them have fields, which I am able to populate using iTextSharp.

My issue is there is one static PDF that seems to be reporting an odd size, 9.5 X 12. The page size should be 8.5 X 11 and if opened using Acrobat reader, it prints just fine.

To get around this, I want to create a new PDF Document, using the correct page size, then read the static file into the new document, save it and print it.

I have read a number of the posts here on SO and done a number of searches for a solution, all of which seem to skirt my particular issue.

Is there a way to copy the existing PDF to a new document using the correct page size?

Thank you

UPDATE

Taking Bruno's advice, I did double check the crop box and media box. The PDF I am working with has both. The crop box array is

[28.008, 38.016, 640.008, 830.016]

And the media box array is

[0, 0, 668.016, 848.016]

I can alter the upper right corner points making the crop box and media box the same Crop box

[28.008, 38.016, 612, 792]

And media box

[0, 0, 612, 792]

But doing so shifts the "text" to the right and top too much, leaving an uneven margin.

I found by changing the lower left coordinates, of the crop box, I can shift the text. In fact setting the crop box to 40, 40, 612, 792 works, but then the top and bottom margins are very narrow.

I need the media box to be 0, 0, 612, 792. Not an issue there. But then how do I shrink the crop box and center the text on the page? I must be missing something.

I've followed Bruno's RotatePages method, in his book, and also looked at this answer, iTextSharp copy document, resize to custom size, and center

But whatever I change with the crop box seems to zoom in on it.

Any advice is greatly appreciated

Thank you Bruno Lowagie and mkl. With your assistance I was able to arrive at this solution.

private void ResizeForm ( string path, string fileName )
{
    string src = path + @"\" + fileName + "_pre.pdf";
    string dest = path + @"\" + fileName + ".pdf";

    File.Move ( dest, src );

    using ( PdfReader pdf = new PdfReader ( src ) )
    {
        PdfDictionary pageDict;
        PdfArray cropBox;
        PdfArray mediaBox;

        float letterWidth = PageSize.LETTER.Width;
        float letterHeight = PageSize.LETTER.Height;

        int pageCount = pdf.NumberOfPages;

        for ( int i = 1; i <= pageCount; i++ )
        {
            pageDict = pdf.GetPageN ( i );
            cropBox = pageDict.GetAsArray ( PdfName.CROPBOX );
            mediaBox = pageDict.GetAsArray ( PdfName.MEDIABOX );

            cropBox [ 0 ] = new PdfNumber ( 30 );
            cropBox [ 1 ] = new PdfNumber ( 40 );
            cropBox [ 2 ] = new PdfNumber ( letterWidth + 30 );
            cropBox [ 3 ] = new PdfNumber ( letterHeight + 40 );

            mediaBox [ 0 ] = new PdfNumber ( 30 );
            mediaBox [ 1 ] = new PdfNumber ( 40 );
            mediaBox [ 2 ] = new PdfNumber ( letterWidth + 30 );
            mediaBox [ 3 ] = new PdfNumber ( letterHeight + 40 );

            pageDict.Put ( PdfName.CROPBOX, cropBox );
            pageDict.Put ( PdfName.MEDIABOX, mediaBox );
        }

        PdfStamper stamper = new PdfStamper ( pdf, new FileStream ( dest, FileMode.Create ) );
        stamper.Close ( );
    }
}

It may be a bit overkill, but it works for my purposes.

Hopefully, this will help someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM