Ejemplo n.º 1
0
        private void pocketmodstyle()
        {
            PdfReader pr = new PdfReader(ofd.FileName);

            if (pr.IsEncrypted())
            {
                MessageBox.Show(ofd.FileName + " is protected.",
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            try
            {
                var dst = PageSize.GetRectangle(comboBox1.SelectedItem.ToString());
                var doc = new Document(dst);
                var pw  = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
                doc.Open();

                for (var page = 1; page <= pr.NumberOfPages; page++)
                {
                    if (page % 8 == 1)
                    {
                        doc.NewPage();
                    }

                    var pcb = pw.DirectContent;
                    var src = pr.GetPageSize(page);
                    int rot = pr.GetPageRotation(page);

                    bool  isLandscape = (src.Width > src.Height) ? true : false;
                    float scale;
                    float offset;
                    if (isLandscape)
                    {
                        scale  = dst.Height / (src.Height * 4);
                        offset = (dst.Width / 2 - (scale * src.Width)) / 2;
                    }
                    else
                    {
                        scale  = dst.Height / (src.Width * 4);
                        offset = (dst.Width / 2 - (scale * src.Height)) / 2;
                    }

                    // For scaling and rotation
                    var transRotate = new iTextSharp.awt.geom.AffineTransform();
                    transRotate.SetToIdentity();
                    transRotate.Scale(scale, scale);

                    // For position
                    var transAdjust = new iTextSharp.awt.geom.AffineTransform();
                    transAdjust.SetToIdentity();

                    // Affine translation of PocketMod style.
                    if (isLandscape || (rot == 90 || rot == 270))
                    {
                        var px   = dst.Width / 2;
                        var py   = dst.Height / 4;
                        var rads = 180 * Math.PI / 180;

                        switch (page % 8)
                        {
                        // LEFT SIDE
                        // Rotate 180 degrees
                        case 1:
                            transAdjust.Translate(px - offset, py * 4);
                            transRotate.Rotate(-rads);
                            break;

                        case 0:     // as 8
                            transAdjust.Translate(px - offset, py * 3);
                            transRotate.Rotate(-rads);
                            break;

                        case 7:
                            transAdjust.Translate(px - offset, py * 2);
                            transRotate.Rotate(-rads);
                            break;

                        case 6:
                            transAdjust.Translate(px - offset, py);
                            transRotate.Rotate(-rads);
                            break;

                        // RIGHT SIDE
                        // no rotation
                        case 2:
                            transAdjust.Translate(px + offset, py * 3);
                            break;

                        case 3:
                            transAdjust.Translate(px + offset, py * 2);
                            break;

                        case 4:
                            transAdjust.Translate(px + offset, py);
                            break;

                        case 5:
                            transAdjust.Translate(px + offset, 0);
                            break;
                        }
                    }
                    else
                    {
                        var px   = dst.Width / 2;
                        var py   = dst.Height / 4;
                        var rads = 90 * Math.PI / 180;

                        switch (page % 8)
                        {
                        // LEFT SIDE
                        // Rotate counterclockwise 90 degrees
                        case 1:
                            transAdjust.Translate(px - offset, py * 3);
                            transRotate.Rotate(rads);
                            break;

                        case 0:     // as 8
                            transAdjust.Translate(px - offset, py * 2);
                            transRotate.Rotate(rads);
                            break;

                        case 7:
                            transAdjust.Translate(px - offset, py);
                            transRotate.Rotate(rads);
                            break;

                        case 6:
                            transAdjust.Translate(px - offset, 0);
                            transRotate.Rotate(rads);
                            break;

                        // RIGHT SIDE
                        // Rotate clockwise 90 degrees
                        case 2:
                            transAdjust.Translate(px + offset, py * 4);
                            transRotate.Rotate(-rads);
                            break;

                        case 3:
                            transAdjust.Translate(px + offset, py * 3);
                            transRotate.Rotate(-rads);
                            break;

                        case 4:
                            transAdjust.Translate(px + offset, py * 2);
                            transRotate.Rotate(-rads);
                            break;

                        case 5:
                            transAdjust.Translate(px + offset, py);
                            transRotate.Rotate(-rads);
                            break;
                        }
                    }
                    var finalTrans = new iTextSharp.awt.geom.AffineTransform();
                    finalTrans.SetToIdentity();
                    finalTrans.Concatenate(transAdjust);
                    finalTrans.Concatenate(transRotate);

                    var importedPage = pw.GetImportedPage(pr, page);
                    pcb.AddTemplate(importedPage, finalTrans);

                    if ((page % 8 == 0) || (page == pr.NumberOfPages))
                    {
                        // Draw guide line for folding.
                        pcb.SetLineWidth(0.01f);

                        // Outside frame
                        if (checkBox1.Checked)
                        {
                            pcb.MoveTo(0f, 0f);
                            pcb.LineTo(dst.Width, 0f);
                            pcb.LineTo(dst.Width, dst.Height);
                            pcb.LineTo(0f, dst.Height);
                            pcb.LineTo(0f, 0f);
                            pcb.Stroke();
                        }

                        pcb.MoveTo(0f, dst.Height * 3 / 4f);
                        pcb.LineTo(dst.Width, dst.Height * 3 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(0f, dst.Height * 2 / 4f);
                        pcb.LineTo(dst.Width, dst.Height * 2 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(0f, dst.Height * 1 / 4f);
                        pcb.LineTo(dst.Width, dst.Height * 1 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(dst.Width / 2, 0f);
                        pcb.LineTo(dst.Width / 2, dst.Height * 1 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(dst.Width / 2, dst.Height * 3 / 4);
                        pcb.LineTo(dst.Width / 2, dst.Height);
                        pcb.Stroke();

                        pcb.SetLineDash(3f, 3f);
                        pcb.MoveTo(dst.Width / 2, dst.Height * 1 / 4);
                        pcb.LineTo(dst.Width / 2, dst.Height * 3 / 4);
                        pcb.Stroke();

                        pcb.SetLineDash(0);
                    }
                }

                doc.Close();
                pw.Close();
                pr.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
        }
Ejemplo n.º 2
0
        private void pocketmodstyle()
        {
            PdfReader pr = new PdfReader(ofd.FileName);
            if (pr.IsEncrypted())
            {
                MessageBox.Show(ofd.FileName + " is protected.",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                return;
            }

            try
            {

                var dst = PageSize.GetRectangle(comboBox1.SelectedItem.ToString());
                var doc = new Document(dst);
                var pw = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
                doc.Open();

                for (var page = 1; page <= pr.NumberOfPages; page++)
                {
                    if (page % 8 == 1) doc.NewPage();

                    var pcb = pw.DirectContent;
                    var src = pr.GetPageSize(page);
                    int rot = pr.GetPageRotation(page);

                    bool isLandscape = (src.Width > src.Height) ? true : false;
                    float scale;
                    float offset;
                    if (isLandscape)
                    {
                        scale = dst.Height / (src.Height * 4);
                        offset = (dst.Width / 2 - (scale * src.Width)) / 2;
                    }
                    else
                    {
                        scale = dst.Height / (src.Width * 4);
                        offset = (dst.Width / 2 - (scale * src.Height)) / 2;
                    }

                    // For scaling and rotation
                    var transRotate = new iTextSharp.awt.geom.AffineTransform();
                    transRotate.SetToIdentity();
                    transRotate.Scale(scale, scale);

                    // For position
                    var transAdjust = new iTextSharp.awt.geom.AffineTransform();
                    transAdjust.SetToIdentity();

                    // Affine translation of PocketMod style.
                    if (isLandscape || (rot == 90 || rot == 270))
                    {
                        var px = dst.Width / 2;
                        var py = dst.Height / 4;
                        var rads = 180 * Math.PI / 180;

                        switch (page % 8)
                        {
                            // LEFT SIDE
                            // Rotate 180 degrees
                            case 1:
                                transAdjust.Translate(px - offset, py * 4);
                                transRotate.Rotate(-rads);
                                break;
                            case 0: // as 8
                                transAdjust.Translate(px - offset, py * 3);
                                transRotate.Rotate(-rads);
                                break;
                            case 7:
                                transAdjust.Translate(px - offset, py * 2);
                                transRotate.Rotate(-rads);
                                break;
                            case 6:
                                transAdjust.Translate(px - offset, py);
                                transRotate.Rotate(-rads);
                                break;

                            // RIGHT SIDE
                            // no rotation
                            case 2:
                                transAdjust.Translate(px + offset, py * 3);
                                break;
                            case 3:
                                transAdjust.Translate(px + offset, py * 2);
                                break;
                            case 4:
                                transAdjust.Translate(px + offset, py);
                                break;
                            case 5:
                                transAdjust.Translate(px + offset, 0);
                                break;
                        }

                    }
                    else
                    {
                        var px = dst.Width / 2;
                        var py = dst.Height / 4;
                        var rads = 90 * Math.PI / 180;

                        switch (page % 8)
                        {
                            // LEFT SIDE
                            // Rotate counterclockwise 90 degrees
                            case 1:
                                transAdjust.Translate(px - offset, py * 3);
                                transRotate.Rotate(rads);
                                break;
                            case 0: // as 8
                                transAdjust.Translate(px - offset, py * 2);
                                transRotate.Rotate(rads);
                                break;
                            case 7:
                                transAdjust.Translate(px - offset, py);
                                transRotate.Rotate(rads);
                                break;
                            case 6:
                                transAdjust.Translate(px - offset, 0);
                                transRotate.Rotate(rads);
                                break;

                            // RIGHT SIDE
                            // Rotate clockwise 90 degrees
                            case 2:
                                transAdjust.Translate(px + offset, py * 4);
                                transRotate.Rotate(-rads);
                                break;
                            case 3:
                                transAdjust.Translate(px + offset, py * 3);
                                transRotate.Rotate(-rads);
                                break;
                            case 4:
                                transAdjust.Translate(px + offset, py * 2);
                                transRotate.Rotate(-rads);
                                break;
                            case 5:
                                transAdjust.Translate(px + offset, py);
                                transRotate.Rotate(-rads);
                                break;
                        }

                    }
                    var finalTrans = new iTextSharp.awt.geom.AffineTransform();
                    finalTrans.SetToIdentity();
                    finalTrans.Concatenate(transAdjust);
                    finalTrans.Concatenate(transRotate);

                    var importedPage = pw.GetImportedPage(pr, page);
                    pcb.AddTemplate(importedPage, finalTrans);

                    if ((page % 8 == 0)||(page == pr.NumberOfPages))
                    {
                        // Draw guide line for folding.
                        pcb.SetLineWidth(0.01f);

                        // Outside frame
                        if (checkBox1.Checked)
                        {
                            pcb.MoveTo(0f, 0f);
                            pcb.LineTo(dst.Width, 0f);
                            pcb.LineTo(dst.Width, dst.Height);
                            pcb.LineTo(0f, dst.Height);
                            pcb.LineTo(0f, 0f);
                            pcb.Stroke();
                        }

                        pcb.MoveTo(0f, dst.Height * 3 / 4f);
                        pcb.LineTo(dst.Width, dst.Height * 3 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(0f, dst.Height * 2 / 4f);
                        pcb.LineTo(dst.Width, dst.Height * 2 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(0f, dst.Height * 1 / 4f);
                        pcb.LineTo(dst.Width, dst.Height * 1 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(dst.Width / 2, 0f);
                        pcb.LineTo(dst.Width / 2, dst.Height * 1 / 4);
                        pcb.Stroke();

                        pcb.MoveTo(dst.Width / 2, dst.Height * 3 / 4);
                        pcb.LineTo(dst.Width / 2, dst.Height);
                        pcb.Stroke();

                        pcb.SetLineDash(3f, 3f);
                        pcb.MoveTo(dst.Width / 2, dst.Height * 1 / 4);
                        pcb.LineTo(dst.Width / 2, dst.Height * 3 / 4);
                        pcb.Stroke();

                        pcb.SetLineDash(0);
                    }

                }

                doc.Close();
                pw.Close();
                pr.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }
        }