ネクロマンシング:
mkl のコードを実践に移す (ベクトル グラフィックスの左上隅と右下隅に小さな白いテキストを入れるだけです):
public static void StartManipulation()
{
byte[] ba = System.IO.File.ReadAllBytes(@"D:\username\Documents\Downloads\itextsharp-master\itextsharp-master\src\CropTest\Files\dwg305.pdf");
// FindBoundingBox(ba);
ba = ManipulatePdf(ba);
System.IO.File.WriteAllBytes(@"D:\username\Downloads\mysizedpdf.pdf", ba);
} // End Sub StartManipulation
public static byte[] ManipulatePdf(byte[] src)
{
byte[] byteBuffer = null;
using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(src))
{
iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
int n = reader.NumberOfPages;
iTextSharp.text.pdf.PdfDictionary pageDict;
for (int pageNumber = 1; pageNumber <= n; pageNumber++)
{
pageDict = reader.GetPageN(pageNumber);
iTextSharp.text.pdf.parser.TextMarginFinder finder = parser.ProcessContent(pageNumber, new iTextSharp.text.pdf.parser.TextMarginFinder());
// iTextSharp.text.Rectangle pageSize = reader.GetPageSize(pageNumber);
// Get Content Size
float Llx = finder.GetLlx();
float Lly = finder.GetLly();
float Urx = finder.GetUrx();
float Ury = finder.GetUry();
//iTextSharp.text.pdf.PdfRectangle rect = new iTextSharp.text.pdf.PdfRectangle(55, 76, 560, 816);
//iTextSharp.text.pdf.PdfRectangle rectTextContentSize = new iTextSharp.text.pdf.PdfRectangle(Llx, Lly, Urx, Ury);
int SafetyMargin = 100;
iTextSharp.text.pdf.PdfRectangle rectTextContentSize = new iTextSharp.text.pdf.PdfRectangle(Llx - SafetyMargin, Lly - SafetyMargin, Urx + SafetyMargin, Ury + SafetyMargin);
pageDict.Put(iTextSharp.text.pdf.PdfName.CROPBOX, rectTextContentSize);
} // Next i
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, ms))
{ }
byteBuffer = ms.ToArray();
} // End Using ms
} // End Using reader
return byteBuffer;
} // End Function ManipulatePdf
public static System.Drawing.Size FindBoundingBox(byte[] src)
{
System.Drawing.Size sze = default(System.Drawing.Size);
// iTextSharp.text.pdf
// iTextSharp.text.pdf.parser
using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(src))
{
iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(reader);
for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
{
iTextSharp.text.pdf.parser.TextMarginFinder finder = parser.ProcessContent(pageNumber, new iTextSharp.text.pdf.parser.TextMarginFinder());
iTextSharp.text.Rectangle pageSize = reader.GetPageSize(pageNumber);
float Llx = finder.GetLlx();
float Lly = finder.GetLly();
float Urx = finder.GetUrx();
float Ury = finder.GetUry();
float PdfSharpLly = pageSize.Height - Lly;
float PdfSharpUry = pageSize.Height - Ury;
sze = new System.Drawing.Size((int)(Urx - Llx), (int)(Ury - Lly));
System.Console.WriteLine("Width: {0}<r\nHeight: {1}", pageSize.Width, pageSize.Height);
System.Console.WriteLine("Llx: {0}\r\nLly: {1}\r\nUrx: {2}\r\nUry: {3}\r\n", Llx, Lly, Urx, Ury);
} // Next pageNumber
} // End Using reader
return sze;
} // End Function FindBoundingBox