Rotate multiple PDFs and write to one single PDF - c#

I have the following simplified code for pulling existing 8x10 PDFs from multiple locations, rotating them if need be (almost all need to be), then writing them to a single 11x17 PDF page by page...
while (Page < StackOne.Length)
{
Files++;
using (var strm = new FileStream(RenderPath + "Test_" + Page + ".pdf", FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (var MasterReport = new iTextSharp.text.Document(iTextSharp.text.PageSize._11X17))
{
using (var writer = PdfWriter.GetInstance(MasterReport, strm))
{
MasterReport.Open();
MasterReport.NewPage();
var cb = writer.DirectContent;
for (; Page <= NumPages * Files && Page < StackOne.Length; Page++)
{
var ProductionEntry = StackOne[Page - 1];
var filepath = NetPath + ProductionEntry.UniqueProductId + ".pdf";
if (File.Exists(filepath))
{
var reader = new PdfReader(filepath);
var pagesize = reader.GetPageSize(1);
if (pagesize.Height > pagesize.Width)
{
var ExistingPage = reader.GetPageN(1);
var rotation = ExistingPage.GetAsNumber(PdfName.ROTATE);
int desiredrot = 90;
if (rotation != null)
{
desiredrot += rotation.IntValue;
desiredrot %= 360;
}
ExistingPage.Put(PdfName.ROTATE, new PdfNumber(desiredrot));
}
cb.AddTemplate(writer.GetImportedPage(reader, 1), 50, 50);
}
MasterReport.NewPage();
}
}
}
}
}
However the page rendered doesn't have the pages rotated as they should be, I've verified the height > width branch is indeed being taken but the pages returned are still 8x10 instead of 10x8 written on each 11x17 page.
I searched around for a question this specific but couldn't find one that wasn't just writing to another file or the entire page instead of to a specific location on an 11x17 sheet.
EDIT: So with a little experimenting and looking at other examples I'm able to rotate the 8x10 page and write it to my 11x17 but unfortunately I can't seem to find a way to place it exactly where I want it here's the relevant code snippet:
var reader = new PdfReader(filepath);
var tm = new AffineTransform(1.0F, 0, 0, 1.0F, x, y);
if (reader.GetPageSize(1).Height > reader.GetPageSize(1).Width)
tm.SetTransform(0, -1f, 1f, 0, 0, reader.GetPageSize(1).Height);
writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), tm);

Okay so after tons of searching and sifting through code it appears the answer was quite simple (as usual), as shown above my initial problem was putting a rotate tag on the page didn't actually rotate the page as I expected. After learning about the option of specifying a matrix for the pdftemplate it was pretty simple here's the working code:
var reader = new PdfReader(filepath);
var tm = new AffineTransform();
if (reader.GetPageSize(1).Height > reader.GetPageSize(1).Width)
tm.SetTransform(0, -1f, 1f, 0, 0, reader.GetPageSize(1).Height);
tm.Concatenate(new AffineTransform(1f, 0, 0, 1f, y, x));
writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), tm);
P.S. http://partners.adobe.com/public/developer/en/pdf/PDFReference.pdf P: 162 (143 in physical form) for those who aren't fresh out of algebra
EDIT: As #mkl has stated below the following is really only applicable for rotate entries of 0° or 90° if you have pages at 180° or 270° this code will need some modifications. Also any annotations in the original will be lost.

You may simple add affine transform into AddTemplate procedure.
Like this:
PdfContentByte cb = stamper.getOverContent(pageNumber);
int rotation = reader.getPageRotation(pageNumber);
PdfImportedPage page = stamper.getImportedPage(reader, pageNumber);
if (rotation == 270)
cb.addTemplate(page, 0, 1f, -1f, 0, reader.getPageSizeWithRotation(pageNumber).getWidth(), 0);
else if (rotation == 90) {
cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(pageNumber).getHeight());
} else {
cb.addTemplate(page, 0, 0);
}
Its java, but I think it is not a problem.

Related

Getting past the 200 inch adobe error with iTextSharp

I am converting a multi-paged (paginated) pdf document into a single page (non-paginated) pdf document.
I am looking to overcome the 200 inch limitation in adobe reader.
With iTextSharp.PdfReader each page is read to create a total height of the target document and find the maximum width.
The code to create the document works ok reading directly from the paginated pdf into the non-paginated pdf. Utilizing Chrome or Foxit the file opens fine. Adobe gives the 200 inch truncation when the page exceeds 200 inchs. In my test file the page height is 8.25 x 814 inches.
Changing the UserUnits to 4.07 (814/200) has Adobe show the page height as 814in but still truncates the page as well as showing the width as 33.
If the width of the target file is set to width/userunits (8.25/4.07) the only left 2 inches are shown in the target file.
The copy part of the code:
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(fn);
SizeF pageSize = new SizeF(pageWidth, pageHeight);
float USERUnitNewValue = ComputeUserUnit(pageSize);
if (pageHeight > 14400f)
{
USERUnitNewValue = pageHeight / 14400f;
}
float NewPageWidth = (pageWidth <= 14400f) ? pageWidth : pageWidth* USERUnitNewValue;
float NewPageHeight = pageHeight * USERUnitNewValue;
FileInfo file1 = new FileInfo(newfn);
DirectoryInfo directory1 = file1.Directory;
if (!directory1.Exists)
directory1.Create();
iTextSharp.text.Rectangle newPagesize = new iTextSharp.text.Rectangle(pageWidth, pageHeight);
Document newPdf = new Document(newPagesize);
PdfWriter writer = PdfWriter.GetInstance(newPdf, new FileStream(newfn, FileMode.Create));
writer.PdfVersion = PdfWriter.VERSION_1_6;
if (pageHeight > 14400)
{
writer.Userunit = USERUnitNewValue;
}
newPdf.SetMargins(0f, 0f, 0f, 0f);
newPdf.Open();
PdfContentByte cb = writer.DirectContent;
float verticalPosition = pageHeight;
for (int pagenumber = 1; pagenumber <= n1; pagenumber++)
{
if (pdfReader.NumberOfPages >= pagenumber)
{
verticalPosition = verticalPosition - pdfReader.GetPageSize(pagenumber).Height;
cb.AddTemplate(writer.GetImportedPage(pdfReader, pagenumber), 0, verticalPosition);
}
else
{
break;
}
}
newPdf.Close();
How can the original file be copied into the target where both files would keep the same size if someone sends it to a printer?
Yes there is some redundancy in this code as I have been troubleshooting this for a little while now.
The key question here is a setting that would maintain the 8.25 x 814in and still allow adobe to open the file.
Thanks,
Mike
Thank you David.
After looking through Mr Lowagie's document and brief note about addTemplate.
cb.addTemplate(page, scale, 0, 0, scale, 0, 0)
The code was updated to utilize the new userunit and scaling.
Opening ok in Adobe now and reporting page length as expected
Once again the code is a little ugly still
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(fn);
SizeF pageSize = GetPageSize(fn);
PdfReader pdfReader = new PdfReader(fn);
float USERUnitNewValue = ComputeUserUnit(pageSize);
int n1 = pdfReader.NumberOfPages;
if (pageSize.Height > 14400f) //14400 value is 72 pixels per inch over 200 inches. 200 inches seems to be adobe limit to a page
{ //determine the userunit to be used
USERUnitNewValue = pageSize.Height / 14400f;
}
float NewPageWidth = (pageSize.Width <= 14400f) ? pageSize.Width / USERUnitNewValue : pageSize.Width / USERUnitNewValue;
float NewPageHeight = pageSize.Height / USERUnitNewValue;
FileInfo file1 = new FileInfo(newfn);
DirectoryInfo directory1 = file1.Directory;
if (!directory1.Exists)
directory1.Create();
iTextSharp.text.Rectangle newPagesize = new iTextSharp.text.Rectangle(NewPageWidth, NewPageHeight);
Document newPdf = new Document(newPagesize);
PdfWriter writer = PdfWriter.GetInstance(newPdf, new FileStream(newfn, FileMode.Create));
writer.PdfVersion = PdfWriter.VERSION_1_6;
if (pageSize.Height > 14400)
{
writer.Userunit = USERUnitNewValue;
}
newPdf.SetMargins(0f, 0f, 0f, 0f);
newPdf.Open();
PdfContentByte cb = writer.DirectContent;
float verticalPosition = NewPageHeight;
for (int pagenumber = 1; pagenumber <= n1; pagenumber++)
{
if (pdfReader.NumberOfPages >= pagenumber)
{
/*convoluted page position. First position should be 0,0
unlike other counters this starts as page 1 so we need to subtract the
first page height away so that we start at the bottom of the previous image
* hmm seems that ths AddTemplate feature adds the pages in reverse order or
* at least the coordinate system sets 0,0 at the bottom left of the page
*/
float widthfactor = 1 / USERUnitNewValue; //Page scaling (width)
float heightfactor = 1 / USERUnitNewValue; //Page scaling (height)
//vertical position needs to take into account the new page height taking new UserUnit in affect
verticalPosition = verticalPosition - (pdfReader.GetPageSize(pagenumber).Height / USERUnitNewValue);
cb.AddTemplate(writer.GetImportedPage(pdfReader, pagenumber), heightfactor, 0, 0, widthfactor, 0, verticalPosition);
}
else
{
break;
}
}
newPdf.Close();
Thank you again for your help,
Mike

How to maintain cropped information in pdf page after page extraction with itextsharp?

I am using acrobat 8.0 professional for cropping text from a PDF.
One page of Original pdf is
After cropping pdf above page is
In mine project i am using cropped pdf and extract individual pages from it by following code
private void ExtractPages(string inputFile, string outputFile, int start, int end)
{
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
//var pgSize = new iTextSharp.text.Rectangle(myWidth, myHeight);
//var doc = new iTextSharp.text.Document(pgSize, leftMargin, rightMargin, topMargin, bottomMargin);
// load the input document
Document inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++)
{
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i));
inputDoc.NewPage();
PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
inputDoc.Close();
}
}
Problem is that after page extraction cropping information is not retained in extracted pdf. Extracted pdf is same as original pdf with extra text in it.
How to retained cropped information in extracted pdf ?

Adding white space in PDF using iTextSharp

I have PDF where some of the text are very closed to the left and right border and I would like to add more white space at the border, top bottom left and right, for each pages in the PDF using iTextSharp.
Is this possible using iTextSharp or there is a better way?
I managed to find the answer. The code below adjusts the size of the pages:
var inputPdf = new PdfReader(inputFile); // Get input document
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
end = pageCount;
var inputDoc = new Document(inputPdf.GetPageSizeWithRotation(1));
using (var fs = new FileStream(outputFile, FileMode.Create))
{
var outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// Copy pages from input to output document
for (int i = start; i <= end; i++)
{
var existingRec = inputPdf.GetPageSizeWithRotation(i);
var newRec = new Rectangle(0.0f, 0.0f, existingRec.Width + 50, existingRec.Height + 25, 0);
inputDoc.SetPageSize(newRec);
inputDoc.NewPage();
PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height);
else cb1.AddTemplate(page, 1f, 0, 0, 1f, 25, 13);
}
inputDoc.Close();
}
Hope this helps someone.

Failed to load PDF document

//the func merges the src pdf with the memory stream, wherein the stream may contain
//few othr src pdf streams in previous calls to this func
//in first cal, ms would be null
public static void MergePdf(MemoryStream ms, string srcFile)
{
PdfReader reader = new PdfReader(srcFile);
Document document = null;
PdfWriter writer = null;
int n = reader.NumberOfPages;
if (document == null)
{
document = new Document(reader.GetPageSizeWithRotation(1));
writer = PdfWriter.GetInstance(document, ms);
document.Open();
}
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
}
I write the Memory stream, back to context.response.Outputstream; but the pdf doesnt load, the page 'Failed to load Pdf' results; Is there any problem in converting btwn memorystream and Pdf contents, or what may be the issue?
I had the same problem, and it turned out that the cause was not closing the document, and adding the following line of code:
document.Close();
should fix the problem.

Rotating PDF in C# using iTextSharp

I am using the below function to split the pdf into two.
Though it is spliting the pdf, the content is appearing upside down. How do I rotate it by 180 degrees.
Please help. below is the code for the same
private static void ExtractPages(string inputFile, string outputFile,
int start, int end)
{
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
// load the input document
Document inputDoc =
new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++)
{
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1));
inputDoc.NewPage();
PdfImportedPage page =
outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
inputPdf.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
inputDoc.Close();
}
}
I have found the above answers do not rotate correctly for all 4 of the main rotations.
Below is my code to handle 0, 90, 180 and 270 correctly. This has been tested with a PDF rotated in each of these directions.
var pageRotation = reader.GetPageRotation(currentPageIndex);
var pageWidth = reader.GetPageSizeWithRotation(currentPageIndex).Width;
var pageHeight = reader.GetPageSizeWithRotation(currentPageIndex).Height;
switch (pageRotation)
{
case 0:
writer.DirectContent.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
break;
case 90:
writer.DirectContent.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pageHeight);
break;
case 180:
writer.DirectContent.AddTemplate(importedPage, -1f, 0, 0, -1f, pageWidth, pageHeight);
break;
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
break;
default:
throw new InvalidOperationException(string.Format("Unexpected page rotation: [{0}].", pageRotation));
}
You should try this. It worked for me:
if (rotation == 90 || rotation == 270)
{
if (rotation == 90)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
}
if (rotation == 270)
{
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(pagenumber).Width, 0);
}
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
#TimS' answer was very close to perfect, and provided the correct parameters to AddTemplate, but I needed to make a few additions to allow for 90, 180, 270 rotation of a PDF where the pages already have rotation of 0, 90, 180 or 270:
Assuming a parameter of RotateFlipType rotateFlipType is passed to the function to specify the rotation (the handy enum from the GDI+ RotateFlip call):
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage page;
int rotation;
int i = 0;
while (i < pageCount)
{
i++;
var pageSize = reader.GetPageSizeWithRotation(i);
// Pull in the page from the reader
page = writer.GetImportedPage(reader, i);
// Get current page rotation in degrees
rotation = pageSize.Rotation;
// Default to the current page size
iTextSharp.text.Rectangle newPageSize = null;
// Apply our additional requested rotation (switch height and width as required)
switch (rotateFlipType)
{
case RotateFlipType.RotateNoneFlipNone:
newPageSize = new iTextSharp.text.Rectangle(pageSize);
break;
case RotateFlipType.Rotate90FlipNone:
rotation += 90;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
break;
case RotateFlipType.Rotate180FlipNone:
rotation += 180;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Width, pageSize.Height, rotation);
break;
case RotateFlipType.Rotate270FlipNone:
rotation += 270;
newPageSize = new iTextSharp.text.Rectangle(pageSize.Height, pageSize.Width, rotation);
break;
}
// Cap rotation into the 0-359 range for subsequent check
rotation %= 360;
document.SetPageSize(newPageSize);
document.NewPage();
// based on the rotation write out the page dimensions
switch (rotation)
{
case 0:
cb.AddTemplate(page, 0, 0);
break;
case 90:
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height);
break;
case 180:
cb.AddTemplate(page, -1f, 0, 0, -1f, newPageSize.Width, newPageSize.Height);
break;
case 270:
cb.AddTemplate(page, 0, 1f, -1f, 0, newPageSize.Width, 0);
break;
default:
throw new System.Exception(string.Format("Unexpected rotation of {0} degrees", rotation));
break;
}
}
Hopefully this will help someone else wishing to correct the rotation of incoming PDFs. Took me 2 days to perfect it.
I tried your code and it worked fine for me; split pages kept their original orientation.
A workaround might be to explicitly rotate your pages 180 degrees.
Replace:
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
With:
cb1.AddTemplate(page, -1f, 0, 0, -1f,
inputPdf.GetPageSizeWithRotation(i).Width,
inputPdf.GetPageSizeWithRotation(i).Height);
If your call to inputPdf.GetPageRotation(i) returns 180 then you can handle this in the if statement that follows (using my suggested code for rotation == 180).
A little change in above code
old code
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageWidth, 0);
new code
case 270:
writer.DirectContent.AddTemplate(importedPage, 0, 1f, -1f, 0, pageHeight, 0);
This will fix the issue with 270 degree rotation

Categories