Skip to content

Commit

Permalink
+增加按首页统一页面尺寸(#219
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Nov 11, 2024
1 parent 0edbb78 commit ad398d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
10 changes: 10 additions & 0 deletions App/Functions/PatcherOptionsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ void _PageSizeBox_SelectedIndexChanged(object sender, EventArgs e) {
_ResizePdfPagesBox.Enabled =
_HeightBox.Enabled = false;
break;
case PaperSize.AsFirstPage:
_HeightBox.Enabled =
_WidthBox.Enabled =
false;
_AutoRotateBox.Enabled =
_ImageVAlignBox.Enabled =
_ImageHAlignBox.Enabled =
_ScalePdfPagesBox.Enabled =
_ResizePdfPagesBox.Enabled = true;
break;
case PaperSize.AsLargestPage:
case PaperSize.AsSmallestPage:
_AutoRotateBox.Enabled =
Expand Down
5 changes: 4 additions & 1 deletion App/Model/PaperSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class PaperSize
{
public const string AsPageSize = "等同原始内容尺寸";
public const string FixedWidthAutoHeight = "固定页宽自动高度";
public const string AsFirstPage = "等同第一页尺寸";
public const string AsSpecificPage = "等同指定页面尺寸";
public const string AsWidestPage = "宽度同最宽页面,自动高度";
public const string AsNarrowestPage = "宽度同最窄页面,自动高度";
Expand All @@ -27,6 +28,7 @@ public string PaperName {
AsNarrowestPage => SpecialPaperSize.AsNarrowestPage,
AsLargestPage => SpecialPaperSize.AsLargestPage,
AsSmallestPage => SpecialPaperSize.AsSmallestPage,
AsFirstPage => SpecialPaperSize.AsFirstPage,
_ => SpecialPaperSize.None,
};
}
Expand Down Expand Up @@ -98,7 +100,8 @@ public enum SpecialPaperSize
AsWidestPage,
AsNarrowestPage,
AsLargestPage,
AsSmallestPage
AsSmallestPage,
AsFirstPage
}

}
42 changes: 20 additions & 22 deletions App/Processor/ContentProcessors/PageDimensionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ internal static CoordinateTranslationSettings ResizePage(PdfDictionary page, Pag
}

static void ResizeBox(PdfDictionary page, Rectangle box, Rectangle refBox) {
page.Put(PdfName.MEDIABOX, new PdfArray(new float[]{
page.Put(PdfName.MEDIABOX, new PdfArray([
box.Left < refBox.Left ? box.Left : refBox.Left,
box.Bottom < refBox.Bottom ? box.Bottom : refBox.Bottom,
box.Right > refBox.Right ? box.Right : refBox.Right,
box.Top > refBox.Top ? box.Top : refBox.Top
}));
]));
}

static bool RotatePage(PdfDictionary page, PageBoxSettings settings) {
Expand Down Expand Up @@ -133,12 +133,8 @@ static bool RotatePage(PdfDictionary page, PageBoxSettings settings) {

static bool FilterPageNumber(int pageNumber, PageFilterFlag filter) {
var odd = (pageNumber & 1) > 0;
if (odd && (filter & PageFilterFlag.Even) == PageFilterFlag.Even
|| (odd == false && (filter & PageFilterFlag.Odd) == PageFilterFlag.Odd)
) {
return false;
}
return true;
return (!odd || (filter & PageFilterFlag.Even) != PageFilterFlag.Even)
&& (odd || (filter & PageFilterFlag.Odd) != PageFilterFlag.Odd);
}

/// <summary>
Expand All @@ -148,11 +144,11 @@ static bool FilterPageNumber(int pageNumber, PageFilterFlag filter) {
/// <param name="pageNumber">页码。</param>
/// <param name="ct">拉伸及平移参数。</param>
internal static byte[] ScaleContent(PdfReader pdf, int pageNumber, CoordinateTranslationSettings ct) {
var newContent = Encoding.ASCII.GetBytes(String.Join(" ", new string[] {
var newContent = Encoding.ASCII.GetBytes(String.Join(" ",
ct.XScale.ToText (), "0",
"0", ct.YScale.ToText (),
ct.XTranslation.ToText (), ct.YTranslation.ToText (), "cm "
}));
));
var cb = pdf.GetPageContent(pageNumber);
Array.Resize(ref newContent, cb.Length + newContent.Length);
cb.CopyTo(newContent, newContent.Length - cb.Length);
Expand All @@ -168,7 +164,7 @@ static void ScaleContent(PageProcessorContext context, CoordinateTranslationSett
if (cmds.Count > 0 && cmds[0].Type == PdfPageCommandType.Matrix) {
var c = cmds[0] as MatrixCommand;
if (c.Name.ToString() == "cm") {
c.Multiply(new double[] { ct.XScale, 0, 0, ct.YScale, ct.XTranslation, ct.YTranslation });
c.Multiply([ct.XScale, 0, 0, ct.YScale, ct.XTranslation, ct.YTranslation]);
}
}
else {
Expand All @@ -186,10 +182,10 @@ static void RewriteAnnotationCoordinates(CoordinateTranslationSettings ct, PdfDi
if (PdfReader.GetPdfObject(item) is PdfDictionary an) {
var rect = an.GetAsArray(PdfName.RECT);
if (rect != null && rect.Size == 4) {
rect[0] = new PdfNumber((rect[0] as PdfNumber).FloatValue * ct.XScale + ct.XTranslation);
rect[1] = new PdfNumber((rect[1] as PdfNumber).FloatValue * ct.YScale + ct.YTranslation);
rect[2] = new PdfNumber((rect[2] as PdfNumber).FloatValue * ct.XScale + ct.XTranslation);
rect[3] = new PdfNumber((rect[3] as PdfNumber).FloatValue * ct.YScale + ct.YTranslation);
rect[0] = new PdfNumber(((PdfNumber)rect[0]).FloatValue * ct.XScale + ct.XTranslation);
rect[1] = new PdfNumber(((PdfNumber)rect[1]).FloatValue * ct.YScale + ct.YTranslation);
rect[2] = new PdfNumber(((PdfNumber)rect[2]).FloatValue * ct.XScale + ct.XTranslation);
rect[3] = new PdfNumber(((PdfNumber)rect[3]).FloatValue * ct.YScale + ct.YTranslation);
}
}
}
Expand All @@ -214,12 +210,12 @@ static void AdjustBoxDimension(PdfDictionary page, Margins margins, PdfName boxN
return;
}
var r = PdfReader.GetNormalizedRectangle(b);
page.Put(boxName, new PdfArray(new float[] {
page.Put(boxName, new PdfArray([
r.Left - margins.Left,
r.Bottom - margins.Bottom,
r.Right + margins.Right,
r.Top + margins.Top
}));
]));
}

#region IPageProcessor 成员
Expand All @@ -241,6 +237,10 @@ public void BeginProcess(DocProcessorContext context) {
switch (Settings.PaperSize.SpecialSize) {
case SpecialPaperSize.AsSpecificPage:
break;
case SpecialPaperSize.AsFirstPage:
var r = context.Pdf.GetPageSizeWithRotation(1);
_refPaperSize = new PaperSize(Settings.PaperSize.PaperName, r.Width, r.Height);
break;
case SpecialPaperSize.AsWidestPage:
case SpecialPaperSize.AsNarrowestPage:
case SpecialPaperSize.AsLargestPage:
Expand Down Expand Up @@ -294,12 +294,11 @@ private PaperSize GetRefPaperSize(DocProcessorContext context) {

public bool Process(PageProcessorContext context) {
var f = Settings.Filter;
if (FilterPageNumber(context.PageNumber, f) == false) {
return false;
}
if (_pageRanges != null && _pageRanges.IsInRange(context.PageNumber) == false) {
if (FilterPageNumber(context.PageNumber, f) == false
|| _pageRanges != null && _pageRanges.IsInRange(context.PageNumber) == false) {
return false;
}

context.Pdf.ResetReleasePage();
if (_resizePages) {
var ct = ResizePage(context.Page, Settings, _refPaperSize);
Expand All @@ -308,7 +307,6 @@ public bool Process(PageProcessorContext context) {
context.IsPageContentModified = true;
}
_cts[context.PageNumber] = ct;
ct = null;
}
if (_adjustMargins) {
AdjustMargins(context.Page, Settings.Margins);
Expand Down
2 changes: 1 addition & 1 deletion App/Processor/PdfDocumentCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using FreeImageAPI;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using PDFPatcher.Common;
using PDFPatcher.Model;
using iTextImage = iTextSharp.text.Image;
Expand All @@ -34,6 +33,7 @@ sealed class PdfDocumentCreator
new PaperSize (PaperSize.AsNarrowestPage, 0, 0),
new PaperSize (PaperSize.AsLargestPage, 0, 0),
new PaperSize (PaperSize.AsSmallestPage, 0, 0),
new PaperSize (PaperSize.AsFirstPage, 0, 0),
new PaperSize ("16 开 (18.4*26.0)", 1840, 2600),
new PaperSize ("32 开 (13.0*18.4)", 1300, 1840),
new PaperSize ("大 32 开 (14.0*20.3)", 1400, 2030),
Expand Down

0 comments on commit ad398d0

Please sign in to comment.