InspectaPDFwithiText
45
HowtoGetPDFpagewidthandheight?
IhaveaPDF,andIwanttogetthewidthandheightofeachpageusingiTextSharp.This
iswhatIhavesofar:
string source=@"D:\pdf\test.pdf";
PdfReader reader = new PdfReader(source);
PostedonStackOverflowonAug13,2013⁸³byMohamedKamal⁸⁴
DoyouwanttheMediaBox?
Rectangle mediabox = = reader.GetPageSize(page);
Doyouwanttherotation?
int rotation = reader.GetPageRotation(page);
Doyouwantthecombinationofboth?
Rectangle pagesize = = reader.GetPageSizeWithRotation(page);
DoyouwanttheCropBox?
Rectangle cropbox = reader.GetCropBox(page);
Thesearesomemethodsthatwillgiveyouinformationaboutthedimensionsofapage.Mostof
themreturnanobjectoftype
Rectangle
thathasmethodssuchas
getWidth()
and
getHeight()
to
getthewidthandtheheightofthepage.Otherusefulmethodsare
getLeft()
and
getRight()
as
wellas
getTop()
and
getBottom()
.Thesefourmethodsreturnthe
x
and
y
coordinatesthatdefine
theboundariesofyourpage.
⁸³
http://stackoverflow.com/questions/18202660/how-to-get-pdf-page-width-and-height
⁸⁴
http://stackoverflow.com/users/2677532/mohamed-kamal
Pdf page size - Compress reduce PDF size in C#.net, ASP.NET, MVC, Ajax, WinForms, WPF
C# Code & .NET API to Compress & Decompress PDF Document
pdf custom paper size; adjust size of pdf
Pdf page size - VB.NET PDF File Compress Library: Compress reduce PDF size in vb.net, ASP.NET, MVC, Ajax, WinForms, WPF
VB.NET PDF Document Compression and Decompression Control SDK
change font size fillable pdf; change paper size pdf
ManipulatingexistingPDFs
Inthischapter,we’regoingtosolvesomeproblemswhenworkingwithexistingPDFsthatneedtobe
splitintodifferentfiles,mergedorstamped.Usually,wearegoingtouseacombinationof
PdfReader
toreadthedocumentand
PdfStamper
,
PdfCopy
or
PdfSmartCopy
tocreateanewPDF.Notethatwe’ll
skipfillingoutinteractiveformsfornow.We’lldealwithAcroFormandXFAtechnologyinthenext
chapter.
HowtoupdateaPDFwithoutcreatinganewPDF?
Ineed tochangethe value of afieldin an existing PDFfile.I am using
PdfReader
,
PdfStamper
and
AcroFields
andthat’sworkingfine.But,indoingso,itisrequiredto
createanewPDFandIwouldlikethechangetobereflectedintheexistingPDFitself.
IfIamsettingthedestination
filename
tobethesameastheoriginal
filename
,thenmy
applicationfails.
PostedonStackOverflowonApr18,2013⁸⁵bytk2013⁸⁶
Youcan’treadafileandwritetoitsimultaneously.ThinkofhowMicrosoftWordworks:youcan’t
openaWorddocumentandwritedirectlytoit.Wordalwayscreatesatemporaryfile,writesthe
changestoit,thenreplacestheoriginalfilewithit,andthenthrowsawaythetemporaryfile.
Youcandothattoo:
• readtheoriginalfilewith
PdfReader
;
• createatemporaryfilefor
PdfStamper
,andwhenyou’redone,
• replacetheoriginalfilewiththetemporaryfile.
Or:
• readtheoriginalfileintoa
byte[]
,
• create
PdfReader
withthis
byte[]
,and
• usethepathtotheoriginalfilefor
PdfStamper
.
Thelatteroptionismoredangerous,asyou’lllosetheoriginalfileifyoudosomethingthatcauses
anexceptionin
PdfStamper
.IfIwereyou,I’dcreateatemporaryfile.
⁸⁵
http://stackoverflow.com/questions/16081831/using-itextsharp-stamper-required-to-update-in-the-same-pdf
⁸⁶
http://stackoverflow.com/users/2239456/tk2013
C# PDF File Split Library: Split, seperate PDF into multiple files
Divide PDF file into multiple files by outputting PDF file size. control, C# developers can easily and accurately disassemble multi-page PDF document into two
best pdf compressor; change font size in pdf form field
C# PDF Thumbnail Create SDK: Draw thumbnail images for PDF in C#.
public override Bitmap ConvertToImage(Size targetSize). Description: Convert the PDF page to bitmap with specified size. Parameters:
change font size in pdf file; best pdf compression
ManipulatingexistingPDFs
47
HowtoaddanimagewatermarktoaPDFfile?
I’musingC#andiTextSharptoaddawatermarktomyPDFfiles:
Document document = new Document();
PdfReader pdfReader r = new PdfReader(strFileLocation);
PdfStamper pdfStamper r = new PdfStamper(pdfReader, new FileStream(strFileLocation\
Out, FileMode.Create, FileAccess.Write, , FileShare.None));
iTextSharp.text.Image img = = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(100300);
PdfContentByte waterMark;
for (int pageIndex = 1; pageIndex x <= pdfReader.NumberOfPages; pageIndex++) ) {
waterMark = = pdfStamper.GetOverContent(pageIndex);
waterMark.AddImage(img);
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
Itworksfine,butmyproblemisthatinsomePDFfilesnowatermarkisaddedalthough
thefilesizeincreased,anyidea?
PostedonStackOverflowonJul8,2013⁸⁷byAbady⁸⁸
Thefactthatthefilesizeincreasesisagoodindicationthatthewatermarkisadded.Themain
problemisthatyou’readdingthewatermarkoutsidethevisibleareaofthepage.Seemyanswerto
thequestionHowtopositiontextrelativetopageusingiText?
Youneedsomethinglikethis:
Rectangle pagesize reader.getCropBox(pageIndex);
if (pagesize == null)
pagesize reader.getMediaBox(pageIndex);
img.SetAbsolutePosition(
pagesize.GetLeft(),
pagesize.GetBottom());
Thatis:ifyouwanttoaddtheimageinthelower-leftcornerofthepage.Youcanaddanoffset,but
makesuretheoffsetinthexdirectiondoesn’texceedthewidthofthepage,andtheoffsetinthey
directiondoesn’texceedtheheightofthepage.
⁸⁷
http://stackoverflow.com/questions/17522965/how-to-add-a-watermark-to-a-pdf-file
⁸⁸
http://stackoverflow.com/users/1450667/abady
VB.NET PDF File Split Library: Split, seperate PDF into multiple
Separate source PDF document file by defined page range in VB.NET class application. Divide PDF file into multiple files by outputting PDF file size.
best pdf compression tool; best compression pdf
VB.NET PDF Thumbnail Create SDK: Draw thumbnail images for PDF in
Program.RootPath + "\\" 1.pdf" Dim doc As PDFDocument = New PDFDocument(inputFilePath) Dim page As PDFPage = doc.GetPage(0) ' Define the size of the
apple compress pdf; adjust file size of pdf
ManipulatingexistingPDFs
48
Whydoesthefunctiontoconcatenate/mergePDFs
causeissuesinsomecases?
I’musingthefollowingcodetomergePDFstogetherusingiText:
public static void concatenatePdfs(List<FilelistOfPdfFilesFile outputFile)
throws DocumentExceptionIOException {
Document document new Document();
FileOutputStream outputStream new FileOutputStream(outputFile);
PdfWriter writer PdfWriter.getInstance(documentoutputStream);
document.open();
PdfContentByte cb writer.getDirectContent();
for (File inFile listOfPdfFiles) {
PdfReader reader new PdfReader(inFile.getAbsolutePath());
for (int = 1; <= reader.getNumberOfPages(); i++) {
document.newPage();
PdfImportedPage page writer.getImportedPage(readeri);
cb.addTemplate(page, 0, , 0);
}
}
document.close();
}
Thisusuallyworksgreat!Butonceandawhile,it’srotatingsomeofthepagesby90
degrees?Anyoneeverhavethishappen?
PostedonStackOverflowonApr14,2014⁸⁹byNicholasDiPiazza⁹⁰
Thereareerrorsonceinawhilebecauseyouareusingthewrongmethodtoconcatenatedocuments.
Youshouldnotuse
PdfWriter
toconcatenate(ormerge)PDFdocuments.Thatiswrongbecause:
• Youcompletelyignorethepagesizeofthepagesintheoriginaldocument(youassumethey
areallofsizeA4),
• Youignorepageboundariessuchasthecropbox(ifpresent),
• Youignoretherotationvaluestoredinthepagedictionary,
• Youthrowawayallinteractivitythatispresentintheoriginaldocument,andsoon.
ConcatenatingPDFsisdoneusing
PdfCopy
,seeforinstance:
⁸⁹
http://stackoverflow.com/questions/23062345/function-that-can-use-itext-to-concatenate-merge-pdfs-together-causing-some
⁹⁰
http://stackoverflow.com/users/1174024/nicholas-dipiazza
C# PDF Convert to Jpeg SDK: Convert PDF to JPEG images in C#.net
Using this C#.NET PDF to JPEG conversion library component toolkit, C# developers can easily and quickly convert a large-size multi-page PDF document to a
change page size pdf acrobat; change font size in pdf fillable form
C# PDF Convert to Tiff SDK: Convert PDF to tiff images in C#.net
zoomValue, The magnification of the original PDF page size. 0.1f
pdf page size; reduce pdf file size
ManipulatingexistingPDFs
49
Document document new Document();
PdfCopy copy new PdfSmartCopy(documentnew FileOutputStream(dest));
document.open();
PdfReader reader;
String line br.readLine();
// loop over readers
// add d the e PDF to PdfCopy
reader new PdfReader(baos.toByteArray());
copy.addDocument(reader);
reader.close();
// end loop
document.close();
Ifyouaremergingdocumentsthatcontainfields,youneedtoaddthefollowingline:
copy.SetMergeFields();
C# PDF Convert to Word SDK: Convert PDF to Word library in C#.net
zoomValue, The magnification of the original PDF page size. 0.1f
pdf markup text size; change file size of pdf
C# Convert: PDF to Word: How to Convert Adobe PDF to Microsoft
Support fast Word and PDF conversion with original document page size remained. Microsoft Office Word 2003 (.doc) and 2007 (.docx) versions are available.
pdf compressor; change font size pdf
ManipulatingexistingPDFs
50
Howtomergedocumentscorrectly?
Iwouldliketoaddalinktoanexistingpdfthatjumpstoacoordinateonanotherpage.
IhavethefollowingproblemwhenprintingthePDFfileaftermerge,thePDFdocuments
getcutoff.Sometimesthishappensbecausethedocumentsaren’t8.5x11whereasthepage
sizemightbe11x17.
Istheresomewaytodetectthepagesizeandthenusethatsamepagesizeforthose
documents?Or,ifnot,isitpossibletohaveitfittopage?Thisismycode:
Document document new Document();
PdfWriter writer PdfWriter.getInstance(documentoutputStream);
document.open();
BaseFont bf BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252BaseFont.NOT_EMBEDDED);
PdfContentByte cb writer.getDirectContent();
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator<PdfReaderiteratorPDFReader readers.iterator();
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader iteratorPDFReader.next();
while (pageOfCurrentReaderPDF pdfReader.getNumberOfPages()) {
Rectangle r pdfReader.getPageSize(
pdfReader.getPageN(pageOfCurrentReaderPDF + 1));
if(r.getWidth()==792.0 && r.getHeight()==612.0)
document.setPageSize(PageSize.A4.rotate());
else
document.setPageSize(PageSize.A4);
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page writer.getImportedPage(pdfReaderpageOfCurrentReaderPDF);
cb.addTemplate(page, 0, , 0);
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER""
currentPageNumber " of " totalPages, 520, 5, 0);
cb.endText();
}
pageOfCurrentReaderPDF = 0;
}
document.close();
ManipulatingexistingPDFs
51
Screenshot
PostedonStackOverflowonFeb12,2014⁹¹bySumitVaidya⁹²
Using
PdfWriter
tomergedocumentsisabadidea.ThishasbeenexplainedonStackOverflowmany
times!
Mergingdocumentsisdoneusing
PdfCopy
(or
PdfSmartCopy
).
Ifyouneedanexample,seeforinstancetheFillFlattenMerge2⁹³example:
Document document new Document();
PdfCopy copy new PdfSmartCopy(documentnew FileOutputStream(dest));
document.open();
PdfReader reader;
String line br.readLine();
// loop over readers
// add d the e PDF to PdfCopy
reader new PdfReader(baos.toByteArray());
copy.addDocument(reader);
⁹¹
http://stackoverflow.com/questions/21731439/pdf-page-cutting-through-itext-api
⁹²
http://stackoverflow.com/users/2853641/sumit-vaidya
⁹³
http://itextpdf.com/sandbox/acroforms/reporting/FillFlattenMerge2
ManipulatingexistingPDFs
52
reader.close();
// end loop
document.close();
Inyourcase,youalsoneedtoaddpagenumbers,youcandothisinasecondgo,asisdoneinthe
StampPageXofY⁹⁴example:
PdfReader reader new PdfReader(src);
int reader.getNumberOfPages();
PdfStamper stamper new PdfStamper(readernew FileOutputStream(dest));
PdfContentByte pagecontent;
for (int = 0; n; ) {
pagecontent stamper.getOverContent(++i);
ColumnText.showTextAligned(pagecontentElement.ALIGN_RIGHT,
new Phrase(String.format("page %s of f %s"in)), 559, 806, 0);
}
stamper.close();
reader.close();
Oryoucanaddthemwhilemerging,asisdoneintheMergeWithToc⁹⁵example.
Document document new Document();
PdfCopy copy new PdfCopy(documentnew FileOutputStream(filename));
PageStamp stamp;
document.open();
int n;
int pageNo = 0;
PdfImportedPage page;
Chunk chunk;
for (Map.Entry<StringPdfReaderentry filesToMerge.entrySet()) {
entry.getValue().getNumberOfPages();
for (int = 0; n; ) ) {
pageNo++;
page copy.getImportedPage(entry.getValue(), ++i);
stamp copy.createPageStamp(page);
chunk new Chunk(String.format("Page %d"pageNo));
if (== 1)
chunk.setLocalDestination("p" pageNo);
ColumnText.showTextAligned(stamp.getUnderContent(),
⁹⁴
http://itextpdf.com/sandbox/stamper/StampPageXofY
⁹⁵
http://itextpdf.com/sandbox/merge/MergeWithToc
ManipulatingexistingPDFs
53
Element.ALIGN_RIGHTnew Phrase(chunk),
559, 810, 0);
stamp.alterContents();
copy.addPage(page);
}
}
document.close();
for (PdfReader r filesToMerge.values()) {
r.close();
}
reader.close();
Istronglyadviseagainstusing
PdfWriter
tomergedocuments!Asdocumented,youarethrowing
awayallannotationsbyadding
PdfImportedPage
instancestoadocumentusing
addTemplate()
.
Thisistypicallynotwhatyouwant.You’reonlymakingitharderonyourselfifyouinsistonusing
thatclass.Idon’tunderstandwhysomanypeopleusethewrongapproachtomergedocuments.I
blametheunofficialdocumentationforthepopularityofthiswrongapproach.
Interactiveforms
IsyourformbasedonAcroFormtechnologyorisitbasedontheXMLFormsArchitecture?That’s
acommoncounter-questionyou’llbeconfrontedwithwhenaskingaquestionaboutforms.Inany
case,theseanswersshouldhelpyousolvingthemostcommonproblemswithrespecttoforms.
Howtofilloutapdffileprogrammatically?(AcroForm
technology)
Whattechniquesavailabletofillapdfformautomaticallyusingexternaldataandsave
them.IhavetousedatafromadatabasetofillatemplatePDFandsaveacopyofiton
diskwiththatdata.Languageandplatformisnotissuebutitwouldbegoodifitcanrun
onwindowsandLinux.
PostedonStackOverflowonJun24,2010⁹⁶byaffan⁹⁷
IfyourformisbasedonAcroFormtechnology,youcanuseiTexttofillitoutlikethis:
PdfReader reader new PdfReader(src);
PdfStamper stamper new PdfStamper(readernew FileOutputStream(dest));
AcroFields form stamper.getAcroFields();
form.setField(keyvalue);
stamper.setFormFlattening(true);
stamper.close();
reader.close();
Inthissnippet,
src
isthesourceofaPDFfile(couldbeapathtoafile,couldbea
byte[]
)and
dest
isthepathtotheresultingPDF.The
key
correspondswiththenameofafieldinyourtemplate.The
value
correspondswiththevalueyouwanttofillin.Ifyouwanttheformtokeepitsinteractivity,
youneedtoremovetheline
stamper.setFormFlattening(true);
otherwiseallformfieldswillbe
removed,resultinginaflatPDF.
⁹⁶
http://stackoverflow.com/questions/3108704/how-to-fill-out-a-pdf-file-programatically
⁹⁷
http://stackoverflow.com/users/109769/affan
Documents you may be interested
Documents you may be interested