mirror of
https://github.com/wmjordan/PDFPatcher.git
synced 2026-10-18 13:42:28 +08:00
5449dae0da
!将文档结构探查器迁移到MuPDF库,不再依赖iText
117 lines
3.4 KiB
C#
117 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using BrightIdeasSoftware;
|
|
using MuPDF;
|
|
using PDFPatcher.Common;
|
|
using MuRectangle = MuPDF.Box;
|
|
|
|
namespace PDFPatcher.Functions.Editor;
|
|
|
|
sealed partial class PagePropertyForm : DraggableForm
|
|
{
|
|
Document _document;
|
|
|
|
public int PageNumber { get; set; }
|
|
|
|
public PagePropertyForm() {
|
|
InitializeComponent();
|
|
this.OnFirstLoad(OnLoad);
|
|
}
|
|
|
|
void OnLoad() {
|
|
MinimumSize = Size;
|
|
_MainTab.SelectedIndexChanged += UpdateOpPage;
|
|
_PageDimensionBox.SelectedIndexChanged += _PageDimensionBox_SelectedIndexChanged;
|
|
_CloseButton.Click += (s, args) => Hide();
|
|
_FontNameColumn.AsTyped<MuFontAndSize>(f => f.AspectGetter = o => o.FontName);
|
|
_SizeColumn.AsTyped<MuFontAndSize>(f => f.AspectGetter = o => o.Size);
|
|
_TextStyleBox.ScaleColumnWidths();
|
|
}
|
|
|
|
void UpdateOpPage(object sender, EventArgs e) {
|
|
if (_document != null && _MainTab.SelectedTab == _OpPage) {
|
|
using (var p = new Processor.ContentParser.ContentProcessor(_document)) {
|
|
ContentStreamViewer.SetContent(_OpBox, p, PageNumber);
|
|
}
|
|
_document = null;
|
|
_MainTab.SelectedIndexChanged -= UpdateOpPage;
|
|
}
|
|
}
|
|
|
|
public void LoadPage(Document pdfDocument, Page page) {
|
|
_PageDimensionBox.Items.Clear();
|
|
AddBox(page, page.CropBox, Constants.Content.PageSettings.CropBox);
|
|
AddBox(page, page.MediaBox, Constants.Content.PageSettings.MediaBox);
|
|
AddBox(page, page.TrimBox, Constants.Content.PageSettings.TrimBox);
|
|
AddBox(page, page.ArtBox, Constants.Content.PageSettings.ArtBox);
|
|
AddBox(page, page.BleedBox, Constants.Content.PageSettings.BleedBox);
|
|
_RotationBox.Text = page.Rotation.ToString();
|
|
if (_PageDimensionBox.Items.Count > 0) {
|
|
_PageDimensionBox.SelectedIndex = 0;
|
|
}
|
|
var ts = new HashSet<MuFontAndSize>(new FontAndSizeComparer());
|
|
foreach (var block in page.TextPage) {
|
|
foreach (var line in block) {
|
|
var c = line.FirstCharacter;
|
|
ts.Add(new MuFontAndSize(Model.PdfDocumentFont.RemoveSubsetPrefix(c.Font.Name), c.Size));
|
|
}
|
|
}
|
|
_TextStyleBox.Objects = ts;
|
|
_TextStyleBox.Sort(_SizeColumn, SortOrder.Descending);
|
|
PageNumber = page.PageNumber + 1;
|
|
_document = pdfDocument;
|
|
}
|
|
|
|
void AddBox(Page page, MuRectangle rect, string title) {
|
|
if (rect.IsValid && !rect.IsEmpty) {
|
|
_PageDimensionBox.Items.Add(new Box(rect, title));
|
|
}
|
|
}
|
|
|
|
void _PageDimensionBox_SelectedIndexChanged(object sender, EventArgs args) {
|
|
if (_PageDimensionBox.SelectedItem is not Box v) {
|
|
return;
|
|
}
|
|
var r = v.Rect;
|
|
_TopBox.Text = r.Bottom.ToText();
|
|
_RightBox.Text = r.Right.ToText();
|
|
_BottomBox.Text = r.Top.ToText();
|
|
_LeftBox.Text = r.Left.ToText();
|
|
_WidthBox.Text = r.Width.ToText();
|
|
_HeightBox.Text = r.Height.ToText();
|
|
}
|
|
|
|
protected override void OnDeactivate(EventArgs e) {
|
|
Hide();
|
|
base.OnDeactivate(e);
|
|
}
|
|
|
|
sealed class Box(MuRectangle rect, string title)
|
|
{
|
|
public readonly MuRectangle Rect = rect;
|
|
public readonly string Title = title;
|
|
|
|
public override string ToString() {
|
|
return Title;
|
|
}
|
|
}
|
|
|
|
sealed class MuFontAndSize(string fontName, float size)
|
|
{
|
|
public readonly string FontName = fontName;
|
|
public readonly float Size = size;
|
|
}
|
|
|
|
sealed class FontAndSizeComparer : IEqualityComparer<MuFontAndSize>
|
|
{
|
|
public bool Equals(MuFontAndSize x, MuFontAndSize y) {
|
|
return x.FontName == y.FontName && x.Size == y.Size;
|
|
}
|
|
|
|
public int GetHashCode(MuFontAndSize obj) {
|
|
return obj.FontName.GetHashCode() ^ obj.Size.GetHashCode();
|
|
}
|
|
}
|
|
}
|