Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using vb.net to split a pdf then merge them accordingly by headers.

My code works fine except that sometimes the original PDF's have pages that are in legal format.

At the end of my code, everything is split/merged accordingly and the format remains in tact. My question is, is there a way within the splitting or merging to autoscale the legal sized PDF's into Letter?

What I have tried:

I have tried using this, but my PDF's are pdfs and not images:

PdfReader reader = new PdfReader(pdfData);
                logInfo.DebugFormat("Converting to a4 page size");
                Document document = null;
                if (isPortrait)
                {
                    document = new Document(PageSize.A4);
                }
                else
                {
                    //create a4 landscape
                    document = new Document(PageSize.A4.Rotate());
                }
                PdfWriter writer = PdfWriter.GetInstance(document, outputData);
                writer.CloseStream = false;
                document.Open();
                //copy the page from the source pdf
                PdfContentByte directcontent = writer.DirectContent;
                PdfImportedPage page = writer.GetImportedPage(reader, 1);
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(page);
                //center the certificate
                if (isPortrait)
                {
                    image.SetAbsolutePosition(-9, 25);
                }
                else
                {
                    image.SetAbsolutePosition(25, -9);
                }
                directcontent.AddImage(image);
                document.NewPage();
                document.Close();
                reader.Close();
Posted
Updated 5-Feb-19 22:51pm
Comments
Richard MacCutchan 31-Jan-19 11:48am    
It depends how the source is formatted. If you are reading pages that have been created at a certain size then you will need to recreate them in the new size. This means you need to check the amount of information you copy to each new page.
Proximus Seraphim Dimitri Daviticus 31-Jan-19 11:55am    
Hey Richard, i was thinking.. and maybe this is far fetched. But before the creation of the new pdf's or it being copied, can't there be a sort of way of writing

If document.pagesize <> itextsharp.text.pagesize.LETTER Then
code here
end if

?? Or is that too broad? My pdf's are split into 5 different headers. The header in particular that has the Legal documents is called exhibits. So i'll know i'll only have to deal with that particular header. Is there not a way to shrink the legal document into a letter?

I tried a code before and what it did was make the legal into letter, but it did this by cutting out a letter size document. So it just looked like a zoomed in cut off document that was letter size.
Richard MacCutchan 31-Jan-19 12:04pm    
If you want to do it properly then you will need to reformat all the content as you copy it, so it fits into the new page size. I cannot see any alternative.

"Scaling" implies (also) a change in font size.

If you're dealing with actual "pdf code", then change the font family and / or fontsize and go from there.

Take out justification, take out / shorten dividers, etc.
 
Share this answer
 
v2
If you use iTextSharp library, then check accepted answer: How to set custom page size of pdf in itextsharp[^]
 
Share this answer
 
Try this code.

string original = args[0];

            string inPDF = original;
            string outPDF = Directory.GetDirectoryRoot(original) + "temp.pdf";
            PdfReader pdfr = new PdfReader(inPDF);

            Document doc = new Document(PageSize.LETTER);
            Document.Compress = true;

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(outPDF, FileMode.Create));
            doc.Open();

            PdfContentByte cb = writer.DirectContent;

            PdfImportedPage page;

            for (int i = 1; i < pdfr.NumberOfPages + 1; i++)
            {
                page = writer.GetImportedPage(pdfr, i);
                cb.AddTemplate(page, PageSize.LETTER.Width / pdfr.GetPageSize(i).Width, 0, 0, PageSize.LETTER.Height / pdfr.GetPageSize(i).Height, 0, 0);
                doc.NewPage();
            }

            doc.Close();

            //just renaming, conversion / resize process ends at doc.close() above
            File.Delete(original);
           File.Copy(outPDF, original);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900